Search code examples
batch-fileescapingechospecial-characters

Batch - Won't echo a specific line to file


I am trying to write some code to a VBS file through batch. Everything works fine except

echo if(Time()>TimeValue("8:30am") and Time()<TimeValue("3:15pm")) then >> 

This is among other echos to this file like

echo do >> %APPDATA%\tbwg22f.vbs
echo if(Time()>TimeValue("8:30am") and Time()<TimeValue("3:15pm")) then >> %APPDATA%\tbwg22f.vbs
echo Dim message, sapi >> %APPDATA%\tbwg22f.vbs

The if(Time) line won't echo to my VBS file but all the other ones do


Solution

  • (, ), > are special characters to the shell. You need to escape them to print as a literal string

    echo if^(Time^(^)^>TimeValue("8:30am") and Time^(^)^<TimeValue^("3:15pm"^)^) then >>