-3

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?

didjek
  • 393
  • 5
  • 16
  • 5
    so many duplicates: [Calculating the sum of two variables in a batch script](http://stackoverflow.com/q/10674974/995714), [Math on batch](http://stackoverflow.com/q/1869155/995714)... – phuclv Nov 14 '16 at 16:52
  • 1
    You understand that variable values don't reside in the batch but in the currently active environment. IF you do set fix values you are overwriting the current value. You might look at `help set` especially Set /A += –  Nov 14 '16 at 17:39
  • or google "how to do arithmetic in batch" – phuclv Nov 15 '16 at 03:22

1 Answers1

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%
Stephan
  • 53,940
  • 10
  • 58
  • 91