Batch file needed to automatically rename files sequentially.

Posted on 2007-08-02
Last Modified: 2010-04-16
I am getting a log file from my server every 5 minutes for diagnostic purposes. The files are copied into a folder and then sent out via FTP.  My problem is that I need to rename the files in the 5 minutes that they are sitting in the folder - called "testfolder" - so that they are unique.  I would like to append the time of day to each file, or simply add a numerical sequence to them.  E.g., "testfile.csv" becomes "testfile08_03_2007_14_21_33.csv".  The "14_21_33" being 14 hours (2 PM), 21 minutes and 33 seconds.  Alternatively, the file could simply be renamed "testfile001.csv".  There are numereous utilites for renaming files manually, or for renaming multiple files, but what I need is a batch file that will do the job automatically 24/7.  I need to come in every morning and find a folder with files that I can distinguish from one another by sight.  If I'm out of the office overnight then I should find over 100 files in the morning that I can compare based on their names.  Thaniks for any help coding a solution for this.
Question by:tedsini
    LVL 58

    Accepted Solution

    @echo off

    for /f "tokens=1,2" %%u in ('date /t') do set d=%%v
    for /f "tokens=1" %%u in ('time /t') do set t=%%u
    if "%t:~1,1%"==":" set t=0%t%
    set timestr=%d:~6,4%%d:~0,2%%d:~3,2%%t:~0,2%%t:~3,2%

    rem echo %timestr%

    move C:\Whatever\testfile.csv C:\Temp\testfile_%timestr%.csv
    LVL 30

    Expert Comment

    I'd recommend getting the Windows Resource kit's sleep.exe for the timing part of this processing:

    @echo off


    set fileName=testfile
    set fileExt=.csv
    set sleepSeconds=60

    set fileSeq=0


    if not exist "%fileName%%fileExt%" goto SLEEP


    set /a fileSeq+=1

    if exist "%fileName%%fileSeq%%fileExt%" goto CHECK

    ren  "%fileName%%fileExt%"  "%fileName%%fileSeq%%fileExt%"


    sleep %sleepSeconds%

    goto AGAIN
    LVL 58

    Expert Comment

    It would be better to add the batch to FTP script/program so that the file is renamed as soon as FTP is done. If you don't do that, it would be difficult to synchronize the rename process with the regular FTP. Whatever you are using to run FTP regularly, it should be possible to run the batch (to rename) after that.
    LVL 33

    Expert Comment

    poor-man's sleep.bat:

    @echo off
    set sleepSeconds=%1
    if "%sleepSeconds%"=="" set sleepSeconds=5
    ping -n %sleepSeconds% localhost > nul

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now