Home > other >  Batch File - How to copy text from file, and then update it to a new value
Batch File - How to copy text from file, and then update it to a new value

Time:06-03

Each time I open the batch file, I would like it to read the information currently stored in the text file, and then apply that stored information it pulled to calculating a new integer.

I'm trying to figure out how to get a number copied from a text file, stored as a variable, and then updated to a new integer in that text file, say adding 1 to the value. I've been sifting through information online, and everything seems to point in a different direction.

Here is a test code I've gotten from digging thus-far:

set file="Test.txt"
set /a _Counter =< %file%
echo:%_Counter%
set /a "_Update=%_Counter% 1"
echo:%_Update% >%file%
timeout /t 10

For some reason when I try to get the information for the counter, it doesn't pull any data from the text file, and I'm left with this line output by the batch file.

F:\Users\Test\Documents\JumbledDirectory> set /a _Counter = Directory\Test.txt 0<F:\Users\Test\Documents\Jumbled

The most common answer I've seen is to use something along the lines of

set /p _Counter=< Test.txt  
echo %_Counter%

As seen here: Windows batch command(s) to read first line from text file

But upon doing this I've either ended up with

echo:%_Counter%

being completely blank, or it defaults to 0 each time.

Any help would be appreciated as I've sadly been trying to find how to get this simple function for around 6 hours now.

CodePudding user response:

So having the batch in the same directory as the textfile this will work:

REM get input of file as Counter
set /p Counter=<number.txt 

REM add a number to Counter and assign it as Counter
set /a "Counter=%Counter% 3

REM empty the file
break>number.txt

REM write Counter in the file
echo %Counter% >> number.txt

CodePudding user response:

@ECHO Off
SETLOCAL
set "file=q72474185.txt"
set /p _Counter=< "%file%"
echo:%_Counter%
set /a _Update=_Counter 1

echo:%_Update% >"%file%"
TYPE "%file%"
GOTO :EOF

When you use the point-click-and-giggle method of executing a batch, the batch window will close if a syntax-error is found or the script runs to completion. You can put a pause after statements and home in on the error, but better to open a 'command prompt' and run your batch from there so that the window remains open and any (error) messages will be displayed.

The error message would be about missing operand.

I've changed the filename as I track each question I respond to with its own set of data.

Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal \, Space or " - build pathnames from the elements - counterintuitively, it is likely to make the process easier.

set /a does not expect input from anywhere, it simply performs the calculation and assigns the result. Quotes are not necessary so I've removed them. % are also not required in a set /a but can be required if you are using delayedexpansion.

set /p expects input, so I've used that to read the file. Note that set /a disregards spaces, but set and set /p will include the space before the = in the variablename assigned, and _Counter & _Counter are different variables.

  • Related