Car981
Car981

Reputation: 261

Increase number in string in bat file?

How can I increment the value of an int in a string ?

Say I have foo-815-bar. I'd like to have foo-815-bar. foo and bar can be constants (although if bar can be an unknown variable, it would be preferable), and 815 is a variable. It's a 3 digit number that is to be incremented, so that foo-123-bar would return foo-124-bar.

Upvotes: 0

Views: 2035

Answers (2)

Kevin Matlock
Kevin Matlock

Reputation: 99

This should get you started... you might have to place this inside a loop depending on your needs.

@echo off
set myFoo=foo-
set myBar=-bar
set /a myIncrementor=123
echo %myFoo%%myIncrementor%%myBar%
set /a myIncrementor+=1
echo %myFoo%%myIncrementor%%myBar%

Upvotes: 0

Endoro
Endoro

Reputation: 37569

Example:

@echo off
set "string=foo-123-bar"
for /f "tokens=1-3 delims=-" %%i in ("%string%") do (
    set "pre=%%i"
    set /a number=%%j+1
    set "post=%%k"
)
set "string=%pre%-%number%-%post%"
echo %string%

.. output is:

foo-124-bar

Upvotes: 3

Related Questions