?
Solved

DOS edit a file and increment number within it

Posted on 2009-02-18
5
Medium Priority
?
642 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
0
Comment
Question by:pauliep99
  • 2
  • 2
5 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 23671393
Is the label line the only one in the text file?
0
 

Author Comment

by:pauliep99
ID: 23671437
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
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23671845
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
 
LVL 21

Accepted Solution

by:
AmazingTech earned 2000 total points
ID: 23671863
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
 

Author Closing Comment

by:pauliep99
ID: 31548289
thanks that worked a treat
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question