cmd, write and read from txt

2019-07-26 05:29发布

问题:

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.

回答1:

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:

  1. 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;

  2. placing parentheses around the echo command:

    (echo %y%)>file.txt
    
  3. 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.



回答2:

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