1.

Solve : DOS batch question on variable addition?

Answer»

Hi, is anyone here good at DOS scripts? I am new in dos script and having this simple script that adds the date but it doesn't work as I expected. Any help WOULD be greatly appreciated.

Here is the script,

@echo off

for /f "tokens=2-4 skip=1 delims=(-./)" %%i in ('echo.^|date') do (
for /f "tokens=1-4 delims=-./ " %%m in ('date /t') do (
(set dow=%%m)&(set %%i=%%n)&(set %%J=%%o)&(set yyyy=%%p)))
echo %dd%
for /l %%c in (1 1 3) do (
echo %%c
set /a var1 = %dd% + %%c%
echo %var1%
Set d=%yyyy%%mm%%var1%
echo %d%)

I expected that the variable %d% would be '20130710', then adds one (%%c as 1) to the %var1% would be %dd% + 1 = 11 (%dd% is 10 for today), and %d% would be '20130711', but %d% never change and stay as '20130710'. Anyone know why?

THANKS,
Alan110I think its related to that fact that you require:

setlocal EnableDelayedExpansion

Such as referenced at this site to allow to INCREMENT++ a variable. http://stackoverflow.com/questions/6097579/batch-variable-not-incrementingHere's a script to give you the date tomorrow, yesterday, in 2 weeks time, etc.

Code: [Select]:: Date foward & backward
@echo off
if "%~2"=="" (
echo to get todays date use call "%~n0" today 0
echo to get yesterdays date use call "%~n0" today -1
echo to get 25 days before 19441213 call "%~n0" 1944/12/13 -25
echo to get 1250 days in the future call "%~n0" today 1250
echo.
echo Add a third parameter if you want a separator in the date string
echo EG: to use - as in YYYY-MM-DD for today's date
echo call "%~n0" today 0 -
echo.
pause
goto :EOF)

set date1=%1
set qty=%2
set separator=%~3
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%%separator%%result:~4,2%%separator%%result:~6,2%
echo %%day%% is set to "%day%" (without the quotes)
pause



Discussion

No Comment Found