Solved Increment Counter for batch file

June 23, 2013 at 23:28:03
Specs: Windows 7
I have a txt file on my desktop called counter.txt with value 001 in it, i want a batch file that would increase the counter value by 1 each time the .bat file is run.

I have no idea about writing codes for batch file as I come from a different background,need this for a customer requirement.

Any help would be appreciated.

Thanks.


See More: Increment Counter for batch file

Report •


✔ Best Answer
June 24, 2013 at 11:50:10
Hi M2: those ever-receding goal-posts do tend to make scoring impossible. I did notice one thing that might need attention, which is using set /a on the item with leading zeros, (which treats as octal). Your input would need them stripped:


@echo off & setlocal
pushd "C:\Documents and Settings\NUI\Desktop"
if not exist counter.txt > counter.txt echo nn/nn/nnnn=1
for /f "tokens=1,2 delims==" %%a in (counter.txt) do (
set dt=%%a
set c= %%b
)
::you can change the following date delimiter.
set dlm=/
set d8=%date:~7,2%%dlm%%date:~4,2%%dlm%%date:~10%
set c=%c: 00=%
set c=%c: 0=%

if %dt% equ %d8% (
set /a c+=1
) else (
set dt=%d8%
set c=1
)
if %c% lss 10 set c=0%c%
if %c% lss 100 set c=0%c%
>counter.txt echo %dt%=%c%
popd



#1
June 24, 2013 at 00:46:27
::
::
::
::
::====== script starts here ===============
::
:: inc.bat 2013-06-24 14:38:51.50
@echo off & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in (count.txt) do (
set x=%%a
)
set/a X+=1
if !x! lss 100 set N=0!x!
if !x! lss 10 set N=00!x!
> count.txt echo.!N!
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#2
June 24, 2013 at 01:11:35
Hi Mechanix2go,

Thanks a tonne for your help, i have used your solution and its working fine, however can u also tell me where to change the path of the count.txt file in the batch file, as the .bat file and .txt file may not necessarily lie on the same folder path.

Also I need to reset the counter to 001 everyday, for example if the current value in count.txt is 079, on 25.06.2013 the first file should have 001 as counter.

Thanks again for your prompt response.


Report •

#3
June 24, 2013 at 01:30:08
::====== script starts here ===============
::
:: inc.bat 2013-06-24 14:38:51.50
@echo off & setLocal enableDELAYedeXpansioN

pushd "C:\Documents and Settings\NUI\Desktop"

set /p R= time to reset ? [press ENTER for no reset]
if defined R (
> count.txt echo 001
goto :eof
)

:main
for /f "tokens=* delims= " %%a in (count.txt) do (
set x=%%a
)
set/a X+=1
if !x! lss 100 set N=0!x!
if !x! lss 10 set N=00!x!
> count.txt echo.!N!
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

Related Solutions

#4
June 24, 2013 at 01:42:26
Hi Mechanix2Go,

I will be calling this Batch file from external report, so I want it to check in background for the date and update the same, if it is current date then it need not change the counter, there will be no user interaction as this will be called externally. Is it possible to check date and reset to 001 in background?

Thank you for your help.


Report •

#5
June 24, 2013 at 01:57:15
Do this and post output:

echo %DATE%

and post the output of this script:

::====== script starts here ===============
:: get D T
:: gt.bat 2013-06-24 15:51:18.67
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

for /f "tokens=* delims= " %%i in ('dir/b NEWFILE') do (
echo %%i %%~Ti
)
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#6
June 24, 2013 at 02:28:26
Hi Mechanix2Go,

Rite now your first solution is feasible to me, so I would proceed with that, but there is also another problem, my count.txt has value '24/06/2013=006' and i want the date to stay as it is, the .bat file should increase only the counter, so that my output is '26/06/2013=007'.

Please help with the script for the above as the first script is removing the date and only giving counter.

Thanks once again.


Report •

#7
June 24, 2013 at 05:05:11
can someone please help me with above requirement?

Report •

#8
June 24, 2013 at 08:05:44
I've redone this a few times.

When you settle on your exact requirement, let us know.

=====================
M2 Golden-Triangle


Report •

#9
June 24, 2013 at 11:50:10
✔ Best Answer
Hi M2: those ever-receding goal-posts do tend to make scoring impossible. I did notice one thing that might need attention, which is using set /a on the item with leading zeros, (which treats as octal). Your input would need them stripped:


@echo off & setlocal
pushd "C:\Documents and Settings\NUI\Desktop"
if not exist counter.txt > counter.txt echo nn/nn/nnnn=1
for /f "tokens=1,2 delims==" %%a in (counter.txt) do (
set dt=%%a
set c= %%b
)
::you can change the following date delimiter.
set dlm=/
set d8=%date:~7,2%%dlm%%date:~4,2%%dlm%%date:~10%
set c=%c: 00=%
set c=%c: 0=%

if %dt% equ %d8% (
set /a c+=1
) else (
set dt=%d8%
set c=1
)
if %c% lss 10 set c=0%c%
if %c% lss 100 set c=0%c%
>counter.txt echo %dt%=%c%
popd


Report •

#10
June 24, 2013 at 14:37:25
Hi nbrane,

I'm out of the business of nagging for info.

=============================
Yeh, I expected to need to strip ;eading zeros, but this works:

C:\TEMP>set Z=001

C:\TEMP>set/a z+=1
2
C:\TEMP>

=====================
M2 Golden-Triangle


Report •

#11
June 24, 2013 at 18:30:14
Ha ha! I think a Murphy-law might state: "the complexity of the final result of a query is inversely proportional to the simplicity of the initial one."

Whoever decided that leading zero is the flag for octal needs to have their brain rewired and have to deal with this mess. Why not "#", or "@", like hex 0xnn, but using 0#nn (f/e). Oh well, that said, have you tested with z=008? On mine it goes to 1.


Report •

#12
June 24, 2013 at 21:39:31
Hi Nbrane,

Your script works like a charm :) thanks a lot! this is exactly what i was looking for.

Thank u soooooo much! :)


Report •

#13
June 24, 2013 at 21:41:29
Thanks Mechanix2go for taking time out to help with my query.

Report •

#14
June 25, 2013 at 01:11:02
Hi nbrane,

No, it does not work with 8.
Here's a bat you can mess with. It sumps 08 etc but doesn't say it's doing so.

BTW, nice job using delim== and c= %%b

::====== script starts here ===============
::
:: nooct.bat 2013-06-25 13:24:56.01
@echo off & setLocal enableDELAYedeXpansioN

set/p z=
for %%a in (b b b b b b b b b b b b) do (
set/a z+=1
echo.!z!
)
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#15
June 25, 2013 at 05:03:37
Hi Nbrane,

When i run the batch file on windows 2003, date in cmd gives me output as Tue 06/25/2013, whereas i need 25/06/2013 in my text file along wth counter, can you please help me with this.

Also when you help me with the code, request you to paste the entire code(date+counter) as i do not know where to put the date specific code.

Thanks.


Report •

#16
June 25, 2013 at 07:51:26
That's why I asked for date layout about 10 replies ago.

=====================
M2 Golden-Triangle


Report •

#17
June 25, 2013 at 11:42:10
@M2: exactly. At least now we know the date-format, Ha ha!

@OP: I'll modify response #9 to reformat the date. I thought the date was just to reset the counter, not for reporting. Your customer's date format would have to be exactly the same for this fix to work, so be sure to check that and let me know if it's different. vbscript or powershell handle date-formats efficiently, batch does not.

ps: this "chews" all leading zeros off, regardless of length:
:zz
set cc=%c%
set c=%c: 0= %
if %c% neq %cc% goto :zz


Report •

#18
June 25, 2013 at 22:21:25
Thanks a tonneeeeeeee for your help,Nbrane, really really appreciate it :)

@Mechanix2go: i dint really understand what you wanted from me when you gave that script some 12 msgs ago, now i get what you wanted :D thanks anyway for ur help too!


Report •


Ask Question