VBS Replication Folder Implementation code _vbs

Source: Internet
Author: User

Need to implement a copy folder function, the Internet to find the relevant code, and made improvements, the VBS script is as follows

Copy Code code as follows:

Dim FSO, Copycount
Set fso = CreateObject ("Scripting.FileSystemObject")

Copycount = Copycount + XCopy (FSO, ". \1", ". \2", True)
MsgBox "Copy" & Copycount & "Files!"

'********************************************************************
' * 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.
' * Overwrite whether to overwrite existing files. Ture Overwrite, False skip
'*
' * Output: Returns the number of copied files
'*
'********************************************************************
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

In the script file path to create a folder, named 1, put two files, run the program results are as follows



VBS copy the code for the file:

Copy Code code 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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.