[Last Call] Learn how to a build a cloud-first strategyRegister Now


Batch file needed to automatically rename files sequentially.

Posted on 2007-08-02
Medium Priority
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
  • 2
LVL 58

Accepted Solution

amit_g earned 2000 total points
ID: 19620029
@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

ID: 19620040
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

ID: 19620091
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

ID: 19620618
poor-man's sleep.bat:

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

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

829 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