Echo a command on screen - file

I would like to echo a command onto the screen. I'm making something of a tutorial for someone and I want to display the command that is going to be running when they press enter.
For example, I have this so far:
echo off
echo Tutorial
pause
echo .
echo .
echo This will show how to read the first line of a text file and place into another text file
echo .
echo .
pause
set /p texte=< test.txt
echo FOR %P IN (%texte%) DO(echo blah >> test2.txt)
pause
However, it won't work when it reaches the last echo, because I'm echoing a command rather than just a text. Is there a way to echo a command?
EDIT: When I try to run something like this, it'll say there is an error once it reaches that last echo command, it says I'm trying to run something following the echo command. But in reality, what I'm trying to do is show the command I'm going to be using on the next line or something along those lines.
This is just an example of what I'm doing, I'm sorry if the actual echo statement just doesn't make sense in general. I'm just wondering if there was a way to echo a command.

> is a special symbol, so you need to escape it. The escape character in bash is the carat: ^ therefore ^>^> should fix that problem, however batch still interprets % differently. for that you need %%. This:
Echo FOR %%P IN (%%texte%%) DO(echo blah ^>^> test2.txt)
will output the command exactly as you want. Also if you add # before your echo off it won't echo echo off at the beginning of your script.

In most of the shells, there is a debug mode to achieve what you want. For example, in Korn shell, you can type set -x to achieve this.

Because no one has answered yet, I'm going to attempt this on my phone.
The reason its giving an error is because you need to escape some stuff.
Echo for ^%p in ^(^%texte^%^) do ^(echo Blah ^>^> test2.txt ^)
That took about 20 minutes so I better get at least an upvote.

Related

CMD: How to echo special command symbol to clipboard?

I need to put this string >>> (it's just some handy copypaste) to clipboard.
Since > is a special cmd-character, I'm using ^ before it to mean all special characters literally.
So far, my Batch code looks like this
(&& pause here is used to see debug messages):
echo ^>^>^> && pause
echo ^>^>^>>"%~dp0foo.txt" && pause
echo foo|clip && pause
echo ^>^>^>|clip && pause
1st line works perfectly (not affecting clipboard though).
2nd line works perfectly (not affecting clipboard either though).
3rd line works perfectly (not using the symbols I need though).
4th line returns >> was unexpected at this time error.
Obviously, I need some syntax tips.
It's a bit tricky, because the pipe creates two new cmd instances (like #aschipf mentioned).
You could use a variable and delayed expansion
set "var=>>>"
cmd /v:on /c "echo(!var!"| clip
Or you can use FOR-variable expansion
set "var=>>>"
( FOR %%X in ("%%var%%") DO #(echo(%%~X^) ) | clip
Okay, I figured an almost decent workaround:
echo ^>^>^>>"%~dp0foo.txt"
type "%~dp0foo.txt"|clip
del "%~dp0foo.txt"
puts >>> into foo.txt right next to your Batch
(it also accounts for spaces in path to the file via ").
returns >>> as a content from foo.txt and puts it into clipboard.
deletes foo.txt right away.
Still hoping to meet a proper-syntax-based solution.

cmd, write and read from txt

i was toying around with cmd a bit and wanted to write a little application which involves a simple feature to read a counter from a txt file, then work with it and at the end raise the counter by one.
set /p x=<file.txt
...
set /a y=x+1
echo %y%>file.txt
Problem is it always returns "ECHO ist eingeschaltet (ON)." which translates to ECHO is turned on (ON) for some reason. Could somebody please explain where it comes from and how to fix it? I dont need anything fancy. I just want it to work and know where my mistake is.
At first, I want to show you how your echo command line should look like:
> "file.txt" echo(%y%
Here is your original line of code again:
echo %y%>file.txt
The reason for the unexpected output ECHO is on./ECHO is off. is because the echo command does not receive anything to echo (type echo /? and read the help text to learn what on/off means). Supposing y carries the value 2, the line expands to:
echo 2>file.txt
The number 2 here is not taken to be echoed here, it is consumed by the redirection instead; according to the article Redirection, 2> constitutes a redirection operator, telling to redirect the stream with the handle 2 (STDERR) to the given file. Such a handle can reach from 0 to 9.
There are some options to overcome that problem:
inserting a SPACE in between the echoed text and the redirection operator:
echo %y% >file.txt
the disadvantage is that the SPACE becomes part of the echoed text;
placing parentheses around the echo command:
(echo %y%)>file.txt
placing the redirection part at the beginning of the command line:
>file.txt echo %y%
I prefer the last option as this is the most general and secure solution. In addition, there is still room for improvement:
quote the file path/name to avoid trouble in case it contains white-spaces or other special characters;
use the odd syntax echo( to be able to output everything, even an empty string or literal strings like on, off and /?;
> "file.txt" echo(%y%
Hint:
To see what is actually going on, do not run a batch file by double-clicking on its icon; open a command prompt window and type its (quoted) path, so the window will remain open, showing any command echoes and error messages. In addition, for debugging a batch file, do not put #echo off on top (or comment it out by preceding rem, or use #echo on) in order to see command echoes.
Echo on means that everything that is executed in the batch is also shown in the console. So you see the command and on the following line the result.
You can turn this off with the echo off command or by preceding a # sign before the command you want to hide.
so
::turns of the echo for the remainder of the batch or untill put back on
::the command itself is not shwn because off the #
#echo off
set /p x=<file.txt
...
::the following won't be shown regardless the setting of echo
#set /a y = x+1
echo %y% > file.txt
EDIT after first comment
because your command echo %y%>file.txt doesn't work, you need a space before the > symbol, now you get the result of echo which gives you the current setting of echo
here a working sample, I put everything in one variable for sake of simplicity.
echo off
set /p x =< file.txt
set /a x += 1
echo %x% > file.txt

Trouble with spaces in batch

I have a question. I have
set /p displaytext=Text to be displayed:
echo "%displaytext%"
pause
and if I input something with spaces it displays what i wrote in quotes. How to get rid of them?. Help! Thanks in advanced. Also if i write something in caps with/without spaces it closes. Basically I want it to display exactly what I type no madder what and without quotes.
Try like below. Just remove the " from "%displaytext%"
#echo off
set /p displaytext=Text to be displayed:
echo %displaytext%
pause
For your reference, see output below for test run
D:\>test.bat
Text to be displayed:hi there user3671588
hi there user3671588
Press any key to continue . . .

Echo off but messages are displayed

I turned off echo in bat file.
#echo off
then I do something like this
...
echo %INSTALL_PATH%
if exist %INSTALL_PATH%(
echo 222
...
)
and I get:
The system cannot find the path specified.
message between those two echos.
What can be the reason of this message and why message ignores echo off?
As Mike Nakis said, echo off only prevents the printing of commands, not results. To hide the result of a command add >nul to the end of the line, and to hide errors add 2>nul. For example:
Del /Q *.tmp >nul 2>nul
Like Krister Andersson said, the reason you get an error is your variable is expanding with spaces:
set INSTALL_PATH=C:\My App\Installer
if exist %INSTALL_PATH% (
Becomes:
if exist C:\My App\Installer (
Which means:
If "C:\My" exists, run "App\Installer" with "(" as the command line argument.
You see the error because you have no folder named "App". Put quotes around the path to prevent this splitting.
Save this as *.bat file and see differences
:: print echo command and its output
echo 1
:: does not print echo command just its output
#echo 2
:: print dir command but not its output
dir > null
:: does not print dir command nor its output
#dir c:\ > null
:: does not print echo (and all other commands) but print its output
#echo off
echo 3
#echo on
REM this comment will appear in console if 'echo off' was not set
#set /p pressedKey=Press any key to exit
"echo off" is not ignored. "echo off" means that you do not want the commands echoed, it does not say anything about the errors produced by the commands.
The lines you showed us look okay, so the problem is probably not there. So, please show us more lines. Also, please show us the exact value of INSTALL_PATH.
#echo off
// quote the path or else it won't work if there are spaces in the path
SET INSTALL_PATH="c:\\etc etc\\test";
if exist %INSTALL_PATH% (
//
echo 222;
)
For me this issue was caused by the file encoding format being wrong.
I used another editor and it was saved as UTF-8-BOM so the very first line I had was #echo off but there was a hidden character in the front of it.
So I changed the encoding to plain old ANSI text, and then the issue went away.

Escaping a batch file echo that starts with a forward slash and question mark

Bit of a tricky one. How can I correctly escape the following in a batch file?
echo /? display this help text
This particular combination of characters is treated as an "ECHO /?" command:
C:\Batch>ECHO /? display this help text
Displays messages, or turns command-echoing on or off.
ECHO [ON | OFF]
ECHO [message]
Type ECHO without parameters to display the current echo setting.
It does not respond to caret (^) escaping, ie. I've tried ^/? /^? and ^/^?.
NB: As a workaround, I found that inserting other characters in between is enough to bypass the ECHO command line processor, eg:
echo ... /? display this help text
Still, this is not ideal and I wondered if there was a way to acheive the desired output, namely with /? at the start of the echoed message.
For escaping echo arguments exists many variants, like echo., echo:, echo=
But only echo( seems to be secure against any appended text.
These one fails, if files exists like echo, echo[, echo] or echo+
echo.
echo[
echo]
echo+
These one fails, if a file in the current directory exists named my.bat
echo\..\my.bat
echo:\..\my.bat
echo.\..\my.bat
These one fails independet of a file
echo/?
echo,/?
echo;/?
Only the echo( seems to be always safe against any content
For escaping echo arguments, you can use the alternative syntax echo.:
echo./?

Resources