Get the App
SLTechnology News&Howtos  ›  Development  › 

How to copy folders with vbs

Shulou Source: shulou.com Published: 2022-06-03 20:00:29 11月12日 Update

This article mainly introduces "how to copy folders in vbs". In daily operation, I believe many people have doubts about how to copy folders in vbs. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to copy folders in vbs". Next, please follow the editor to study!

The code is as follows:

Dim fso, CopyCount

Set fso = CreateObject ("Scripting.FileSystemObject")

CopyCount = CopyCount + XCopy (fso, ".\ 1", ". 2", True)

MsgBox "copied" & CopyCount & "A file!"

'*

'* Function: XCopy

'*

'* Purpose: copy files and directory trees.

'*

'* Input: fso FileSystemObject object instance

'* source specifies the file to copy.

'* destination specifies the location and / or name of the new file.

'* whether overwrite overwrites existing files. Ture override, False skip

'*

'* Output: returns the number of files copied

'*

'*

Function XCopy (fso, source, destination, overwrite)

Dim s, d, f, l, CopyCount

Set s = fso.GetFolder (source)

If Not fso.FolderExists (destination) Then

Fso.CreateFolder destination

End If

Set d = fso.GetFolder (destination)

CopyCount = 0

For Each f In s.Files

L = d.Path & "\" & f.Name

If Not fso.FileExists (l) Or overwrite Then

If fso.FileExists (l) Then

Fso.DeleteFile l, True

End If

F.Copy l, True

CopyCount = CopyCount + 1

End If

Next

For Each f In s.SubFolders

CopyCount = CopyCount + XCopy (fso, f.Path, d.Path & "\" & f.Name, overwrite)

Next

XCopy = CopyCount

End Function

Create a folder in the path of the script file, name it 1, and put in two files. The result of running the program is as follows

Vbs copies the code for the file:

The code is as follows:

[code]

Dim fso

Set fso = CreateObject ("Scripting.FileSystemObject")

Set fn2=fso.GetFile ("c:\ index2.htm")

Flsize2=fn2.size

Fldate2=fn2.datelastmodified

Set fn=fso.GetFile ("c:\ index.htm")

Flsize1=fn.size

Fldate1=fn.datelastmodified

If fso.FileExists ("c:\ index2.htm") and flsize2 > 50000 and fldate2 > fldate1 Then

Fso.getfile ("c:\ index2.htm") .copy ("c:\ index.htm")

If err.number=0 then WriteHistory "success" & now (), "log.txt"

End if

Sub WriteHistory (hisChars, path)

Const ForReading = 1, ForAppending = 8

Dim fso, f

Set fso = CreateObject ("Scripting.FileSystemObject")

Set f = fso.OpenTextFile (path, ForAppending, True)

F.WriteLine hisChars

F.Close

End Sub

[/ code]

At this point, the study on "how to copy folders in vbs" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Files folders learning code more help practical successful next two number location name instance object copy article method theory directory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB MySQL Xiaomi Shulou Technology