C:\_G\WWW\~ELISANET\INFO\tscmd006.html
<https://www.salminet.fi/info/tscmd006.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Thu 11-Oct-2018 21:01:56

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



6} How does one get yesterday's date?

Utilizing G(nu)Awk we can demonstrate
  @echo off & setlocal enableextensions
  gawk 'BEGIN{printf"%%s\n",strftime("%%x",systime()-24*60*60)}'
  set back_=1
  gawk 'BEGIN{printf"@set datewas_=%%s\n",^
    strftime("%%d.%%m.%%Y",systime()-%back_%*24*60*60)}'^
    >>"%TEMP%\tmp$$$.cmd"
  for %%f in (call del) do %%f "%TEMP%\tmp$$$.cmd"
  echo The date was %datewas_%
  endlocal & goto :EOF

The output will be something like
  Wed Nov 19 2003
  The date was 19.11.2003
As a side issue also notice continuing a line with the caret ^.

There is a catch. If one uses a more recent GnuWin32 gawk then with that gawk port (let's call it unxgawk) one has to adjust the syntax to
  @echo off & setlocal enableextensions
  unxgawk "BEGIN{printf\"%%s\n\",strftime(\"%%x\",systime()-24*60*60)}"
  set back_=1
  unxgawk "BEGIN{printf\"@set datewas_=%%s\n\",strftime(\"%%d.%%m.%%Y\",systime()-%back_%*24*60*60)}">>"%TEMP%\tmp$$$.cmd"
  for %%f in (call del) do %%f "%TEMP%\tmp$$$.cmd"
  echo The date was %datewas_%
  endlocal & goto :EOF

This task could also be performed using Zeller's congruence. It will take some programming. External date to/from Julian date number programs are utilized in the solution below
  @echo off
  ::
  :: This option is not valid beyond 32-bit
  if defined ProgramW6432 (
    echo/
    echo Exiting: %~f0 is incompatible with 64-bit Windows
    goto :EOF)
  ::
  :: Get and show today's Julian date number
  :: Requires DATE2NUM.EXE from tscmd.zip
  DATE2NUM /set > "%mytemp%\tmp$$$.cmd"
  for %%c in (call del) do %%c "%mytemp%\tmp$$$.cmd"
  echo datenum_=%datenum_%
  ::
  :: Calculate yesterday's Julian date number
  set /a yesterday_=%datenum_% -1
  ::
  :: Convert it back into a date
  :: Requires NUM2DATE.EXE from tscmd.zip
  NUM2DATE %yesterday_% /set > "%mytemp%\tmp$$$.cmd"
  for %%c in (call del) do %%c "%mytemp%\tmp$$$.cmd"
  echo dd_=%dd_% mm_=%mm_% yyyy_=%yyyy_%
  ::
  :: Clean up
  for %%v in (datenum_ yesterday_ dd_ mm_ yyyy_) do set %%v=

The output would be something like
  D:\TEST>cmdfaq
  datenum_=2452982
  dd_=7 mm_=12 yyyy_=2003

Yesterday's date problem can also be solved with a Visual Basic Script (VBScript) aided command line script
  @echo off & setlocal enableextensions
  :: Build a Visual Basic Script
  findstr "'VBS" "%~f0"|findstr /v "findstr" > tmp$$$.vbs
  :: Run it with Microsoft Windows Script Host Version 5.6
  cscript //nologo tmp$$$.vbs
  :: Call the command line script the script host built
  call tmp$$$.cmd
  :: Clean up
  for %%f in (tmp$$$.vbs tmp$$$.cmd) do if exist %%f del %%f
  :: Show the result
  echo %date%
  echo yd_=%yd_%
  endlocal & goto :EOF
  '
  Dim oShell 'VBS
  Set oShell = WScript.CreateObject ("WSCript.shell") 'VBS
  oShell.run "cmd /c echo @set yd_=" & DateAdd("d",-1,Date) & " > tmp$$$.cmd" 'VBS

The output would be something like
  D:\TEST>cmdfaq
  31.01.2004
  yd_=30.01.2004

Or, more concisely
  @echo off & setlocal enableextensions
  set vbs_=%temp%\tmp$$$.vbs
  >"%vbs_%" echo WScript.Echo DateAdd("d",-1,Date)
  for /f "tokens=* delims=" %%a in (
    'cscript //nologo "%vbs_%"') do set yd_=%%a
  for %%f in ("%vbs_%") do if exist %%f del %%f
  :: Show the result
  echo %date%
  echo yd_=%yd_%
  endlocal & goto :EOF

Just for the sake of demonstration, even if not necessary for this item in any way, the usage of usebackq
  @echo off & setlocal enableextensions
  set vbs_=%temp%\tmp$$$.vbs
  >"%vbs_%" echo WScript.Echo DateAdd("d",-1,Date)
  for /f "usebackq tokens=* delims=" %%a in (
    `cscript //nologo "%vbs_%"`) do set yd_=%%a
  for %%f in ("%vbs_%") do if exist %%f del %%f
  :: Show the result
  echo %date%
  echo yd_=%yd_%
  endlocal & goto :EOF

Also see items #31 and #147. Furthermore, see the related EDATE.CMD and GDATE.CMD scripts accompanying tscmd.zip

References/Comments: (If a Google message link fails try the links within the brackets.)
  About the calendar issues in this FAQ
  Google Groups Feb 1 2004, 1:03 am [M]
  Google Groups 5 Jul 2004 21:30:57 [T]
  Google Groups May 1 2010, 1:18 am [M]
  gawk online manual

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd006.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd006
https://www.salminet.fi/info/tscmd006.html
file:///c:/_g/www/~elisanet/info/tscmd006.html