We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

DOS edit a file and increment number within it

Medium Priority
651 Views
Last Modified: 2013-11-09
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
Comment
Watch Question

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
Is the label line the only one in the text file?

Author

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.
Top Expert 2009

Commented:
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

Top Expert 2009
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
thanks that worked a treat
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.