Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Literally echo the text "on"

I'm writing a windows CMD Batch file, that (for simplified purposes) is supposed to echo every line of this file:

Flash
your
lights
on 
and
off

The problem is when it gets to the 4th and 6th words, it ends up running echo on and echo off, which does not actually echo the text, instead it just sets the state of echo to be ON and OFF.

for /F %%a in (DataFile.txt) do echo %%a

The resulting Output is:

Flash
your
lights
and

Is there a way to literally echo the text on and off?

like image 385
abelenky Avatar asked Mar 11 '23 02:03

abelenky


2 Answers

According to this post on DosTips.com, the only safe way to echo any text is to use the odd-looking syntax echo(. So for your command line:

for /F %%a in (DataFile.txt) do echo(%%a

There is the syntax echo. commonly used, but this fails in case there is a file called echo. (no extension) in the current working directory.
The syntax echo/ sometimes used fails if you try to echo a string starting with a question mark (because / followed by ? is interpreted as the /? switch to display the help text for the command).

like image 60
aschipfl Avatar answered Mar 23 '23 15:03

aschipfl


Try this:

@echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ %userprofile%\Desktop\DataFile.txt"`) do (
    set "var=%%a"
    SETLOCAL EnableDelayedExpansion
    set "var=!var:*:=!"
    echo(!var!
    ENDLOCAL
)
Pause

Check out this link.

on and off, in the context of what you are doing won't work because they are what you call (reserved words in the language) They are used as @echo off or @echo on

The script I posted got around that minor problem. However my lack of of knowledge in batch forces me to conclude my answer as I have no idea of how the script does it.

like image 30
Jonas Avatar answered Mar 23 '23 13:03

Jonas