如何在vbscript中运行one liner xcopy命令?

时间:2015-06-10 21:54:18

标签: windows batch-file vbscript

我有一个xcopy,由于一些挪威字符,无法在.bat(批处理)文件中正确运行。

该行是:

xcopy /I /V /H /R /C /Y /K /O /X "G:\P - cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF" "F:\CAD\P - cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF"

如果我从批处理文件中运行它,则会因为" 0文件被复制而失败"因为它因字符集而错误地翻译源文件。

我尝试使用chcp更改字符集以及使用Notepad ++并使用各种字符集对其进行编码,但由于我在Windows中的批处理中的限制,它仍然会失败。

所以我的问题是,如何在vbscript中执行上面的行并记录xcopy输出?

我试过了:

set objShell = CreateObject("wscript.Shell")
strSource = "G:\P - Tdwos_cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF"
strDest = "F:\CAD\P - Tdwos_cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF"
command = "xcopy /I /V /H /R /C /Y /K /O /X " & chr(34) & strSource & chr(34) &chr(32) & chr(34)  & strDest & chr(34) & " > F:\testvbs.log 2>&1"
objshell.run command

然后执行" cscript test.vbs"

但不幸的是,尽管如果我将objshell.run更改为wscript.echo,它确实显示了"命令"的正确输出,但这并不起作用。变量...意味着它显示正确的语法。

所以...任何程序员都知道如何在vbscript中正确运行原始xcopy行?如果您知道如何使用编码在批处理文件中正确运行它,那也很好......我还没想到它。

2 个答案:

答案 0 :(得分:1)

下一个脚本在下一个条件下使用组合的挪威语 - 捷克语文件夹和文件名:

  • 保存使用UTF-16LE字节顺序标记编码的脚本0xFFFE
  • 以管理员身份运行脚本!

此处注释的代码段:

' save the script UTF-16LE encoded with the 0xFFFE byte order mark
' run the script as administrator!
' consider //U option: Use Unicode for redirected I/O from the console
'  cscript //U 30767978.vbs  
'
option explicit
dim objShell, strSource, strDest, command, cmd, xx
set objShell = CreateObject("wscript.Shell")

strSource =   "D:\test\éíáýžřčšě\a Ø1000XØ90 b.txt"
strDest =     "D:\test\ěščřžýáíé\a Ø1000XØ90 b.txt"

'                      chcp does not matter: `dir` as well as `xcopy` work for all
cmd = "%comspec% /U /C chcp 65000 & "
cmd = "%comspec% /U /C chcp 65001 & "
cmd = "%comspec% /U /C chcp   437 & "
cmd = "%comspec% /U /C chcp   852 & "
cmd = "%comspec% /U /C chcp  1250 & "
'                      chcp supposedly matters for redirected output

command = "dir " & """" & strSource & """ """ & strDest & """" & " & pause "
xx = objShell.Run( cmd & command, 1, true)

command = "xcopy /I /V /H /R /C /-Y /K /O /X " _
    & """" & strSource & """ """  & strDest & """" & " & pause "
xx = objShell.Run( cmd & command, 1, true)

Wscript.Echo xx, cmd & command 

答案 1 :(得分:0)

虽然JosefZ接受的答案运作良好,但我发现通过简单地使用我的xCopy行并将它们从.bat文件移动到Powershell .ps1文件并运行它,我可以更轻松地处理这个问题。由于某些原因,Powershell对批处理文件等字符集并不感兴趣。

所以我在2003服务器上安装了Powershell并以这种方式运行脚本并且运行良好。

我接受了JosefZ的回答,因为它回答了实际的问题,但是在这里发布这个问题以防其他人遇到同样的问题而想要使用Powershell。