'How to modify a numeric variable value with Windows batch [duplicate]

I have a script file script_A.cmd containing a lot of commands, including the following:

set NUMBER_RUN=1

This script calls another script called stript_B.cmd. During the run of script_B.cmd, I want to update the script_A.cmd and increment the value of the NUMBER_RUN value by 1. In other words, after the first run, it should change that text in script_A.cmd to

set NUMBER_RUN=2

and so on for subsequent runs. So this requires both batch arithmetic and some kind of search/replace to change the actual text in script_A.cmd accordingly.

How do I do that, without using any tools downloaded from the internet, just Windows native batch?



Solution 1:[1]

Automatic change of code is a bad idea. Better use a file to store values, like:

script_B.cmd (reading the number from the file, incrementing it and writing it back)

<count.txt set /p Number_Run=
set /a Number_Run +=1
>count.txt echo %Number_Run%

First line reads the counter from a file, second line increases it by one, and the third line rewrites it to the file again.

script_A.cmd (just read the counter from the file)

<count.txt set /p Number_Run=
echo %Number_Run%

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1