Timestamp batch file
@echo off
echo -
echo NOTE: THIS METHOD *ONLY* WORKS ON WINDOWS XP/2000!
echo -
echo This sample demonstrates parsing batch file
echo environment variables %%date%% and %%time%%.
echo -
echo %%date%% = %date%
echo %%time%% = %time%
echo -
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
for /f "tokens=1-4 delims=.: " %%i in ("%time%") do (
set hour=%%i
set minute=%%j
set second=%%k
set hundredth=%%l
)
if %hour% LSS 10 set hour=0%hour%
echo NOTE: Due to execution time, the following time
echo may vary a little from the previous.
echo -
echo Sample usages:
echo -
echo Today is %dow%
echo %year%-%month%-%day% %time%
echo %year%%month%%day%T%hour%%minute%%second%.%hundredth%
echo File_%year%-%month%-%day%_%hour%h_%minute%m_%second%s.txt
echo -
pause
John’s Rambles » Blog Archive » Batch file timestamp creation said
[...] I've been looking for a good way to generate timestamps in batch files. I created this sample based on a comment posted about an article appearing in Windows IT Pro magazine. [...]
Tafadzwa said
Batch file to rename a file, by appending a previous day’s date.
Andrew English said
When I run this under Windows 2003 Server x64, the day does not appear, but the rest of it does do you have any solutions to fix the day? Thanks
Anthony Hoang said
This is an excellent solution. It worked perfect for your suggestion.
Thanks a lot.
Anthony.
Graeme Locke said
John .. This is a wonderful little batch file, thanks for the code.
I changed the order around to accept my regional modifications but the batch file worked flawlessly.
Andrew .. The Date variable is picked up from the “short date” set in your regional settings, if you want the Day of Week to appear change the short date something like dddd yyyy-mm-dd (ddd = Mon, Tue, etc ; dddd = Monday, Tuesday, etc).
Dan said
.log works better and it works on all OS.
@echo off
echo the current time is:
.log
jgiors said
[Upfront apologies to any visitors who have to read this semi-flaming reply, but I feel it is preferable to deleting "Dan's" post. -- JohnG]
Dan,
When you post to someone’s blog without using your real credentials and without using “anonymous”, then use a fake web address with an offensive name, it is obvious that your post doesn’t really require a reply.
However, I don’t want others to be misled by your post, so I’m going to [somewhat unwisely, I admit] reply anyway…
…I ran your sample batch on my Windows XP computer, and got this error:
‘.log’ is not recognized as an internal or external command, operable program or batch file.
Obviously, something is wrong with the batch you provided. Either .log doesn’t work on all operating systems, there is an error in your script, or there is some special Windows setting needed to allow .log to work on a Windows XP/2000 systems. The batch I posted works, is not in error, and does not require any special settings on Windows XP/2000.
More importantly, the batch in my post puts the date and time information into environment variables so you can do things like construct filenames from the date and time. Using .log as you suggested does nothing more than display the date/time [in Windows XP/2000, "echo %date% %time%" would suffice]. Your batch misses the entire point of the posted batch.
HumanJHawkins said
Very nice. Thanks.
Jim said
Hi, Just got this to work on Windows 2003 with the following modification.
for /f “tokens=1-4 delims=/ ” %%i in (‘echo %date%’) do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
ie using ‘echo %date%’ instead of %date%
Perhaps if that works as well on XP and 2000, then we’ve got ourselves a universal script?