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
Related
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.
So I wrote a batch that has some code to check how many times it has been run by reading a textfile and then writing back into that textfile the new, increased number.
#ECHO OFF
for /f "delims=" %%x in (TimesRun.txt) do set Build=%%x
set Build=%Build%+1
#echo Build value : %Build%
echo %Build%>>TimesRun.txt
Pause
That does append the textfile allright, but it adds "1+1" to it. Silly me! I forgot to use the /a switch to enable arithmetic operations! But when I change the code accordingly...
#ECHO OFF
for /f "delims=" %%x in (TimesRun.txt) do set Build=%%x
set /a Build=%Build%+1
#echo Build value : %Build%
echo %Build%>>TimesRun.txt
Pause
... something funny happens: Instead of appending my file, ECHO is off. gets written on the console. Now, I know that this usually happens when ECHO is used without text or with an empty variable. I have added the first #echo Build value : %Build% specifically to see whether the variable Build is empty or not; it is not, and the calculation was carried out correctly.
I already figured out that
>>TimesRun.txt (echo %Build%)
does bring the desired result. I still do not understand why
echo %Build%>>TimesRun.txt
does not, however. What am I missing?
You are unintentionally specifying a redirection handle.
Redirection allows you to specify a certain handle that defines what is to be redirected:
0 = STDIN (keyboard input)
1 = STDOUT (text output)
2 = STDERR (error text output)
3 ~ 9 = undefined
For the input redirection operator <, handle 0 is used by default; for the output redirection operators > and >>, the default handle is 1.
You can explicitly specify a handle by putting a single numeric figure in front of the redirection operator; for instance 2> defines to redirect the error text output.
In your echo command line you are doing exactly this unintentionally, when %Build% is a single numberic digit, like 1 for example:
echo 1>>TimesRun.txt
To avoid that, you have the following options:
To reverse the statement so that the redirection definition comes first:
>>TimesRun.txt echo %Build%
This is the most general and secure way of doing redirections.
To enclose the redirected command in parentheses:
(echo %Build%)>>TimesRun.txt
This also works safely.
To put a SPACE in front of the redirection operator:
echo %Build% >>TimesRun.txt
This works too, but the additional SPACE is included in the output of echo.
See also this great post: cmd.exe redirection operators order and position.
Batch file redirection can be customized to specify where you're outputting to.
command 1>file.txt redirects the output of STDOUT to file.txt
command 2>file.txt redirects the output of STDERR to file.txt
Your build value was 1, so you inadvertently told CMD to send the output of echo to TimesRun.txt - when you run echo by itself, it prints it's status (ON or OFF).
You also could have said echo %Build% >>TimesRun.txt and the space would prevent the value of Build from being treated as a redirection command.
The Microsoft article Using command redirection operators explains the 3 standard handles and how to redirect them to another handle, command, device, file or console application.
Redirection of output written to handle 1 - STDOUT - to a file should be done with just
using > ... create file if not already existing or overwrite existing file, or
using >> ... create file if not already existing or append to existing file.
The redirection operators are usually appended at end of a command line. But this is problematic in case of using command ECHO and the string output to STDOUT ends with 1 to 9.
One of several solutions is to specify in this case the redirection at beginning of the command line:
#for /F "delims=" %%x in (TimesRun.txt) do #set Build=%%x
#set /A Build+=1
#echo Build value : %Build%
>>TimesRun.txt echo %Build%
Executing this small batch file without #echo off at top from within a command prompt window shows what Windows command processor executes after preprocessing each line with text file TimesRun.txt containing currently the value 0 or does not exist at all.
echo 1 1>>TimesRun.txt
It can be seen that Windows command interpreter moved the redirection to end of line with inserting a space and 1 left to >>.
With above batch code the line with >> really executed after preprocessing is:
echo 2 1>>TimesRun.txt
Specifying the redirection at end with 1>>, i.e. use in the batch file
echo %Build%1>>TimesRun.txt
is also no good idea as this would result on first run in executing the line:
echo 11 1>>TimesRun.txt
So 11 is written into the file instead of 1. This wrong output could be avoided by inserting a space before >> or 1>>, i.e. use one of those two:
echo %Build% >>TimesRun.txt
echo %Build% 1>>TimesRun.txt
But then the space after %Build% is also written into the file as really executed is:
echo 1 1>>TimesRun.txt
The trailing space would be no problem here, but should be nevertheless avoided.
Note: On using arithmetic operations, i.e. set /A ... any string not being a number or operator is automatically interpreted as variable name and the current value of this variable is used on evaluating the arithmetic expression. Therefore after set /A with environment variable names consisting only of word characters and starting with an alphabetic character as usually used for environment variables no %...% or !...! must be used inside the arithmetic expression. This is explained in help of command SET output into console window on running set /? within a command prompt window.
I wrote the following:
#ECHO OFF
SET v1old=4
SET v2old=0
SET v3old=7453
SET v1new=83
SET v2new=645
SET v3new=2
SET bug=SET
ECHO #ECHO OFF ^& SETLOCAL>>newupdate.bat
ECHO REM .>>newupdate.bat
ECHO REM version von dolphin>>newupdate.bat
ECHO %bug% v1old=%v1new%>>newupdate.bat
ECHO %bug% v2old=%v2new%>>newupdate.bat
ECHO %bug% v3old=%v3new%>>newupdate.bat
ECHO %bug% bug=%bug%
PAUSE
EXIT
The following line will be shown in console instead of beeing written into the bat file:
ECHO %bug% v3old=%v3new%>>newupdate.bat
The variable v3new stores a value from 0-9 if I let them store something other like chars, signs or values with more then 1 digit it will work...
I want that the script mutates himself where only the first lines will change.
So I want to write the first lines to a new file...
Why does it not work, is that a bug or do I do something wrong?
Solutions
The answer by Seth McCauley works fine for the echo-ed set lines.
An alternative and more generic solution is to put every echo within parentheses like this, hence the original set string is redirected without any modification:
(ECHO %bug% v3old=%v3new%)>>newupdate.bat
Another yet worse possibility is to insert a single SPACE before redirection >/>>; this prevents the line from being displayed in the console window, but the SPACE is redirected as well (and, when newupdate.bat is executed, it will be assigned to the variable value as well).
Root Cause
The problem is the combination of characters that appear in the failing echo line (the third one here):
ECHO %bug% v1old=%v1new%>>newupdate.bat
ECHO %bug% v2old=%v2new%>>newupdate.bat
ECHO %bug% v3old=%v3new%>>newupdate.bat
Remove #echo off from the top of your batch script temporarily and you will see what command line is echoed to the console respectively:
ECHO SET v1old=83 1>>newupdate.bat
ECHO SET v2old=645 1>>newupdate.bat
ECHO SET v3old= 2>>newupdate.bat
The console shows the unwanted response, which is not written to newupdate.bat unintentionally:
SET v3old=
This is because of the character substring =2>> in the expanded and echoed command. Why:
= acts as a separator, like also SPACE, ,, ;.
Because of the preceding separator =, 2>> is treated as the redirection operator.
2, since it is a one-digit number immediately preceding >>, is taken as a handle for redirection, where 2 means STDERR (1 is STDOUT, the default for >/>>; 0 is STRIN, 3 - 9 are undefined).
All this means that STDERR is redirected into newupdate.bat rather than the echo-ed line which appears at STDOUT (handle 1). STDERR carries no data here so nothing goes to the file.
For the other lines, as at least one of the above listed conditions is violated, redirection is not modified unintentionally, so >> (same as 1>>) is applied, so STDOUT is redirected into the file and STDERR goes to the console but is empty anyway.
So STDOUT carries the following data with respect to the above three lines:
SET v1old=83
SET v2old=645
Note that without redirection, both STDOUT and STDERR were passed to the console as per default.
Reference this resource for the redirection syntax and this post for a great explanation of redirection.
Conclusion
Since there is a separator (one of SPACE, ,, ;, =) followed by a variable string portion followed by redirection signs >>, it could happen that unintended redirections occur, when the said central string becomes a single decimal figure.
All of the solutions presented on top of this answer (including the referenced one) work because there is a character inserted in between the redirection mark >> and the preceding string portion.
Here are two modern methods to echo into a file without trailing spaces:
Note the first redirection only has one > to create or overwrite the file.
#ECHO OFF
SET v1old=4
SET v2old=0
SET v3old=7453
SET v1new=83
SET v2new=645
SET v3new=2
SET bug=SET
>newupdate.bat ECHO #ECHO OFF ^& SETLOCAL
>>newupdate.bat ECHO REM .
>>newupdate.bat ECHO REM version von dolphin
>>newupdate.bat ECHO %bug% v1old=%v1new%
>>newupdate.bat ECHO %bug% v2old=%v2new%
>>newupdate.bat ECHO %bug% v3old=%v3new%
ECHO %bug% bug=%bug%
PAUSE
EXIT
This method is very useful, but ) need to be escaped to ^) also
#ECHO OFF
SET v1old=4
SET v2old=0
SET v3old=7453
SET v1new=83
SET v2new=645
SET v3new=2
SET bug=SET
(
ECHO #ECHO OFF ^& SETLOCAL
ECHO REM .
ECHO REM version von dolphin
ECHO %bug% v1old=%v1new%
ECHO %bug% v2old=%v2new%
ECHO %bug% v3old=%v3new%
)>newupdate.bat
ECHO %bug% bug=%bug%
PAUSE
EXIT
The batch echo command has a variety of issues displaying certain content. Putting double quotes around the SET statements will resolve the issue you are experiencing. You may also want to change your first file redirection to use > instead of >>, so that the batch file is overwritten each time instead of appended to. Here is the updated code:
#ECHO OFF
SET "v1old=4"
SET "v2old=0"
SET "v3old=7453"
SET "v1new=8"
SET "v2new=645"
SET "v3new=2"
ECHO #ECHO OFF ^& SETLOCAL>newupdate.bat
ECHO REM .>>newupdate.bat
ECHO REM version von dolphin>>newupdate.bat
ECHO SET "v1old=%v1new%">>newupdate.bat
ECHO SET "v2old=%v2new%">>newupdate.bat
ECHO SET" v3old=%v3new%">>newupdate.bat
PAUSE
EXIT
This is happening because v3new is 2. So you are directing STDERR to newupdate.bat. Easy way for you to see that is to temporarily comment out the #ECHO OFF and you will see that the line in question expands to
ECHO SET v3old= 2>>newupdate.bat
#Seth has good suggestions but you will still have the problem. Inserting a space between > and newupdate.bat in each case would solve the problem.
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.
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.