Thomas
Thomas

Reputation: 77

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

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 

Upvotes: 0

Views: 79

Answers (2)

foxidrive
foxidrive

Reputation: 41234

This is a more readable way to create the script:

Opening brackets don't need to be escaped.
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
)

Upvotes: 1

Mofi
Mofi

Reputation: 49086

( 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 input.bat 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.

Upvotes: 1

Related Questions