Diego Felipe
Diego Felipe

Reputation: 391

get a number from txt add 1 and write it back in bat file

The titles says everything, I have a txt file called totalcred.txt with a number, all I want to do is:

  1. keep the number written on the .txt file in a variable
  2. add +195 to this variable
  3. replace the old number with the newer (the one who got +195)

But nothing on this earth worked until now, I got dissapointed.

Here's my code so for (I tried lot of things too):

FOR /F "tokens=1" %%a IN (totalcred.txt) DO (
    set val=%%a
    IF [%val%] GEQ [0] (
        set /a val += 195
        echo %val% > totalcred.txt
    )
)

Upvotes: 1

Views: 757

Answers (2)

Diego Felipe
Diego Felipe

Reputation: 391

FOR /F "tokens=1 delims=" %%a IN (totalcred.txt) DO (

set /a val=%%a+0


goto con

)

:con
set /a val2=%val%+100
echo %val2%

(Echo(%val2%)>totalcred.txt

Upvotes: 0

Compo
Compo

Reputation: 38589

You don't even need a for loop, which as you can imagine is problematic when trying to read from the same file you're writing to:

@Echo Off
Set/P "val="<totalcred.txt
If %val% GEq 30000 GoTo :EOF
Set/A "val+=195"
(Echo(%val%)>totalcred.txt

Upvotes: 1

Related Questions