【问题标题】:Recursive folder synchronization using VBScript (Mirror Folders)使用 VBScript(镜像文件夹)的递归文件夹同步
【发布时间】: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


【解决方案1】:

由于这个vbscript强制处理Cscript引擎;你应该像这样通过小批量执行它:

@echo off
Cscript /nologo SyncFolders.vbs "C:\Users\Zac\Desktop\Folder" "W:\Folder"
pause

编辑: 如果你想避免这个批处理并使用 Cscript 引擎,试试这个:

Option Explicit
'You must only change the absolute paths of the two folders here
Call SyncFolders("C:\Users\Zac\Desktop\Folder","W:\Folder") 
'**********************Don't Change nothing below this line *****************************
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
'********************************************************************************

【讨论】:

  • 所以我使用了一个批处理文件,仍然得到一个预期的')'错误,我在没有批处理文件的情况下运行它,得到同样的错误,错误消息是;行:15 字符:18 错误:应为 ')'
  • 哈哈,它正在工作,谢谢,还有一个问题,我如何停止出现对话框,我打算在连接“W:\”驱动器时进行自动运行,并且不想与它有任何互动。
  • @ZacharyWight 这就是为什么你使用cscript.exe ;)
  • @ZacharyWight 只对你找到 wscript.echo 的每一行进行 cmets;所以现在就检查吧!
  • 如果您使用WScript.Echo 语句运行但使用cscript.exe 调用脚本,它们将输出到StdOut 缓冲区,无需交互。没有互动和没有反馈是两个不同的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-05
  • 1970-01-01
  • 2013-02-03
相关资源
最近更新 更多