DOS edit a file and increment number within it

I have a txt file which has a a number value in it which I will use to label some code.  I want to be able to open the file and increment the label by one so therefore 9.9.9.0001 to be incremented to 9.9.9.0002

label=9.9.9.0001
pauliep99Asked:
Who is Participating?
 
AmazingTechCommented:
Instead of

Copy /y nul label.tmp

we could just delete it if it exist

if exist label.tmp del label.tmp
SETLOCAL ENABLEDELAYEDEXPANSION
Set Filename=label.txt
 
if exist label.tmp del label.tmp
for /f "tokens=* delims=" %%a in ('type "%Filename%"') do (
    echo %%a | Find /i "label=9.9.9."
    IF NOT ERRORLEVEL 1 (
        Set Line=%%a
        Set /a Num=1!Line:~-4!+1
        echo !Line:~0,-4!!Num:~-4!>>label.tmp
    ) ELSE (
        echo %%a>>Label.tmp
    )
)
Move Label.tmp "%filename%"

Open in new window

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Is the label line the only one in the text file?
0
 
pauliep99Author Commented:
NO its full of other text:
###############################################################################
#
#   VERSION NUMBERS for CONTENT PLATFORM
#
#   Edit this file before doing a release.
#   When finished changes, simply save and close to resume build process
#
#   If you are doing an official release, this file will be checked in
#   with the following comment:
#
      version-scc-comment=test
#
###############################################################################
#
    version-title=test
    label=9.9.9.0001
    version-portal=1.60
#
###############################################################################

so i want to eidt this fiule and increment the label=9.9.9.0001 by one each time I execute a bat file.
0
 
AmazingTechCommented:
Try this.

Hopefully your file does not have any Special DOS characters.
SETLOCAL ENABLEDELAYEDEXPANSION
Set Filename=label.txt
 
copy /y nul label.tmp
for /f "tokens=* delims=" %%a in ('type "%Filename%"') do (
    echo %%a | Find /i "label=9.9.9."
    IF NOT ERRORLEVEL 1 (
        Set Line=%%a
        Set /a Num=1!Line:~-4!+1
        echo !Line:~0,-4!!Num:~-4!>>label.tmp
    ) ELSE (
        echo %%a>>Label.tmp
    )
)
Move Label.tmp "%filename%"

Open in new window

0
 
pauliep99Author Commented:
thanks that worked a treat
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.