I have a cmd line that reads
I need to assign the vardate a value of yesterday's date.
Is there a way to assign the current date -1?
:: Date foward & backward
@echo off
:: from code by Phil Robyn
setlocal
if [%1]==[] (
echo to get todays date use
echo call "%~n0" today 0
echo.
echo to get yesterdays date use
echo call "%~n0" today -1
echo.
echo to get the date 25 days ago:
echo call "%~n0" today -25
echo.
echo to get the date 1250 days in the future
echo call "%~n0" today +1250
goto :EOF)
set date1=%1
set qty=%2
if /i "%date1%" EQU "TODAY" (
set date1=now
) else (
set date1="%date1%"
)
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs" right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs" right(100+day(s),2)
for /f %%a in (
'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set day=%result:~0,4%-%result:~4,2%-%result:~6,2%
echo %%day%% is set to "%day%" (without the quotes)
It seems to me to be easier to use a batch/WSH hybrid here, as in ...
::RunWSH.CMD
@echo off
if '%1==' goto :EOF
if /i '%1=='date %_then_% (
%0 "year(now)&right(chr(48)&month(now),2)&right(chr(48)&day(now),
2)%2"
)
echo wsh.echo "res="^&eval("%~1")> %temp%\tmp.vbs
for /f "delims=" %%a in ('cscript //nologo %temp%\tmp.vbs') do set %
%a
del %temp%\tmp.vbs
% For demonstration % echo Res = %Res%
This takes up to two arguments. The first can be an expression for
evaluation, or the word DATE (case insensitive) and the second
[optional] argument can be a continuation of the evaluation or a
number to add (+/-n) to the DATE. The expressions can be enclosed
with double quotes, if needed to handle included delimiters, like
commas or spaces. However, the procedure as written cannot handle
string (text) expressions.
The procedure stores its return value in the environment variable
RES. The last line is intended as an illustration, only. It is best
to remove it or comment it out for production use.
In use, the current question can be answered in several ways,
depending on the desired format of the result ...
C:\>RunWSH date -1
Res = 20090215
or
C:\>RunWSH date-1
Res = 2/15/2009
or
C:\>RunWSH now-1
Res = 2/15/2009 8:14:09 AM
I suspect the first version is the most helpful.
The procedure is not limited to date manipulation, however. For
example, the issue of handling decimal math ...
C:\>RunWSH 2/3
Res = 0.666666666666667
etc. Anything that can be expressed as a valid VBS mathematical
expression. Values stored in environment variables can be used, as
with any batch procedure. Knowledge of VBS IS needed to use
complicated expressions or intrinsic functions, but not to do
straightforward math. Such math expressions are almost universally as
you might expect, simply the numbers with the operators between.
Tom Lavedas
***********
http://there.is.no.more/tglbatch/