13} How do I get the current day of the week? (Or yesterday?)
For today we have utilizing
G(nu)Awk
@echo off & setlocal enableextensions
rem Requires gawk
gawk
'BEGIN{printf"@set wd_=%%s\n",strftime("%%a")}
'>"%TEMP%\tmp$$$.cmd"
for %%c in (call del) do %%c "%TEMP%\tmp$$$.cmd"
echo The weekday today is %wd_%
set wd_=
endlocal & goto :EOF
The output could be e.g.
C:\_D\TEST>cmdfaq
The weekday today is Thu
Or
@echo off & setlocal enableextensions
rem Requires GnuWin32 gawk (let's call it unxgawk)
unxgawk
"BEGIN{printf
\"@set wd_=%%s\n
\",strftime(
\"%%a
\")}
">"%TEMP%\tmp$$$.cmd"
for %%c in (call del) do %%c "%TEMP%\tmp$$$.cmd"
echo The weekday today is %wd_%
set wd_=
endlocal & goto :EOF
Or, if one has a
UNIX-port date (let's
call it
unxdate for distinction) we can
have
@echo off & setlocal enableextensions
for /f %%a in ('unxdate "+%%a"') do set wd_=%%a
echo wd_=%wd_%
endlocal & goto :EOF
The output could be e.g.
C:\_D\TEST>cmdfaq
wd_=Thu
For yesterday
@echo off & setlocal enableextensions
rem Requires gawk
gawk 'BEGIN{printf"@set wd_=%%s\n",strftime("%%a",systime()-24*60*60)}'>"%TEMP%\tmp$$$.cmd"
for %%c in (call del) do %%c "%TEMP%\tmp$$$.cmd"
echo The weekday yesterday was %wd_%
endlocal & goto :EOF
The output could be e.g.
C:\_D\TEST>cmdfaq
The weekday yesterday was Wed
Or without the auxiliary, temporary file
@echo off & setlocal enableextensions
rem This time using GnuWin32 gawk (let's call it unxgawk)
for /f usebackq %%a in (`
unxgawk "BEGIN{printf\"%%s\n\",strftime(\"%%a\",systime()-24*60*60)}"`) do (
set wd_=%%a)
echo The weekday yesterday was %wd_%
endlocal & goto :EOF
Another option for today's weekday is
@echo off
::
:: Requires DATE2NUM.EXE from tscmd.zip. Not for 64-bit.
if defined ProgramW6432 (
echo/
echo Exiting: %~f0 is incompatible with 64-bit Windows
goto :EOF)
::
:: Get and show today's Julian date number
DATE2NUM /set>"
%mytemp%\tmp$$$.cmd"
for %%c in (call del) do %%c "%mytemp%\tmp$$$.cmd"
echo datenum_=%datenum_%
::
:: Convert the result into a weekday number (Mon=1 to Sun=7)
set /a wd_=(%datenum_% %% 7) + 1
echo wd_=%wd_%
::
:: Convert the weekday into three letters
for /f "tokens=%wd_%" %%d in ('echo Mon Tue Wed Thu Fri Sat Sun') do echo %%d
::
:: Clean up
for %%v in (datenum_ wd_) do set %%v=
The output on Thu 30-Jun-2005 would have been
C:\_D\TEST>cmdfaq
datenum_=2453552
wd_=4
Thu
Apropos
%mytemp%. If not (pre)set, the
temporary files will go to the root directory.
Incidentally, for
SET/A the line
set /a wd_=(%datenum_% %% 7) + 1
could be also written as
set /a wd_=(datenum_ %% 7) + 1
(but there is a
caveat [
M].)
The problem can also be solved with a Visual Basic
Script (VBScript) aided command line script
@echo off & setlocal enableextensions
:: Build a Visual Basic Script
set skip=
findstr "'%skip%VBS" "%~f0" > "%temp%\tmp$$$.vbs"
:: Run it with Microsoft Windows Script Host Version 5.6
cscript //nologo "%temp%\tmp$$$.vbs"
:: Call the command line script the script host built
call "%temp%\tmp$$$.cmd"
:: Clean up
for %%f in ("%temp%\tmp$$$.vbs" "%temp%\tmp$$$.cmd") do if exist %%f del %%f
:: Show the result
echo wd_=%wd_%
endlocal & goto :EOF
'
'The Visual Basic Script
Const ForReading = 1, ForWriting = 2, ForAppending = 8 'VBS
Dim DateNow, fso, f, MyTemp, TempFile 'VBS
DateNow = Date 'VBS
Set WshShell = WScript.CreateObject("WScript.shell") 'VBS
MyTemp=WshShell.ExpandEnvironmentStrings("%temp%") 'VBS
Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
TempFile = MyTemp & "\tmp$$$.cmd" 'VBS
Set f = fso.OpenTextFile(TempFile, ForWriting, True) 'VBS
f.Write "@set wd_=" & WeekDayName(Weekday(DateNow),true) 'VBS
f.Close 'VBS
Or, much more concisely
@echo off & setlocal enableextensions
if not exist c:\mytemp mkdir c:\mytemp
echo>c:\mytemp\tmp$$$.vbs WScript.Echo WeekDayName(Weekday(Date),true)
for /f %%d in ('cscript //nologo c:\mytemp\tmp$$$.vbs') do set wd_=%%d
for %%f in (c:\mytemp\tmp$$$.vbs) do if exist %%f del %%f
rmdir c:\mytemp
echo Weekday wd_=%wd_%
endlocal & goto :EOF
The output could be e.g.
D:\TEST>cmdfaq
Weekday wd_=Thu
Using a number to indicate the weekday instead of the letters
@echo off & setlocal enableextensions
if not exist c:\mytemp mkdir c:\mytemp
echo>c:\mytemp\tmp$$$.vbs WScript.Echo Weekday(Date,vbMonday)
for /f %%d in ('cscript //nologo c:\mytemp\tmp$$$.vbs') do set wd_=%%d
for %%f in (c:\mytemp\tmp$$$.vbs) do if exist %%f del %%f
rmdir c:\mytemp
echo Weekday number ^(with Monday as 1^) wd_=%wd_%
endlocal & goto :EOF
The output could be e.g.
D:\TEST>cmdfaq
Weekday number (with Monday as 1) wd_=4