使用VBScript(镜像文件夹)的递归文件夹同步 [英] Recursive folder synchronization using VBScript (Mirror Folders)

查看:338
本文介绍了使用VBScript(镜像文件夹)的递归文件夹同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有真正写过vbs(曾经写过一个脚本,欢迎我在开机),但我是一个脚本基本上执行:

  robocopyfolder1folder2/ MIR 

我有一个复制的脚本从这里 VBS镜像,使用顶部脚本:


此代码同步两个
文件夹的内容(文件和子文件夹)。每个文件夹都被递归遍历,任何缺失的
子文件夹和文件都被复制。如果相应的文件夹
包含具有匹配文件名但具有不同时间戳的文件,则
具有最新时间戳的文件将覆盖较旧的时间戳。


SyncFolders.vbs

 显式
ForceScriptEngine(cscript)

Dim wshArgs
设置wshArgs = Wscript.Arguments
如果WshArgs.Count = 2然后
调用SyncFolders
同样运行一次反向捕获不匹配的子文件夹计数:
调用SyncFolders(WshArgs.Item(1),WshArgs.Item(0))
Else
Wscript.Echo(Wrong number of arguments。语法:SyncFolders Folder1 Folder2)
Wscript.Sleep(3000)'允许看到函数语法弹出消息。
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'确保首先创建缺少的文件夹:
If objFileSys.FolderExists(arrFolders(i))= False然后
wscript.echo(Creating folder& arrFolders )
objFileSys.CreateFolder(arrFolders(i))
结束如果
下一页
设置objFolder1 = objFileSys.GetFolder(strFolder1)
设置objFolder2 = objFileSys.GetFolder strFolder2)
对于i = 0到1
如果i = 1那么'第二次运行时文件比较的反向
设置objFolder1 = objFileSys.GetFolder(strFolder2)
设置objFolder2 = objFileSys.GetFolder(strFolder1)
结束如果
对于objFolder1.files中的每个objFile1
如果不是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
设置objFile2 = objFileSys.GetFile(objFolder2&\& objFile1.name)
如果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
对于objFolder1.subFolders中的每个objSubFolder
调用SyncFolders(strFolder1&\ & objSubFolder.name,strFolder2& _
\& objSubFolder.name)
下一页
Set objFileSys = Nothing
End Sub
$ b b Sub ForceScriptEngine(strScriptEng)
'强制此脚本在所需的脚本宿主下运行。
'有效的参数是wscript或cscript。
'命令行参数传递给新调用。
Dim arrArgs
Dim strArgs
对于每个arrArgs在WScript.Arguments中
strArgs = strArgs& & Chr(34)& arrArgs&那么
如果使用Instr(1,Wscript.FullName,strScriptEng,1),那么
)= 0然后
CreateObject(Wscript.Shell)。运行cscript.exe // Nologo& _
Chr(34)& Wscript.ScriptFullName& Chr(34)& strArgs
Wscript.Quit
End If
Else
如果Instr(1,Wscript.FullName,strScriptEng,1)= 0则
CreateObject(Wscript.Shell ).Runwscript.exe& Chr(34)& _
Wscript.ScriptFullName& Chr(34)& strArgs
Wscript.Quit
结束如果
结束如果
结束Sub

我改变了:

  Sub SyncFolders(strFolder1,strFolder2)



  Sub SyncFolders(strC: \\Users\Zac\Desktop\Folder,strW:\Folder)

错误Expected)'



我相信这是一个非常明显的东西,但有人可以告诉我,我需要改变的脚本,使我的文件夹镜像每个其他?

解决方案

由于这个vbscript强制处理Cscript引擎,你应该按照这样的方式执行: / p>

  @echo off 
Cscript / nologo SyncFolders.vbsC:\Users\Zac\Desktop\文件夹W:\Folder
暂停


如果您想要避免此批处理并使用Cscript引擎,请尝试:

 选项显式
'你必须只改变这两个文件夹的绝对路径
调用SyncFolders(C:\Users\Zac\Desktop\Folder,W: \\ Folder)
'**********************不要更改此行下方任何内容*********** ******************
Sub SyncFolders(strFolder1,strFolder2)
Dim objFileSys
Dim objFolder1
Dim objFolder2
Dim objFile1
Dim objFile2
Dim objSubFolder
Dim arrFolders
Dim i
设置objFileSys = CreateObject(Scripting.FileSystemObject)
arrFolders = Array strFolder1,strFolder2)
For i = 0 To 1'确保首先创建缺少的文件夹:
If objFileSys.FolderExists(arrFolders(i))= False然后
'wscript.echo 创建文件夹&
objFileSys.CreateFolder(arrFolders(i))
结束如果
接下来
设置objFolder1 = objFileSys.GetFolder(strFolder1)
设置objFolder2 = objFileSys .GetFolder(strFolder2)
For i = 0到1
如果i = 1那么'第二次运行时文件比较的反向
设置objFolder1 = objFileSys.GetFolder(strFolder2)
设置objFolder2 = objFileSys.GetFolder(strFolder1)
结束如果
对于objFolder1.files中的每个objFile1
如果不是objFileSys.FileExists(objFolder2&\& objFile1.name)
'Wscript.Echo(Copying& objFolder1&\& objFile1.name& _
'至& objFolder2&\& objFile1。 name)
objFileSys.CopyFile objFolder1& \& objFile1.name,_
objFolder2& \& objFile1.name
Else
设置objFile2 = objFileSys.GetFile(objFolder2&\& objFile1.name)
如果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
对于objFolder1.subFolders中的每个objSubFolder
调用SyncFolders(strFolder1&\ & objSubFolder.name,strFolder2& _
\& objSubFolder.name)
下一页
Set objFileSys = Nothing
End Sub
'** **************************************************** ****************************


I've never really written in vbs (once wrote a script that would welcome me on boot) but I'm after a script to essentially perform :

robocopy "folder1" "folder2" /MIR

At the moment what I've got is a copied script from here VBS Mirror, Using the top script :

This code synchronizes the contents (files and subfolders) of two folders. Each folder is traversed recursively and any missing subfolders and files are copied both ways. If corresponding folders contain files with matching file names but with different time stamps, the file with the newest time stamp will overwrite the older.

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

I changed the :

Sub SyncFolders(strFolder1, strFolder2)

to

Sub SyncFolders(strC:\Users\Zac\Desktop\Folder, strW:\Folder)

and get the error "Expected ')'

I'm sure it's something very obvious, but could someone please tell me what I need to change in that script to make my folders mirror each other?

解决方案

Since this vbscript force to deal with Cscript engine; you should execute it by a litlle batch like this way :

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

Edit : And if you like to avoid this batch and using Cscript engine,try this :

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
'********************************************************************************

这篇关于使用VBScript(镜像文件夹)的递归文件夹同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆