`
bardo
  • 浏览: 380985 次
  • 性别: Icon_minigender_1
  • 来自: 上海
博客专栏
D1407912-ab64-3e76-ae37-b31aa4afa398
浅述PHP设计模式
浏览量:11877
9d6df9f7-91da-3787-a37c-0e826525dd5d
Zend Framewor...
浏览量:10194
85b628bd-a2ed-3de2-a4b1-0d34985ae8b6
PHP的IDE(集成开发环...
浏览量:9563
社区版块
存档分类
最新评论

文件夹同步工具源码

    博客分类:
  • VB
 
阅读更多
Option Explicit
'操作说明
'第一步,指定同步的源文件夹
'第二步,指定一个用于映射网络驱动器的盘符
'第三步,指定本地目标文件夹
'第四步,确认开始操作
Dim objNetwork, strDrive, objShell, objUNC, Fs
Dim strRemotePath, strDriveLetter, strNewName
'
strRemotePath = InputBox("请输入远程源文件夹的路径:", "文件同步工具", "\\tp022\help")
strDriveLetter = InputBox("把"&strRemotePath&"映射为本地的驱动器名:", "文件同步工具", "W:")
 
strNewName = "网络硬盘"

Set Fs=WScript.CreateObject("Scripting.FileSystemObject")
Set objNetwork = WScript.CreateObject("WScript.Network")

if not Fs.DriveExists(left(strDriveLetter,1)) then

  ' Section to map the network drive
  objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
  ' Section which actually (re)names the Mapped Drive
  Set objShell = WScript.CreateObject("Shell.Application")
  objShell.NameSpace(strDriveLetter).Self.Name = strNewName
 
end if

On Error Resume Next
Dim DirTotal, TimeSpend, FileTotal, fso, curPath, sPath, objFolder, objFolderItem, strPath

Const MY_COMPUTER = &H11&
Const WINDOW_HANDLE = 0
Const OPTIONS = 0

Set objShell = WScript.CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_COMPUTER)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path

Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, "指定本地同步文件夹:", OPTIONS, strPath)
If objFolder Is Nothing Then
         MsgBox "您没有选择任何有效目录!"
         WScript.Quit
Else
         Set objFolderItem = objFolder.Self
         sPath = objFolderItem.Path
         curPath = sPath
         FileTotal = 0       
         DirTotal = 0        
         if MsgBox ("确认现在开始同步吗?本地目录:" & curPath & " 远程目录: " & strRemotePath, 4, "文件同步工具") = 7 THEN 
          Set fso = Nothing       
          WScript.Quit         
         end if       
        
         TimeSpend = Timer
         myFind strDriveLetter,curPath
         Set fso = Nothing
   objNetwork.RemoveNetWorkDrive strDriveLetter,true,true
         TimeSpend = Round(Timer - TimeSpend, 2)
         MsgBox "处理完成!共耗费时间:" & TimeSpend & " 处理文件数: " & FileTotal
   WScript.Quit
End If

Sub myFind(ByVal thePath, ByVal trgPath)
         Dim fso, trg, myFolder, myFile, trgFile, curFolder, trgFolder, curFolders
         Set fso = WScript.CreateObject("Scripting.FileSystemObject")
         Set trg = WScript.CreateObject("Scripting.FileSystemObject")
         Set curFolders = fso.getfolder(thePath)
         set trgFolder =trg.getfolder(trgPath)
        
         DirTotal = DirTotal + 1       
         If curFolders.Files.Count > 0 Then
                 For Each myFile In curFolders.Files
                         if not trg.FileExists(myFile.Name) then
                          myFile.Copy FormatPath(trgPath) & "\" & myFile.Name
                         else
                          trgFile=trg.GetFile(myFile.Name)
        if ((myFile.Size<>trgFile.Size) OR (myFile.DateCreated<>trgFile.DateCreated) OR (myFile.DateLastModified<>trgFile.DateLastModified)) THEN
         myFile.copy FormatPath(trgPath) & "\" & myFile.Name, true
        end if
       end if
       FileTotal = FileTotal + 1
                 Next
         End If
         If curFolders.subfolders.Count > 0 Then
                 For Each myFolder In curFolders.subfolders
                   if not trg.FolderExists(FormatPath(trgPath) & "\" & myFolder.Name) THEN
                    trg.CreateFolder  FormatPath(trgPath) & "\" & myFolder.Name
                   end if
                         myFind FormatPath(thePath) & "\" & myFolder.Name, FormatPath(trgPath) & "\" & myFolder.Name
                 Next
         End If
End Sub

Function FormatPath(ByVal thePath)
         thePath = Trim(thePath)
         FormatPath = thePath
         If Right(thePath, 1) = "\" Then FormatPath = Mid(thePath, 1, Len(thePath) - 1)
End Function


将以上代码另存为文件名为:syncfiles.vbs 就可以运行使用了
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics