%y - the year in two-digit format
%Y - the year in four-digit format
%m - the month as a two-digit decimal number (01-12)
%d - the day of the month as a two digit decimal number (01-31)
%H - the hour in two-digit 24 hour clock format (00-23)
%I - the hour in two-digit 12 hour clock format (01-12)
%p - AM/PM designation
%M - the minute as a two digit decimal number (00-59)
%a - abbreviated day name
%A - full day name
%b - abbreviated month name
%B - full month name
%c - the "local appropriate date and time representation" (whatever that means)
%U - the week number (00 - 53, changes on Sunday)
%W - the week number (00 - 53, changes on Monday)
%w - the number of the day of the week (0 - 6, Sunday = 0)
%x - the "local appropriate date representation"
%X - the "local appropraite time representation"
%z - the time zome, if available
%% - a real '%' sign
DateString = strftime( format_string, time)
where DateString is the output string, format_string is a string made up of formatting flags (above) mixed with text and/or delimiters, and time is the time to process in the format returned by the systime() function.BEGIN{
Format = "%Y%m%d%H%M"
print strftime( Format, systime() )
}
to display the current date and time in YYYYMMDDhhmm format. As a one shot batch program that becomes
GSPC0010.BAT (DATETIME.BAT) @gawk "BEGIN{Format = \"%Y%m%d%H%M\";print strftime( Format, systime() )}"
Note that unlike MAWK, GAWK accepts short programs on the command line. @echo off
set format=set datetime=%%Y%%m%%d%%H%%M
gawk "BEGIN{Format = \"%format%\";print strftime( Format, systime() )}" > }{.bat
call }{
del }{.bat
set format=
to put the formatted date and time into an environment variable, where it can be used to rename files, make directories, and numerous other tasks. That is NT specific because of the '=' in the argument to the SET command: you can't use '=' inside a SET command in any other of the common Microsoft operating systems, and while it is trivial to work around this simply by putting the format string in the GAWK argument as a string literal, there are many cases where it would be useful to define the format once for use many times. @echo off
set format=%%Y%%m%%d%%H%%M
gawk "BEGIN{Format = \"set datetime=%format%\";print strftime( Format, systime() )}" > }{.bat
call }{
del }{.bat
set format=
and gain the advantage that the format string can be used for things other than just setting one specific environment variable. However, this does not solve all the possible problems found in variations of this program. Rather than try to identify all of them and treat them individually, I will take this as a good point to introduce the use of escaped octal character numbers in string - such as format strings - that are treated as string literals by both batch language and the AWK statement parser. This may be a bit forced here, but the technique is valuable and I want to introduce it early in this work.'%', \045
',', \054
';', \073
'<', \074
'=', \075
'>', \076
'|', \174
'>', '<', and '|', command argument delimiters ',', ';', and '=', and the variable marker '%'. '^' (\136) and '&' (\046) are magic in NT4, but not in Real DOS or Win9x. @echo off
set format=set datetime\075\045Y\045m\045d\045H\045M
gawk "BEGIN{Format = \"%format%\";print strftime( Format, systime() )}" > }{.bat
call }{
del }{.bat
set format=
Because that is about as bullet proof as I can make it, I will use that code as the general pattern for all code to set an environment variable to the date and/or time in whatever format is needed at the moment. @echo off
if %1!==! goto error
set format=set datetime\075\045y\045m\045d
gawk "BEGIN{Format = \"%format%\";print strftime( Format, systime() )}" > }{.bat
call }{
del }{.bat
ren %1 %datetime%.*
set datetime=
set format=
goto end
:error
echo SYNTAX: %0 filename
pause
:end
As code to include in another batch file where the file is already known - replace "foo" with the name of the file or the reference to the environment variable containing it.
set format=set datetime\075\045y\045m\045d
gawk "BEGIN{Format = \"%format%\";print strftime( Format, systime() )}" > }{.bat
call }{
del }{.bat
ren foo %datetime%.*
set datetime=
set format=
@echo off
set format=set datetime\075\045m\045d\045Y
gawk "BEGIN{Format = \"%format%\";print strftime( Format, systime() )}" > }{.bat
call }{
del }{.bat
md %datetime%
set datetime=
set format=
@echo off
set format=set datetime\075\045m-\045d-\045Y
gawk "BEGIN{Format = \"%format%\";print strftime( Format, systime() )}" > }{.bat
call }{
del }{.bat
md %datetime%
set datetime=
set format=
@echo off
if exist tue.bat goto end
set format=set day\075\045w
gawk "BEGIN{Format = \"%format%\";print strftime( Format, systime() )}" > }{.bat
call }{
del }{.bat
if %day%==2 echo dir > tue.bat
if %day%==3 del tue.bat
if exist tue.bat call tue.bat
set day=
:end
** Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001 Ted Davis - see License, included by reference. **
Input and feedback from readers are welcome. NOTE: the subject of the message must contain the word "batch" for the message to get past the spam filter.
Back to the Table of Contents page
Back to my personal links page - back to my home page