Announcement

Collapse
No announcement yet.

Batch: Add an incremental number to file names

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Batch: Add an incremental number to file names

    Moderator EDIT:
    "This post was splitted from the thread: Batch File to Rename Files as Date "


    Hi all,

    I have a similar requirement, only I also need to include the time, followed by an incremental number, i.e. "My_File_dd-mm-yyyy_hh-mm_incremental-num".

    I have tried:

    @echo off
    for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "Test.txt" Report_%%d-%%e-%%f_Now().txt

    Amongst other variations of this for the date and time, but have no idea about how to combine this with the incremental number. So far I have managed "My_File_dd-mm-yyyy.txt"

    Any ideas???

    Thanks,

    mrt2005

    *********EDIT***********

    I have now managed to rename my file "Report_dd-mm-yyyy_hh-mm.txt" with the following:

    @echo off
    for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "Test.txt"
    Report_%%d-%%e-%%f_%time:~0,2%-%time:~3,2%.txt

    Now all I need is the incremental number bit!

    Thanks.
    Last edited by Rems; 13th August 2008, 08:58. Reason: Update

  • #2
    Re: Batch File to Rename Files as Date

    I assume the incremental number is added to assure a unique file name.
    Would it then still be nesseray to add an 'incremental document count number' to each file name when... you could also add the Seconds and 10th-of-Seconds of the current time to the file name. ?

    Like this:
    Code:
    @echo off
    
    ::Devide the Current Date in dateparts, YYYY and MM and DD
    for /f "tokens=2-4 skip=1 delims=(-./)" %%i in ('echo.^|date') do (
       for /f "tokens=1-4 delims=-./ " %%m in ('date /t') do (
          (set dow=%%m)&(set %%i=%%n)&(set %%j=%%o)&(set YYYY=%%p)
       )
    )
    
    ::Devide the Current Time in timeparts, HH and MM and SSxx
    For /F "tokens=1-4 delims=:., " %%i in ('echo.%time%') Do (
       (Set HH=0%%i)&(set MM=%%j)&(set SSxx=%%k.%%l)
    )
    Set HH=%HH:~-2%
    
    Set "OutputFile=C:\ReportsFolder\My_File_@%YYYY%-%MM%-%DD%[email protected]%HH%,%MM%,%SSxx%.txt"
    
    echo.
    echo.
    echo.      File= %OutputFile%
    echo.
    
    echo.&echo.&pause
    Anyway,
    to retrieve the incremental document count number, you just have to count the 'right' documents in the 'right' folder and and raise that number with one.

    example
    Code:
    @echo off
    SetLocal enabledelayedexpansion
    
    Set "sFolder=C:\ReportsFolder"
    
    Set iCnt=0
    For /f "delims=" %%* in ('dir/b "%sFolder%\My_File_*.txt" 2^>NUL') do Set/a iCnt=!iCnt!+1
    
    echo.Number of present Documents= %iCnt%
    echo.
    
    :: new
    Set/a incr#=%iCnt%+1
    Set incr#=000000%incr#%&&Set "incr#=!incr#:~-4!"
    
    echo.
    echo.     new incremental Number= %incr#%
    echo.
    
    echo.&echo.&pause
    \Rems
    Last edited by Rems; 19th August 2008, 20:38. Reason: 2^>NUL

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment

    Working...
    X