2

I have a problem with writing a file with a batch file. It seems that one character is triggering the echo end like this one: the code is a downloader.

C:\ProgramData\Tempfolder\test.vbs ECHO if WScript.Arguments.Count < 1 then

But the file output is this.

if WScript.Arguments.Count  then

Here's my code:

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 )

And the whole echo output is this

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

As you can see, there's one line missing from the echo'd text. Is there some kind of start and end character so I could echo commands like those?

I would be happy if somebody could answer my question.

  • You are writing a batch file that is writing a vbs file. Just write the VBS file and run it. – EBGreen Aug 25 '15 at 19:15
  • 2
    Have you tried escaping the > or < characters, like so: ^> or ^< (For the parts that go into the vbs script) – Jared Allard Aug 25 '15 at 19:16
2

Using >> C:\ProgramData\Tempfolder\test.vbs ECHO if WScript.Arguments.Count <1 then as the example....

If you try this specific line by itself form a command prompt, it will fail with a "The system cannot find the file specified" error.

This is because it's reading the <1 as a file input command (opposite of the output >).

So you'd have to 'escape' the < so that it gets treated like a plain text character in the Echo, and not as a file input redirect command.

Meaning, for this case, you'd want to use:

>> C:\ProgramData\Tempfolder\test.vbs ECHO if WScript.Arguments.Count ^<1 then  

Same with

>> C:\ProgramData\Tempfolder\test.vbs ECHO if WScript.Arguments.Count > 1 then

You'd want to escape the > in that one:

>> C:\ProgramData\Tempfolder\test.vbs ECHO if WScript.Arguments.Count ^> 1 then

The ^ is the batch escape character, and you'll want to keep this in mind for each line you're echoing out, in case it's needed.

From http://ss64.com/nt/syntax-esc.html:

^ Escape character.

Adding the escape character before a command symbol allows it to be treated as ordinary text. When piping or redirecting any of these charcters you should prefix with the escape character: & \ < > ^ |

 e.g.  ^\  ^&  ^|  ^>  ^<  ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.