86947

Why gives my script an other output with the echo command?

Question:

The output is not what I expected to get... especially the first piece of the output. I

think it goes wrong with the @ token. But I couldnt find anything about it and cant

figure it out.

Does anyone know what I am doing wrong??

THNX

This is my script:

if not exist input.bat ( echo @echo off > input.bat echo title Input >> input.bat echo :set >> input.bat echo MODE CON: COLS=29 LINES=5 >> input.bat echo :loop >> input.bat echo cls >> input.bat echo echo Gebruik de wasd toetsen >> input.bat echo echo om te bewegen >> input.bat echo echo a/Left d/Right >> input.bat echo choice /c:wscradp /n >> input.bat echo if ERRORLEVEL 6 ( >> input.bat echo echo d^>action.txt >> input.bat echo goto loop) >> input.bat echo if ERRORLEVEL 5 ( >> input.bat echo echo a^>action.txt >> input.bat echo goto loop) >> input.bat echo if ERRORLEVEL 4 ( >> input.bat echo echo r^>action.txt >> input.bat echo goto loop) >> input.bat echo if ERRORLEVEL 3 ( >> input.bat echo taskkill /f /im cmd.exe >> input.bat echo exit >> input.bat echo ) >> input.bat echo if ERRORLEVEL 2 ( >> input.bat echo echo s^>action.txt >> input.bat echo goto loop) >> input.bat echo if ERRORLEVEL 1 echo w^>action.txt >> input.bat echo goto loop >> input.bat )

This is the output (the file input.bat) when input.bat doesn`t exist:

goto loop if ERRORLEVEL 5 ( echo a>action.txt goto loop) if ERRORLEVEL 4 ( echo r>action.txt goto loop) if ERRORLEVEL 3 ( taskkill /f /im cmd.exe exit ) if ERRORLEVEL 2 ( echo s>action.txt goto loop) if ERRORLEVEL 1 echo w>action.txt goto loop

Answer1:

( and ) must be also escaped with ^.

The last page on entering cmd.exe /? in a command prompt window contains the list of special characters which have a special meaning in batch files and therefore require an escape character to be interpreted as literal character if not within a double quoted string.

if not exist input.bat ( echo @echo off > input.bat echo title Input >> input.bat echo :set >> input.bat echo MODE CON: COLS=29 LINES=5 >> input.bat echo :loop >> input.bat echo cls >> input.bat echo echo Gebruik de wasd toetsen >> input.bat echo echo om te bewegen >> input.bat echo echo a/Left d/Right >> input.bat echo choice /c:wscradp /n >> input.bat echo if ERRORLEVEL 6 ^( >> input.bat echo echo d^>action.txt >> input.bat echo goto loop^) >> input.bat echo if ERRORLEVEL 5 ^( >> input.bat echo echo a^>action.txt >> input.bat echo goto loop^) >> input.bat echo if ERRORLEVEL 4 ^( >> input.bat echo echo r^>action.txt >> input.bat echo goto loop^) >> input.bat echo if ERRORLEVEL 3 ^( >> input.bat echo taskkill /f /im cmd.exe >> input.bat echo exit >> input.bat echo ^) >> input.bat echo if ERRORLEVEL 2 ^( >> input.bat echo echo s^>action.txt >> input.bat echo goto loop^) >> input.bat echo if ERRORLEVEL 1 echo w^>action.txt >> input.bat echo goto loop >> input.bat )

Even better would be

if not exist input.bat ( echo @echo off> input.bat echo title Input>> input.bat echo :set>> input.bat echo MODE CON: COLS=29 LINES=^5>> input.bat echo :loop>> input.bat echo cls>> input.bat echo echo Gebruik de wasd toetsen>> input.bat echo echo om te bewegen>> input.bat echo echo a/Left d/Right>> input.bat echo choice /c:wscradp /n>> input.bat echo if ERRORLEVEL 6 ^(>> input.bat echo echo d^>action.txt>> input.bat echo goto loop^)>> input.bat echo if ERRORLEVEL 5 ^(>> input.bat echo echo a^>action.txt>> input.bat echo goto loop^)>> input.bat echo if ERRORLEVEL 4 ^(>> input.bat echo echo r^>action.txt>> input.bat echo goto loop^)>> input.bat echo if ERRORLEVEL 3 ^(>> input.bat echo taskkill /f /im cmd.exe>> input.bat echo exit>> input.bat echo ^)>> input.bat echo if ERRORLEVEL 2 ^(>> input.bat echo echo s^>action.txt>> input.bat echo goto loop^)>> input.bat echo if ERRORLEVEL 1 echo w^>action.txt>> input.bat echo goto loop>> input.bat )

This version with no space left of >> avoids a trailing space in created <em>input.bat</em> on every line. To get this version to work for all lines it is necessary to escape also 5 in line 5 or 5>> would be interpreted instead of appending character 5 to the file.

Answer2:

This is a more readable way to create the script:

Opening brackets don't need to be escaped.<br /> You may also prefer to use echo( as it is more resistant to errors that occur from various leading characters, and it is faster.

@echo off if not exist input.bat ( ( echo @echo off echo title Input echo :set echo MODE CON: COLS=29 LINES=5 echo :loop echo cls echo echo Gebruik de wasd toetsen echo echo om te bewegen echo echo a/Left d/Right echo choice /c:wscradp /n echo if ERRORLEVEL 6 ( echo echo d^>action.txt echo goto loop^) echo if ERRORLEVEL 5 ( echo echo a^>action.txt echo goto loop^) echo if ERRORLEVEL 4 ( echo echo r^>action.txt echo goto loop^) echo if ERRORLEVEL 3 ( echo taskkill /f /im cmd.exe echo exit echo ^) echo if ERRORLEVEL 2 ( echo echo s^>action.txt echo goto loop^) echo if ERRORLEVEL 1 echo w^>action.txt echo goto loop )>input.bat )

Recommend

  • OnSharedPreferenceChangeListener not called #2
  • On submit the form don't display its POST data
  • Using iText to fill text field background
  • how to catch an array of nodes to a property
  • SPListItem in FormsLib does not handle blank values
  • Java webservice NoClassDefFoundError
  • Is it possible to clear the console tab during runtime in Intellij with Java?
  • How to to parallelize the matrix transpose?
  • Read a variable from a text file
  • Multiple materials with JSONLoader?
  • How objects are created when the prototype of their constructor isn't an object?
  • Gephi's java default method not implemented in C# with an ikvm-from dll library
  • Why the event AbstractAuthenticationFailureEvent is never triggered in spring security?
  • Splitting string into groups of specific length
  • Disconnect FB user from using App
  • DevExpress: How do get an instance of a control client-side and access its client-side members?
  • Create unique ids for a group
  • How to sort SimpleXMLElement on PHP
  • maven-cobertura-plugion does not show the sources [closed]
  • how to implement `Jackson AnnotationIntrospector`?
  • How to initialize global variable at each class invocation?
  • Error: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
  • Simple script doesn't show anything on the Output in LuaEdit
  • Passing and ArrayList through intent
  • Focus Traversal Policy in TitledPane
  • CSS table cell alignment and ellipsis not working
  • Use neo4j server instead of embedded mode
  • Automapper missing type map configuration or unsupported mapping
  • Are there any libraries for Python to simulate keyboard action?
  • Pass Dictionary to Javascript array
  • How to know when stdin is empty if it contains EOF?
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Disable Enter in editText android
  • Read text file and split every line in MSBuild
  • How to check if every primary key value is being referenced as foreign key in another table
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Return words with double consecutive letters
  • how to add data labels for bar graph in matlab
  • python regex in pyparsing
  • Reading document lines to the user (python)