【发布时间】:2016-10-26 05:53:56
【问题描述】:
我正在使用this question 的答案中的技术让批处理文件输出另一个文件。问题是当我到达“&”符号时,我得到了错误。这是我的批处理文件,后面是错误输出:
@echo off>zip.vbs
@echo 'Get command-line arguments.>>zip.vbs
@echo Set objArgs = WScript.Arguments>>zip.vbs
@echo InputFolder = objArgs(0)>>zip.vbs
@echo ZipFile = objArgs(1)>>zip.vbs
@echo >>zip.vbs
@echo 'Create empty ZIP file.>>zip.vbs
@echo CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)>>zip.vbs
@echo >>zip.vbs
@echo Set objShell = CreateObject("Shell.Application")>>zip.vbs
@echo >>zip.vbs
@echo Set source = objShell.NameSpace(InputFolder).Items>>zip.vbs
@echo >>zip.vbs
@echo objShell.NameSpace(ZipFile).CopyHere(source)>>zip.vbs
@echo >>zip.vbs
@echo 'Required!>>zip.vbs
@echo wScript.Sleep 2000>>zip.vbs
产生这个 zip2.vbs 文件:
'Get command-line arguments.
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)
ECHO is off.
'Create empty ZIP file.
ECHO is off.
Set objShell = CreateObject("Shell.Application")
ECHO is off.
Set source = objShell.NameSpace(InputFolder).Items
ECHO is off.
objShell.NameSpace(ZipFile).CopyHere(source)
ECHO is off.
'Required!
wScript.Sleep 2000
这个错误输出:
E:\scripts>zip2
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "
PK"
'Chr' is not recognized as an internal or external command,
operable program or batch file.
'Chr' is not recognized as an internal or external command,
operable program or batch file.
'String' is not recognized as an internal or external command,
operable program or batch file.
【问题讨论】:
-
&符号对
cmd有特殊含义,即命令连接运算符,如echo abc & echo def;要回显文字&,您需要像^&一样对其进行转义;<、>、|和^以及(和)也是如此,如果echo命令位于带括号的代码块中。要输出空行,请使用echo(。我建议将重定向放在行首,例如>>"zip.vbs" echo Some text.或>>"zip.vbs" echo(,以避免在输出中出现尾随空格或在>之前的最后一个字符是数字时出现奇怪的行为。 -
在每个 "&" 之前添加 "^" 效果很好。看起来你的评论应该是一个答案!谢谢。
-
它即将推出......
标签: batch-file echo