用于跨网络复制文件和文件夹的VBS脚本

时间:2015-10-06 11:51:00

标签: file vbscript copy

我需要将所有照片从旧笔记本电脑复制到我的新笔记本电脑上。这是一个快速而脏的脚本,我将其放在一起(基于此站点上的其他脚本)将文件从一个网络位置复制到另一个网络位置。我希望该过程能够在网络复制错误的情况下恢复,因为复制所有照片的总时间是40小时。

sourceRoot和targetRoot是要在位置之间替换的文件路径的开始部分。 lastFileLog是一个用于跟踪最后复制文件的文件。需要从部分副本中恢复。即使文件无法复制,Windows似乎也会分配完整的文件大小。所以我只是跟踪最后一个文件,在失败时再次复制它。 objStartFolder是源网络位置的起始路径。

'initialize paths
objStartFolder = "\\owner-pc\d\pics"
lastFileLog = "c:\Files\misc\archive.log"
sourceRoot = "\\owner-pc\d"
targetRoot = "c:\Files"


Set objFSO = CreateObject("Scripting.FileSystemObject")

'read log
Set objFile = objFSO.OpenTextFile(lastFileLog)
Do Until objFile.AtEndOfStream
    replacefile= objFile.ReadLine
    Wscript.Echo "This file will be replaced: " & replacefile
Loop
objFile.Close

'copy files
Set objFolder = objFSO.GetFolder(objStartFolder)
ShowSubfolders objFSO.GetFolder(objStartFolder)

'clear log
Set objFileLog = objFSO.CreateTextFile(lastFileLog,True)
objFileLog.Write ""
objFileLog.Close

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders

        Wscript.Echo Subfolder.Path

        if not(objFSO.FolderExists(replace(Subfolder.Path,sourceRoot,targetRoot))) then
          objFSO.CreateFolder(replace(Subfolder.Path,sourceRoot,targetRoot))
        end if

        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles

            if not(objFSO.FileExists(replace(Subfolder.Path & "\" & objFile.Name,sourceRoot,targetRoot))) then 
              Wscript.Echo Subfolder.Path & "\" & objFile.Name

              Set objFileLog = objFSO.CreateTextFile(lastFileLog,True)
              objFileLog.Write Subfolder.Path & "\" & objFile.Name
              objFileLog.Close

              objFSO.CopyFile Subfolder.Path & "\" & objFile.Name, replace(Subfolder.Path & "\" & objFile.Name,sourceRoot,targetRoot)

            elseif replacefile = Subfolder.Path & "\" & objFile.Name then
              Wscript.Echo "Replacing ... " & Subfolder.Path & "\" & objFile.Name  
              objFSO.CopyFile Subfolder.Path & "\" & objFile.Name, replace(Subfolder.Path & "\" & objFile.Name,sourceRoot,targetRoot),true            
            else
              Wscript.Echo "Skip ... " & Subfolder.Path & "\" & objFile.Name
            end if
        Next
        ShowSubFolders Subfolder
    Next
end sub

1 个答案:

答案 0 :(得分:0)

对于文件夹:试试这个。

Option Explicit
Dim obj,Itemcoll1,a,b
Set obj=CreateObject("Shell.Application")
Function SelectFold1(Desc)
Set SelectFold1=obj.BrowseForFolder(0,Desc,0,"C:\Users\Mohammed Sajjad\Desktop\")
End Function

Set Itemcoll1=SelectFold1("Copy: ").Items
SelectFold1("Paste: ").CopyHere Itemcoll1 'Use MoveHere if you want to move
MsgBox "Completed"

For File:

Option Explicit
Dim objApp : Set objApp = CreateObject("Shell.Application")
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objSHL : Set objSHL = CreateObject("WScript.Shell")

'Browse for Folder
'----------------------------------------------------------
Function SelectFold()
     Dim objFolder
     Set objFolder = objApp.BrowseForFolder(0,"Select a Folder",0,0)
     If objFolder Is Nothing Then
     MsgBox "Canceled"
     WScript.Quit
     Else
     SelectFold = objFolder.Self.Path & "\"
     End If
End Function
'----------------------------------------------------------
'Browse for file
'----------------------------------------------------------
Function SelectFile()
Dim tempFolder : Set tempFolder = objFSO.GetSpecialFolder(2)
Dim tempFile : tempFile = objFSO.GetTempName() & ".hta"
Dim path : path = "HKCU\Volatile Environment\MsgResp"
 With tempFolder.CreateTextFile(tempFile)
    .Write "<input type=file name=f>" & _
     "<script>f.click();(new ActiveXObject('WScript.Shell'))" & _
     ".RegWrite('HKCU\\Volatile Environment\\MsgResp', f.value);" & _
     "close();</script>"
    .Close
 End With
 objSHL.Run tempFolder & "\" & tempFile, 0, True
 If objSHL.RegRead(path) = "" Then
  objSHL.RegDelete path
  objFSO.DeleteFile tempFolder & "\" & tempFile
  WScript.Quit
 End If
 SelectFile = objSHL.RegRead(path)
 objSHL.RegDelete path
 objFSO.DeleteFile tempFolder & "\" & tempFile
End Function
'----------------------------------------------------------
objFSO.CopyFile SelectFile, SelectFold