1.

Solve : Using a BAT program to rename a file with date and time?

Answer»

I found article CH000987 about using a BAT program to rename a file with the date or the time. Is there a way to combine the two so that the new file name contains both the date and the time? I'm trying to do this on a server machine running Windows 2000 Server. Thanks for any help provided.What date and time format are you using in your locale? In other words, do you use NORTH American format, or "rest of the world" format?

the results of

Code: [Select]echo %date%
and

Code: [Select]echo %time%
would be useful.




I'm in the US.

echo %date% YIELDED 05/21/2008

echo %time% yielded 13:06:51.86 at the time I ran the command.

ThanksGreat. Thanks. Now... what did you want to do exactly? rename all the files in a folder, or just some of them, with the original name plus the date and time before or after, or are you happy ARRANGING all that yourself and just need pointers towards how to extract the relevant date & time data into variables?

Well, here's what's going on. At a scheduled time every weekday, a secure FTP client software package that we're running on our server computer FTP's a file called INVOICES to a third party company. But, I want to keep archive copies of all the daily INVOICES files. The FTP client software gives me the option of running a post-transfer command so I want to set up and run a BAT program that will move the INVOICES file to an archive folder and rename it. I'd like the renamed file to have a format such as INV_20080521_2000 (if the file was renamed at 8:00 PM today, for example).

I've done something similar to this on a Win 98 machine several years ago but my BAT programming skills are a bit rusty. Plus, like I said, this is a Windows 2000 Server machine.

Thanks.Something like this ought to do it

Code: [Select]@echo off
set thisfolder=%cd%
set mm=%date:~0,2%
set dd=%date:~3,2%
set yr=%date:~6,4%
set hr=%time:~0,2%
set mn=%time:~3,2%
set newname=INV_%yr%%mm%%dd%_%hr%%mn%
REM folder where INVOICES is intially
set folder1=d:\folder\where\INVOICES\file\is\located\
REM folder where renamed file is to go
set folder2=d:\folder\where\renamed\file\is\to\go\
cd /d "%folder1%"
ren INVOICES %newname%
move %newname% "%folder2%"
cd /d %thisfolder%
I'm very close but I'm running into a slight problem with the hr variable. If I run the BAT program before 12 noon the hr variable gets set to " #" with a leading blank. For example, when I ran it just now hr was set to " 7" because it's 7:56 AM here. This causes a problem because the newname variable ends up with an embedded blank which cause the ren statement to fail.

Is there a way to NOT suppress the leading 0 in the hour portion of the time in the set hr command? Or is there a way to replace a blank in hr with a 0 after the value is set?

Thanks again!This may have more to do with your regional settings, but I've always had success with using the date/time command rather than the variable:

Code: [Select]for /f "tokens=1-4 delims=/ " %%i in ('date /t') do (
set yy=%%L
set mm=%%j
set dd=%%k
)
for /f "tokens=1-3 delims=: " %%i in ('time /t') do (
set hh=%%i
set mn=%%j
)

Good luck.

That worked in test when I ran it manually. I've entered the command into my FTP client software to execute the BAT program after the FTP tonight.

Keep your fingers crossed! I'll let you know how it works.

Thanks!Quote

If I run the BAT program before 12 noon the hr variable gets set to " #"

I think you mean 10 am?

Anyway apologies for the omission

Code: [Select]@echo off

REM remember current folder
set thisfolder=%cd%

set mm=%date:~0,2%
set dd=%date:~3,2%
set yr=%date:~6,4%
set hr=%time:~0,2%
set mn=%time:~3,2%

REM if 1ST character of hour is a space
REM replace with a zero
if "%hr:~0,1%"==" " set hr=0%hr:~1,1%

set newname=INV_%yr%%mm%%dd%_%hr%%mn%

REM folder where INVOICES is intially
set folder1=d:\folder\where\INVOICES\file\is\located\

REM folder where renamed file is to go
set folder2=d:\folder\where\renamed\file\is\to\go\

cd /d "%folder1%"
ren INVOICES %newname%
move %newname% "%folder2%"

REM return to
cd /d "%thisfolder%"Yes, you're correct, I did mean 10 AM - not 12 noon. I thought about that afterwards. :-o

By the way, on that server the day name is actually the first thing returned from the date so I had to tweak those set statements a little bit as follows:

set mm=%date:~4,2%
set dd=%date:~7,2%
set yy=%date:~10,4%

Thanks again.


Discussion

No Comment Found