用于跨网络复制文件和文件夹的VBS脚本 [英] VBS script to copy files and folders across network
本文介绍了用于跨网络复制文件和文件夹的VBS脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将我所有的照片从我的旧笔记本电脑复制到我的新笔记本电脑上。这是一个快速而肮脏的脚本,我将它放在一起(基于此站点上的其他脚本),将文件从一个网络位置复制到另一个网络位置。我希望这个过程能够在网络复制错误的情况下恢复,因为复制我所有的照片总共需要40个小时。
sourceRoot和targetRoot是要在位置之间替换的文件路径的开始部分。lastFileLog是用于跟踪上次复制的文件的文件。这是从部分拷贝恢复所需的。Windows似乎会分配完整的文件大小,即使文件复制失败。因此,我只是跟踪最后一个文件,以便在失败时再次复制它。objStartFolder是源网络位置上的起始路径。'initialize paths
objStartFolder = "\owner-pcdpics"
lastFileLog = "c:Filesmiscarchive.log"
sourceRoot = "\owner-pcd"
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
推荐答案
对于文件夹:请尝试此操作。
Option Explicit
Dim obj,Itemcoll1,a,b
Set obj=CreateObject("Shell.Application")
Function SelectFold1(Desc)
Set SelectFold1=obj.BrowseForFolder(0,Desc,0,"C:UsersMohammed SajjadDesktop")
End Function
Set Itemcoll1=SelectFold1("Copy: ").Items
SelectFold1("Paste: ").CopyHere Itemcoll1 'Use MoveHere if you want to move
MsgBox "Completed"
对于文件:
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 = "HKCUVolatile EnvironmentMsgResp"
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
这篇关于用于跨网络复制文件和文件夹的VBS脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文