Monday, April 23, 2007

HowTo: Create a Timestamp in a bat file

I may be one of only two or three people I know that might still write a batch file to do something. Hopefully not, but it does seem to be a dying art. One of my collegues turned me on to this particular piece of code that will create a timestamp for a file. I didn't know how to do that and this would be one way to do it.

Just look at it in all its ugly syntax, this is Old School, pocket protector wearer stuff:

for /F "tokens=1-4 delims=/ " %%i IN ('date /t') DO (
set DT_DAY=%%i
set DT_MM=%%j
set DT_DD=%%k
SET DT_YYYY=%%l)

doSomething.exe > "%DT_YYYY%_%DT_MM%_%DT_DD%.txt"

UGLY. Where in the hell does the j,k, & l come from? Some sort of crazy auto-magicness? This is from the times when it was best not to question how it works. I still don't know but at least I got this trick card in the deck now.

Labels:

4 Comments:

Blogger shells said...

hey, u can no longer claim to be the only person in the world that still writes batch files.
Just a month ago i wrote one...i can evens end it to you if you want proof. anyways, DBAs write them often still...no sure when they will ever be phased out, sometimes it's the only way to get things done :-)

11:56 AM  
Blogger Swamproot said...

I actually got this from a db backup script. Also, I merely claimed to be one of the few persons I KNEW that still wrote them. But that includes a whole lot of luddites in my immediate cirlce. :-)

It does seem to be a dying art tho'.

12:31 PM  
Blogger Techie Mom said...

Use this code : I wrote it myself. It captures the timestamp in a variable "T" and appends the variable T to the file name "Temp_File". Feel free to use it if you need it.

set timest=%time:~0,8%
set timest1=%time:~0,8%
PAUSE
if @%timest:~0,1% == @ set timest=0^%timest:~1,7%
set T=%DATE:~4,2%%DATE:~7,2%%DATE:~10,4%^_^%timest:~0,2%^%timest:~3,2%^%timest:~6,2%
move D:\TEMP_File.csv C:\TEMP_File%T%.csv
pause
if %timest1:~0,1%==1 @set T=%DATE:~4,2%%DATE:~7,2%%DATE:~10,4%^_^%timest1:~0,2%^%timest1:~3,2%^%timest1:~6,2%

Regards,
Shweta

9:49 PM  
Anonymous Anonymous said...

Wicked cool + ol'skool. This was exactly what I needed.

11:45 AM  

Post a Comment

Links to this post:

Create a Link

<< Home