computing
  • 2

Solved Increment Counter For Batch File

  • 2

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.

Share

1 Answer

  1. 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

    • 0