我在使用批处理文件写入文件时遇到问题。似乎有一个字符触发了 echo 结尾,就像这样:代码是一个下载器。
C:\ProgramData\Tempfolder\test.vbs ECHO if WScript.Arguments.Count < 1 then
但是文件输出是这样的。
if WScript.Arguments.Count then
这是我的代码:
mkdir C:\ProgramData\Tempfolder
>> C:\ProgramData\Tempfolder\test.vbs ECHO if WScript.Arguments.Count <1 then
>> C:\ProgramData\Tempfolder\test.vbs ECHO MsgBox "Usage: wget.vbs <url> (file)"
>> C:\ProgramData\Tempfolder\test.vbs ECHO WScript.Quit
>> C:\ProgramData\Tempfolder\test.vbs ECHO end if
>> C:\ProgramData\Tempfolder\test.vbs ECHO URL = WScript.Arguments(0)
>> C:\ProgramData\Tempfolder\test.vbs ECHO if WScript.Arguments.Count > 1 then
>> C:\ProgramData\Tempfolder\test.vbs ECHO saveTo = WScript.Arguments(1)
>> C:\ProgramData\Tempfolder\test.vbs ECHO else
>> C:\ProgramData\Tempfolder\test.vbs ECHO parts = split(url,"/")
>> C:\ProgramData\Tempfolder\test.vbs ECHO saveTo = parts(ubound(parts))
>> C:\ProgramData\Tempfolder\test.vbs ECHO end if
>> C:\ProgramData\Tempfolder\test.vbs ECHO Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
>> C:\ProgramData\Tempfolder\test.vbs ECHO objXMLHTTP.open "GET", URL, false
>> C:\ProgramData\Tempfolder\test.vbs ECHO objXMLHTTP.send()
>> C:\ProgramData\Tempfolder\test.vbs ECHO If objXMLHTTP.Status = 200 Then
>> C:\ProgramData\Tempfolder\test.vbs ECHO Set objADOStream = CreateObject("ADODB.Stream")
>> C:\ProgramData\Tempfolder\test.vbs ECHO objADOStream.Open
>> C:\ProgramData\Tempfolder\test.vbs ECHO objADOStream.Type = 1 'adTypeBinary
>> C:\ProgramData\Tempfolder\test.vbs ECHO objADOStream.Write objXMLHTTP.ResponseBody
>> C:\ProgramData\Tempfolder\test.vbs ECHO objADOStream.Position = 0 'Set the stream position to the start
>> C:\ProgramData\Tempfolder\test.vbs ECHO Set objFSO = Createobject("Scripting.FileSystemObject")
>> C:\ProgramData\Tempfolder\test.vbs ECHO If objFSO.Fileexists(saveTo) Then objFSO.DeleteFile saveTo
>> C:\ProgramData\Tempfolder\test.vbs ECHO Set objFSO = Nothing
>> C:\ProgramData\Tempfolder\test.vbs ECHO objADOStream.SaveToFile saveTo
>> C:\ProgramData\Tempfolder\test.vbs ECHO objADOStream.Close
>> C:\ProgramData\Tempfolder\test.vbs ECHO Set objADOStream = Nothing
>> C:\ProgramData\Tempfolder\test.vbs ECHO End if
>> C:\ProgramData\Tempfolder\test.vbs ECHO Set objXMLHTTP = Nothing
>> C:\ProgramData\Tempfolder\test.vbs ECHO WScript.Quit
start C:\ProgramData\Tempfolder\test.vbs (link goes here )
整个 echo 输出如下
if WScript.Arguments.Count then <---- some numbers are missing
MsgBox "Usage: wget.vbs <url> (file)"
WScript.Quit
end if
URL = WScript.Arguments(0) <------- 1 line is missing
saveTo = WScript.Arguments(1)
else
parts = split(url,"/")
saveTo = parts(ubound(parts))
end if
Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objXMLHTTP.open "GET", URL, false
objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0 'Set the stream position to the start
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(saveTo) Then objFSO.DeleteFile saveTo
Set objFSO = Nothing
objADOStream.SaveToFile saveTo
objADOStream.Close
Set objADOStream = Nothing
End if
Set objXMLHTTP = Nothing
WScript.Quit
如您所见,回显的文本中缺少一行。是否有某种开始和结束字符,以便我可以回显类似的命令?
如果有人能回答我的问题我会很高兴。
答案1
以此>> C:\ProgramData\Tempfolder\test.vbs ECHO if WScript.Arguments.Count <1 then
为例....
如果您尝试在命令提示符中单独执行此特定行,它将失败并出现“系统找不到指定的文件”错误。
这是因为它将其读取<1
为文件输入命令(与输出相反>
)。
因此您必须“转义”,<
以便它在 Echo 中被视为纯文本字符,而不是文件输入重定向命令。
意思是,对于这种情况,您需要使用:
>> C:\ProgramData\Tempfolder\test.vbs ECHO if WScript.Arguments.Count ^<1 then
相同
>> C:\ProgramData\Tempfolder\test.vbs ECHO if WScript.Arguments.Count > 1 then
你想逃离>
那一个:
>> C:\ProgramData\Tempfolder\test.vbs ECHO if WScript.Arguments.Count ^> 1 then
是^
批处理转义字符,您需要在回显的每一行中都记住这一点,以备不时之需。
从http://ss64.com/nt/syntax-esc.html:
^ 转义字符。
在命令符号前添加转义字符可使其被视为普通文本。在管道传输或重定向任何这些字符时,应使用转义字符作为前缀:& \ < > ^ |
e.g. ^\ ^& ^| ^> ^< ^^