使用 VBScript 通过 xcopy 复制文件时出现问题 [英] Problem copying files through xcopy using VBScript

查看:42
本文介绍了使用 VBScript 通过 xcopy 复制文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VBScript 使用 xcopy 复制文件.问题是文件夹路径必须由用户输入.假设我将该路径放在一个变量中,比如 h,我该如何在 xcopy 命令中使用这个变量?

I am using VBScript to copy files using xcopy. The problem is that the folder path has to be entered by the user. Assuming I put that path in a variable, say h, how do I use this variable in the xcopy command?

这是我试过的代码:

Dim WshShell, oExec, g, h
h = "D:\newfolder"

g = "xcopy $h D:\y\ /E"
Set WshShell = CreateObject("WScript.Shell")

Set oExec = WshShell.Exec(g)

我也试过 &h 但它没有用.谁能帮我算出正确的语法?任何帮助表示赞赏.

I also tried &h but it did not work. Could anyone help me work out the correct syntax? Any help is appreciated.

推荐答案

问题可能是您没有正确使用引号.试试这个

The problem may be that you are not using quotes properly. Try this

Dim WshShell, oExec,g,h 
h= Chr(34) & "D:\newfolder" & Chr(34)
g="xcopy " & h & " " & Chr(34) & "D:\y\" & Chr(34) & " /E"
Set WshShell = CreateObject("WScript.Shell")

Set oExec = WshShell.Exec(g)

如果任一路径中有空格,则路径必须包含在引号中,Chr(34) 是引号字符,因此通过将它们插入到路径的开头和结尾,它将路径用引号括起来.

If there are spaces in either path the path must be contained in quotes, Chr(34) is the quote character so by inserting them at the beginning and end of the path it wraps the paths in quotes.

假设源路径是 C:\Documents and Settings.如果你将它传递给 xcopy,它会认为源是C:\Documents",目标是and",参数是Settings\".这就是为什么你的路径必须用引号括起来,如果你通过 xcopy "C:\Documents and Settings" "C:\"/e 那么它知道源是 'C:\Documents and Settings' 目的地是 'C:\' 并且参数是 '/e'.

Lets say the source path is C:\Documents and Settings. If you pass that to xcopy it will think the source is 'C:\Documents' the destination will be 'and' and the arguments will be 'Settings\'. This is why your paths have to be wrapped in quotes, if you pass xcopy "C:\Documents and Settings" "C:\" /e then it knows the source is 'C:\Documents and Settings' the destination is 'C:\' and the arguments are '/e'.

这篇关于使用 VBScript 通过 xcopy 复制文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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