在VBS中使用xcopy(使用Twist)

时间:2012-07-15 22:44:19

标签: vbscript xcopy folderbrowserdialog

好的,所以这是交易:我正在尝试整理一个执行简单xcopy的脚本。问题是我希望用户能够浏览源文件夹和目标文件夹。我有几个简单的部分,但我无法将它们拼凑在一起,特别是尝试区分源和目标,并试图从浏览功能中获取输出...

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "Example", 1, "c:\Programs")

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "Example", 1, "c:\Programs")

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "Xcopy ""objShell"" ""objShell"" /C /D /E /H /I /K /R /S /Y"

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

像这样,它可以在Windows 7上进行测试

Set objShell = CreateObject("Shell.Application")
Set oFso = CreateObject("Scripting.FileSystemObject")
root = "c:\"
Set source = get_path(objShell.BrowseForFolder(0, "Source folder", 1, root))
Set target = get_path(objShell.BrowseForFolder(0, "Target folder", 1, root))
Set objShell = WScript.CreateObject("WScript.Shell")
command = "Xcopy """&source&""" """&target&""" /C /D /E /H /I /K /R /S /Y"
'wscript.echo command
objShell.Run command

function get_path(foldername)
  Set oFolderItem = foldername.Items.Item
  Set selected = oFso.GetFolder(oFolderItem.Path)
  If selected.IsRootFolder Then
    Set get_path = oFso.GetDrive(selected.Drive)
  Else
    Set get_path = oFso.GetFolder(oFolderItem.Path)
  End If
end function