用批处理写文件但文本正在消失


2

使用批处理文件编写文件时遇到问题。似乎一个字符正在触发这样的回声结束:代码是一个下载器。

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 )

而整个回声输出就是这个

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

如您所见,回声文本中缺少一行。是否有某种开始和结束字符,所以我可以回应这些命令?

如果有人能回答我的问题,我会很高兴。


您正在编写正在编写vbs文件的批处理文件。只需编写VBS文件并运行即可。
EBGreen 2015年

2
您是否尝试转义>或<字符,如下所示:^>或^ <(对于进入vbs脚本的部分)
Jared Allard 2015年

Answers:


2

>> 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.  ^\  ^&  ^|  ^>  ^<  ^^
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.