how to increment numbers in a batch file

Microsoft Windows 7 professional - upgra...
July 13, 2010 at 10:44:46
Specs: Windows 7
I am trying to create a batch file that will increment a build number.
For instance, the batch file will read
"Version 1.2.xxx:"
(xxx will be the current build number)
After the batch file runs, it should increment the last 3 digits.
Example:
If the batch file reads:
Version 1.2.111
After it runs, it should read:
Version 1.2.112
I can create a batch file that will search for an specific string but after it runs the first time I have to change the script again to math the string.

I appreciate any help.



See More: how to increment numbers in a batch file

Report •


#1
July 13, 2010 at 15:11:23
Will this work? It's as simple as it gets...

@ECHO OFF

SET /p CurrentBuild=<CurrentBuild.txt
SET /a NewBuild=%CurrentBuild%+1
ECHO Build 123.456.%NewBuild%
ECHO %NewBuild%>CurrentBuild.txt
PAUSE

It requires a text document with the build you are currently at in it.

If your current build is 111, write that in a text document and save it as Current Build.txt

EDIT:

Here, I've improved it for you. Now it will run a one time set up, and make the file hidden and read only. Simple yet again, Let me know if it fits your needs.
@ECHO OFF

IF EXIST CurrentBuild.txt GOTO Start
ECHO What is the current build?
SET /p Build=""
ECHO %Build%>CurrentBuild.txt
ATTRIB +R +H CurrentBuild.txt

:Start

CLS
SET /p CurrentBuild=<CurrentBuild.txt
SET /a NewBuild=%CurrentBuild%+1
ECHO Build 123.456.%NewBuild%
ATTRIB -R -H CurrentBuild.txt
ECHO %NewBuild%>CurrentBuild.txt
ATTRIB +R +H CurrentBuild.txt
PAUSE
EXIT


Report •

#2
July 13, 2010 at 16:51:30
use a data/file processing tool for the job. eg you can download gawk for windows (see my sig). Then use this one liner

C:\test>more file
this is a line
Version 1.2.111
this is last line

C:\test>gawk -F"." "/Version/{$NF+=1;}1" OFS="."  file
this is a line
Version 1.2.112
this is last line


the above assumes you have the version on one line.

GNU win32 packages | Gawk


Report •

#3
July 13, 2010 at 18:29:59
Ehh, I'm not an expert, and don't claim to be one. Just thought I'd help as I'm learning too.

Report •

Related Solutions

#4
July 14, 2010 at 06:29:40
Thank you Matt123. I created a .bat file with the code you sent me, I created a .txt file ("CurrentBuild.txt") But when I run the batch file, everythying in the .txt file gets deleted and If I run it again it says "Access Denied"
How can I write to the file?

I appreciate your help.


Report •

#5
July 14, 2010 at 12:11:27
Have you already got 'Version 1.2.111' assigned to
a variable? If so, you'll need to break it up before
incrementing [see code below]. If not, where might it
be found in the file? Is it the only thing in there? Is it
on a line of its own or is it mixed in with other text?

@echo off

setlocal EnableDelayedExpansion

set version=Version 1.2.111

for /f "tokens=1-3 delims=." %%a in ("%version%") do (
  set /a build=%%c+1
  set version=%%a.%%b.!build!
)
echo %version%



Report •

#6
July 14, 2010 at 12:54:15
regarding matt's script, this seems really wierd. His syntax should work, and i swear it used to work, but when i ran the script, it fails unless it is changed to:
>currentbuild.txt echo %build%
and:
>currentbuild.txt echo %newbuild%
then it worked!
try making those changes. not sure where the "access denied" is coming from, didn't get that error on my system.

Report •

#7
July 14, 2010 at 13:08:30
Hi Guys,


Thank you for your help. This is what I have so far and it works.

set str=version 1.0.111
FOR /F "tokens=1-5* delims=." %%d in ("%str%") do set hold=%%f
set /a hold+=1
echo %hold%
echo.version 1.0.%hold%

The problem is that I want to get the string "Version 1.0.*"
from a txt file so I can assign it to the str variable.
I have the following:

findstr /l "version 1.2.*" text.txt

But I do now know how to store this result into a variable so I can use the for loop and parse it.

Any help would be appreciated it.

Thank you,


Report •

#8
July 14, 2010 at 16:23:58
About my script - Is your computer a little slow? It might be trying to write to the file before it makes itself readable. Add:

PING 1.0.0.0 -n 10 -w 1000 >Nul

Into it, before and after:

ECHO %NewBuild%>CurrentBuild.txt

----

Now, I'm not quite sure what you mean here with your second problem.

Would saving it as "Verison 1.0.%CurrentBuild%" work?

*Obviously change "%CurrentBuild%" to what you used in your batch file.

I'm out of answers now, sorry. I've not used the findstr command before, so... I might aswell go learn hehe.

EDIT: Nahh, it doesn't work, I tested it out, I'll see if I can find anything out for you though,


Report •

#9
July 14, 2010 at 17:12:29
as for findstr, i don't think
findstr /l "version 1.2.*" text.txt

will work correctly (see: findstr /?)
(it will give false postives if a line just has "version")
i've learned the hard way, when spaces are involved, you need:
findstr /i /L /c:"version 1.2."
or
findstr /i /r /c:"version 1\.2\.."
(if you want to make sure that there has to be content after the "2.")
to capture:
for /f "tokens=*" %%a in ('findstr /i /r /c:"version 1\.2\.." file.txt') do set wholestr=%%a
will capture the entire line (i'm not sure what the line content looks like).
you might be able, depending, to narrow it down using tokens clause. If you post a sample content, and what exactly you want out of it, i or someone here can tailor it to your needs.


Report •

#10
July 15, 2010 at 06:17:01
Here is the content of the txt file:

<CatapultVariableSet>
<Variable
Comment=""
Name="Build">Version 1.0.111</Variable>
</CatapultVariableSet>

I need to update "Version 1.0.111" every time I run the batch file.
So after running one time, it will look like "Version 1.0.112" and
so on.
I do not know if findsrt is a good idea, I have no experience in batch files.

Thank you for your help so far.


Report •

#11
July 15, 2010 at 06:38:22
c:\> more file
<CatapultVariableSet>
<Variable
Comment=""
Name="Build">Version 1.0.111</Variable>
</CatapultVariableSet>

c:\> gawk -F"." -vRS="</Variable>" "/Version/{$NF+=1}{print $0RT}" OFS="." ORS=""  file
<CatapultVariableSet>
<Variable
Comment=""
Name="Build">Version 1.0.112</Variable>
</CatapultVariableSet>

GNU win32 packages | Gawk


Report •

#12
July 15, 2010 at 07:28:50
Hi ghostdog,


I do not know how to use the code you sent me. I donwload Gawk but I do not know where to go from there.


Help!


Report •

#13
July 15, 2010 at 07:55:49
Here's the batch version. Change:

set filename=somefile

to equal your actual file name and if this is part of some
bigger script, make sure

setlocal EnableDelayedExpansion

is near the top.

[Code Edit: don't know what I was
typing nul into the backup file for...]


@echo off

setlocal EnableDelayedExpansion

set filename=somefile

copy "%filename%" "%filename%.bak"
type nul> TmpFile
for /f "usebackq delims=" %%a in ("%filename%") do (
  echo "%%a"|findstr /i /r /c:"version [0-9]"> nul
  if !errorlevel! neq 1 (
    for /f "tokens=1-4 delims=<." %%b in ("%%a") do (
      set /a build=%%d+1
      echo %%b.%%c.!build!^<%%e>> TmpFile
    )
  ) else (
    echo.%%a>> TmpFile
  )
)
move TmpFile "%filename%"


Report •

#14
July 15, 2010 at 08:16:17
@lostinbatch, if you installed it properly, you should have gawk.exe. Its just an executable, much like findstr.exe , find.exe , ping.exe etc. You can put it in your system32, or specify the full path to where your gawk.exe resides when executing it.

GNU win32 packages | Gawk


Report •

#15
July 15, 2010 at 08:24:19
@markLS, nice effort on the batch, but here's a few things i found
1) slowness of execution.
2) i don't know version number format OP has, but if it's something like 1.0.1.119 for example, your batch will break. Of course, i am hoping OP doesn't have such a format.

GNU win32 packages | Gawk


Report •

#16
July 15, 2010 at 09:32:09
markLS,


thank you for your batch. I ran your code, it calls the file somefile.txt. After I run it, I get an empty TmpFile.bak file.
I do not know what I am doing wrong.

Once again thank you for your help,



Report •

#17
July 15, 2010 at 12:23:19
lostinbatch, there's instructions at the top of my post
[before the code] for trouble-free operation - please
read them ;)

ghostdog, thanks! I'm not sure about the speed thing -
I wrote and tested it on a Win2k machine and there's little
delay but I know that vista executes batches a lot slower
than Win2k. (Don't know about about XP or Win7.)
As for versioning, I thought OP was clear about that -
hope so anyway :)



Report •

#18
July 15, 2010 at 14:21:27
MarkLS,

I read the steps but still does not work.
Create a batch file with your work.(change set filename=test.txt)
Create a test.txt file with the text: version.1.0.111
run the batch file.
Result:
TmpFile.bak
empty and test.txt file unchanged.

Am I following the steps correctly?

Thank you for your help,



Report •

#19
July 15, 2010 at 16:08:56
lostinbatch, In response #10 you listed the contents
of your file. My script is tailored to work on that specific
data so for testing purposes you will need to use a copy
of your actual file to get the correct result.

If it's not flexible enough for whatever reason, maybe
take a look at the gawk offering; gawk's good but it's
a while since I looked at it so I don't know if what's been
offered is bulletproof.



Report •

#20
July 15, 2010 at 16:43:25
>> gawk's good but it's
>> a while since I looked at it so I don't know if what's been
>> offered is bulletproof.


Tests with different version number.

C:\test>more file
<CatapultVariableSet>
<Variable
Comment=""
Name="Build"
Tag=""> Version 1.1.0.119 </Variable>
</CatapultVariableSet>

C:\test>gawk -F"." -vRS="</Variable>" "/Version/{$NF+=1}{print $0RT}" OFS="." ORS=""  file
<CatapultVariableSet>
<Variable
Comment=""
Name="Build"
Tag=""> Version 1.1.0.120</Variable>
</CatapultVariableSet>

C:\test>more file
<CatapultVariableSet>
<Variable
Comment=""
Name="Build"
Tag=""> Version 1.1.0.2.3.4.199 </Variable>
</CatapultVariableSet>

C:\test>gawk -F"." -vRS="</Variable>" "/Version/{$NF+=1}{print $0RT}" OFS="." ORS=""  file
<CatapultVariableSet>
<Variable
Comment=""
Name="Build"
Tag=""> Version 1.1.0.2.3.4.200</Variable>
</CatapultVariableSet>

GNU win32 packages | Gawk


Report •

#21
July 15, 2010 at 17:44:04
Hi LostInBatch

This help

@echo off
cls
setlocal EnableDelayedExpansion
type nul > Temp.txt
for /f "tokens=1,* delims= " %%a in (Version.txt) do (
if %%a EQU Version (
set Ver=%%b
for /f "tokens=1-3 delims=." %%x in ("!Ver!") do (
set /a Num=%%z+1
echo %%a %%x.%%y.!Num! >> Temp.txt
)
) else (echo %%a %%b >> Temp.txt)
)
move Temp.txt Version.txt > nul
type Version.txt


Report •

#22
July 15, 2010 at 18:18:24
@dtech, i ran your batch, but the number doesn't increase.

C:\test>more test.bat
@echo off
setlocal EnableDelayedExpansion
for /f "tokens=1,* delims= " %%a in (file) do (
if %%a EQU Version (
set Ver=%%b
for /f "tokens=1-3 delims=." %%x in ("!Ver!") do (
set /a Num=%%z+1
echo %%a %%x.%%y.!Num!
)
) else (echo %%a %%b )
)

C:\test>more file
<CatapultVariableSet>
<Variable
Comment=""
Name="Build"
Tag=""> Version 1.0.111 </Variable>
</CatapultVariableSet>

C:\test>test.bat
<CatapultVariableSet>
<Variable
Comment=""
Name="Build"
Tag=""> Version 1.0.111 </Variable>
</CatapultVariableSet>

GNU win32 packages | Gawk


Report •

#23
July 16, 2010 at 04:30:23
ghostdog, I wasn't thinking so much about version format, but
more about how well it copes with data (in general) that differs
from what is provided; many people seem to post questions and
supply data that isn't a very good representation of their actual
data...



Report •

#24
July 16, 2010 at 05:13:18
when designing a piece of code, its always good to view things in different angles, like what possible scenarios would break your code in the future, etc etc. This way, your code is resilient to sudden changes. While OP doesn't provide enough data to illustrate possible version number, why not make the code always increment the last number, regardless what the version maybe, so even a version format such as 1.1.1.1.1.1.1.1.1.199 would still become 1.1.1.1.1.1.1.1.1.200 and not something else.

GNU win32 packages | Gawk


Report •

#25
July 16, 2010 at 05:36:39
Yep, I'm painfully aware of that, but you've got to draw a
line somewhere. Take your gawk lines, for example: if you
feed it a line like 'aaa Version 1.2.111 bbb', you don't get
the whole line back.



Report •

#26
July 16, 2010 at 06:21:17
the format of the whole file is more predictable than a version number. The version number appears in the "</variable>" tag. If that's the structure every time that file is generated eg XML, then its not a problem. BUT version number changes every so often. of course, if "aaa" and "bbb" are always there everytime, then my gawk command will include them as well.

GNU win32 packages | Gawk


Report •

#27
July 16, 2010 at 07:08:29
BUT version number changes every so often.

It's just my opinion, but if that's the case then it's up to
the OP to make that clear. No-one's getting paid here
and I don't think anyone should be expected to spend
hours googling around researching what some piece
of data might be referring to.



Report •


Ask Question