【发布时间】:2016-05-28 17:43:59
【问题描述】:
我从来没有真正用 vbs 写过(曾经写过一个在启动时会欢迎我的脚本),但我想要一个基本上可以执行的脚本:
robocopy "folder1" "folder2" /MIR
目前我得到的是从这里复制的脚本VBS Mirror,使用顶部脚本:
此代码同步两个文件的内容(文件和子文件夹) 文件夹。递归遍历每个文件夹,任何丢失的文件夹 子文件夹和文件以两种方式复制。如果对应的文件夹 包含文件名匹配但时间戳不同的文件, 具有最新时间戳的文件将覆盖旧的。
SyncFolders.vbs
Option Explicit
ForceScriptEngine("cscript")
Dim wshArgs
Set wshArgs = Wscript.Arguments
If WshArgs.Count = 2 Then
Call SyncFolders(WshArgs.Item(0), WshArgs.Item(1))
' Also run once in reverse to catch mismatching subfolder count:
Call SyncFolders(WshArgs.Item(1), WshArgs.Item(0))
Else
Wscript.Echo("Wrong number of arguments. Syntax: SyncFolders Folder1 Folder2")
Wscript.Sleep(3000) ' To allow Function syntax popup message to be seen.
End If
Sub SyncFolders(strFolder1, strFolder2)
Dim objFileSys
Dim objFolder1
Dim objFolder2
Dim objFile1
Dim objFile2
Dim objSubFolder
Dim arrFolders
Dim i
Set objFileSys = CreateObject("Scripting.FileSystemObject")
arrFolders = Array(strFolder1, strFolder2)
For i = 0 To 1 ' Make sure that missing folders are created first:
If objFileSys.FolderExists(arrFolders(i)) = False Then
wscript.echo("Creating folder " & arrFolders(i))
objFileSys.CreateFolder(arrFolders(i))
End If
Next
Set objFolder1 = objFileSys.GetFolder(strFolder1)
Set objFolder2 = objFileSys.GetFolder(strFolder2)
For i = 0 To 1
If i = 1 Then ' Reverse direction of file compare in second run
Set objFolder1 = objFileSys.GetFolder(strFolder2)
Set objFolder2 = objFileSys.GetFolder(strFolder1)
End If
For Each objFile1 in objFolder1.files
If Not objFileSys.FileExists(objFolder2 & "\" & objFile1.name) Then
Wscript.Echo("Copying " & objFolder1 & "\" & objFile1.name & _
" to " & objFolder2 & "\" & objFile1.name)
objFileSys.CopyFile objFolder1 & "\" & objFile1.name, _
objFolder2 & "\" & objFile1.name
Else
Set objFile2 = objFileSys.GetFile(objFolder2 & "\" & objFile1.name)
If objFile1.DateLastModified > objFile2.DateLastModified Then
Wscript.Echo("Overwriting " & objFolder2 & "\" & objFile1.name & _
" with " & objFolder1 & "\" & objFile1.name)
objFileSys.CopyFile objFolder1 & "\" & objFile1.name, _
objFolder2 & "\" & objFile1.name
End If
End If
Next
Next
For Each objSubFolder in objFolder1.subFolders
Call SyncFolders(strFolder1 & "\" & objSubFolder.name, strFolder2 & _
"\" & objSubFolder.name)
Next
Set objFileSys = Nothing
End Sub
Sub ForceScriptEngine(strScriptEng)
' Forces this script to be run under the desired scripting host.
' Valid arguments are "wscript" or "cscript".
' The command line arguments are passed on to the new call.
Dim arrArgs
Dim strArgs
For Each arrArgs In WScript.Arguments
strArgs = strArgs & " " & Chr(34) & arrArgs & Chr(34)
Next
If Lcase(Right(Wscript.FullName, 12)) = "\wscript.exe" Then
If Instr(1, Wscript.FullName, strScriptEng, 1) = 0 Then
CreateObject("Wscript.Shell").Run "cscript.exe //Nologo " & _
Chr(34) & Wscript.ScriptFullName & Chr(34) & strArgs
Wscript.Quit
End If
Else
If Instr(1, Wscript.FullName, strScriptEng, 1) = 0 Then
CreateObject("Wscript.Shell").Run "wscript.exe " & Chr(34) & _
Wscript.ScriptFullName & Chr(34) & strArgs
Wscript.Quit
End If
End If
End Sub
我改变了:
Sub SyncFolders(strFolder1, strFolder2)
到
Sub SyncFolders(strC:\Users\Zac\Desktop\Folder, strW:\Folder)
并得到错误“预期')'
我确信这很明显,但有人可以告诉我我需要在该脚本中进行哪些更改以使我的文件夹相互镜像吗?
【问题讨论】:
-
很抱歉,但我仍然不太确定我应该在这里做什么。我已经将“strFolder1”和“strFolder2”更改为我能想到的几乎所有组合中的目录,但仍然出现某种错误?我需要更改哪些才能使其正常工作?
-
你应该这样调用 sub ==> 调用 SyncFolders("C:\Users\Zac\Desktop\Folder" ,"W:\Folder")
-
这里的问题是您将
Sub SyncFolders(strFolder1, strFolder2)定义更改为不正确的语法。就个人而言,在尝试使用这样的脚本之前,您首先需要学习如何编写和构建 VBScript。该脚本是自包含的,您只需调用它并传递预期的参数。
标签: vbscript copy synchronization mirror