Why is choice returning errorlevels oddly? - windows

choice /c 123456789 >NUL
if errorlevel = 9 if %ui9%==9 set "ui9=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 8 if %ui8%==8 set "ui8=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 7 if %ui7%==7 set "ui7=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 6 if %ui6%==6 set "ui6=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 5 if %ui5%==5 set "ui5=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 4 if %ui4%==4 set "ui4=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 3 if %ui3%==3 set "ui3=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 2 if %ui2%==2 set "ui2=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 1 if %ui1%==1 set "ui1=X" & set /a moves = %moves% + 1 & goto :medmodulebot
cls
echo That spot's already taken
ping localhost -n 3 >NUL
goto medmoduleuser
I'm making a tic-tac-toe bot. When entering a digit such as 9, then entering it again in the next turn, it will trigger the if errorlevel = 8 if %ui8%==8 set "ui8=X" & set /a moves = %moves% + 1 & goto :medmodulebot instead of just going through the code until it hits the bottom... It seems that choice is returning errorlevels oddly? What do I do to prevent this?

In addition to the comments, errorlevel testing could be avoided completely. By converting the choice value into the literal value with a for / F loop metavariable
#echo off & Setlocal ENABLEdelayedexpansion
Rem // script body
For /F "Delims=" %%C in ('Choice /N /C:123456789') do (
if !ui%%C!==%%C (
set "ui%%C=X"
set /a moves+=1
goto :medmodulebot
)
)
cls
echo That spot's already taken
ping localhost -n 3 >NUL
goto medmoduleuser

Related

Game Crashes After Playing The First Round, How Can I Get It To Function Correctly?

So, I am working on a batch TD game, and whenever i test it and play through Round 1, It crashes instantly. I have no idea what could cause this. I'll send the code, Thanks in advance! (healing and conceding doesnt work right now)
#echo off
goto gamediff
:gamediff
cls
echo GAME COLOUR
echo.
echo [1] Blue (Easy)
echo [2] Yellow (Normal)
echo [3] Red (Hard)
set /p col=
if %col% == 1 set /a cashamm= 500 & set /a damamm= 30 & goto gameload
if %col% == 2 set /a cashamm= 250 & set /a damamm= 50 & goto gameload
if %col% == 3 set /a cashamm= 250 & set /a damamm= 60 & goto gameload
goto gamediff
:gameload
set /a turn= 1
set /a basic= 0
set /a heavy= 0
set /a snip= 0
set /a total= 0
set /a cash= %cashamm%
set /a dam= %damamm%
set /a attack= 0
set /a heal= 3
set /a health= 500
ping 127.0.0.1 -n 2 > nul
if %col% == 1 color b0
if %col% == 2 color f0
if %col% == 3 color c0
goto game
:game
cls
set /a nonpro= %dam%
echo Round %turn%
echo Cash: %cash%
echo Opponent Damage: %dam%
echo Your Protection: %attack%
echo Helth: %health%
echo.
echo STATS
echo Basic Troops: %basic%
echo Heavy Troops: %heavy%
echo Sniping Troops: %snip%
echo TOTAL: %total%
echo.
echo.
echo [0] Concede
echo [1] Play Round
echo [2] Recruitment
echo [3] Heal (Heals left: %heal%)
set /p gamin=
if %gamin% == 0 goto lose
if %gamin% == 1 goto play
if %gamin% == 2 goto shop
if %gamin% == 3 goto healtime
goto game
:play
cls
echo YOU PROTECT!
ping 127.0.0.1 -n 2 > nul
echo %attack% PROTECTED
ping 127.0.0.1 -n 3 > nul
echo.
echo THEY ATTACK!
ping 127.0.0.1 -n 2 > nul
set /a nonpro-= %attack%
if %nonpro% LSS 1 set /a nonpro= 0
set /a health-= %nonpro%
echo -%nonpro% HEALTH!
pause
if %health% LSS 1 goto lose
if %round% GTR 50 goto win
set /a round+= 1
set /a dam+= %random% %% 20
set /a cash+= %random% %% 100
goto game
:shop
cls
echo Recruitment
echo.
echo [0] Leave
echo [1] Basic Troop (-50 Cash, +15 Protection)
echo [2] Heavy Troop (-100 Cash, +40 Protection)
echo [3] Sniping Troop (-70 Cash,+25 Protection)
set /p shope=
if %shope% == 0 goto game
if %shope% == 1 goto buy1
if %shope% == 2 goto buy2
if %shope% == 3 goto buy3
goto shop
:buy1
if cash LSS 50 goto game
set /a cash-= 50
set /a attack+= 15
goto game
:buy2
if cash LSS 100 goto game
set /a cash-= 100
set /a attack+= 40
goto game
:buy3
if cash LSS 70 goto game
set /a cash-= 70
set /a attack+= 25
goto game
Thanks to anyone who can help me!
If you need more information, please ask for it in the comments.
You can contact me through whatever this site has if you run into any more problems. I can also help you with any questions you have, quickly.
lose and win are non-existent labels. Use quotations when using if, and also with set unless you're declaring a variable as another. The /a switch is not necessary for set whenever arithmetic is not being performed.
Other proper things to do while writing in batch:
--In a goto statement ensure the label is preceded with a colon :.
--When prompting user input in the form of selection with under 9 options, use the choice command. It's more suitable.
--Your variable names should make sense.
--Batch is slow, inefficient, unreliable, elementary, featureless, and basic. Opt for a better coding language that's simple but much, much better, like VB.NET or Python. Batch is barely even a coding language.
The following code has most of the problems I mentioned fixed, except for the choice one because, in some cases, set could be a better option.
#echo off
goto gamediff
:gamediff
cls
echo GAME COLOUR
echo.
echo [1] Blue (Easy)
echo [2] Yellow (Normal)
echo [3] Red (Hard)
set /p "col=>"
if %col% == 1 set /a cashamm = 500 & set /a damamm = 30 & goto gameload
if %col% == 2 set /a cashamm = 250 & set /a damamm = 50 & goto gameload
if %col% == 3 set /a cashamm = 250 & set /a damamm = 60 & goto gameload
goto :gamediff
:gameload
set /a turn = 1
set /a basic = 0
set /a heavy = 0
set /a snip = 0
set /a total = 0
set /a cash = %cashamm%
set /a dam = %damamm%
set /a attack = 0
set /a heal = 3
set /a health = 500
timeout /t 2 /nobreak >NUL
if %col% == 1 color b0
if %col% == 2 color f0
if %col% == 3 color c0
goto :game
:game
cls
set /a nonpro = %dam%
echo Round %turn%
echo Cash: %cash%
echo Opponent Damage: %dam%
echo Your Protection: %attack%
echo Health: %health%
echo.
echo STATS
echo Basic Troops: %basic%
echo Heavy Troops: %heavy%
echo Sniping Troops: %snip%
echo TOTAL: %total%
echo.
echo.
echo [0] Concede
echo [1] Play Round
echo [2] Recruitment
echo [3] Heal (Heals left: %heal%)
set/p "gamin=>"
if %gamin% == 0 goto lose
if %gamin% == 1 goto play
if %gamin% == 2 goto shop
if %gamin% == 3 goto healtime
goto :game
:play
cls
echo YOU PROTECT!
timeout /t 2 /nobreak >NUL
echo %attack% PROTECTED
timeout /t 3 /nobreak >NUL
echo.
echo THEY ATTACK!
timeout /t 3 /nobreak >NUL
set /a nonpro -= %attack%
if "%nonpro%" LSS "1" set /a nonpro = 0
set /a health -= %nonpro%
echo -%nonpro% HEALTH!
if "%health%" LSS "1" goto :lose
if "%round%" GTR "50" goto :win
set /a round += 1
set /a dam += %random% %% 20
set /a cash += %random% %% 100
goto :game
:shop
cls
echo Recruitment
echo.
echo [0] Leave
echo [1] Basic Troop (-50 Cash, +15 Protection)
echo [2] Heavy Troop (-100 Cash, +40 Protection)
echo [3] Sniping Troop (-70 Cash,+25 Protection)
set /p "shope=>"
if "%shope%" == "0" goto :game
if "%shope%" == "1" goto :buy1
if "%shope%" == "2" goto :buy2
if "%shope%" == "3" goto :buy3
goto :shop
:buy1
if "%cash%" LSS "50" goto :game
set /a cash -= 50
set /a attack += 15
goto :game
:buy2
if "%cash%" LSS "100" goto :game
set /a cash -= 100
set /a attack += 40
goto :game
:buy3
if "%cash%" LSS "70" goto :game
set /a cash -= 70
set /a attack += 25
goto :game
:lose
REM When the player loses, run this code
goto :game
:win
REM When the player wins, run this code
goto :game
Also, your shop doesn't work. Also also I tested your code I modified and it does work

Wait for file program failed due to %Counter% LSS %Limit%

SETLOCAL ENABLEDELAYEDEXPANSION
SET /A Counter = 0
SET /A Limit = 5
:File_Check
IF EXIST D:\Automation\OTOT_OBAL\Automation\myfilter.jar (
GOTO Trigger_Found
)
ELSE IF %Counter% LSS %Limit% (
timeout /t 5 /nobreak >null
SET /A Counter = Counter + 1
GOTO File_Check
)
GOTO (VERYEND)
ENDLOCAL
:Trigger_Found
echo ON
echo TRIGGER FOUND
Exit
:VERYEND
echo !Counter!
echo program has reached maximum wait time we are going to exit.
Exit
The else is redundant, but if used must be on the same physical line as the preceding )
The target of the final goto is the label (veryend) not the label veryend.
In the absence of current code:
IF EXIST D:\Automation\OTOT_OBAL\Automation\myfilter.jar (
GOTO Trigger_Found
) ELSE IF %Counter% LSS %Limit% (
timeout /t 5 /nobreak >null
SET /A Counter = Counter + 1
GOTO File_Check
)
GOTO VERYEND
should work
IF EXIST D:\Automation\OTOT_OBAL\Automation\myfilter.jar GOTO Trigger_Found
IF %Counter% LSS %Limit% (
timeout /t 5 /nobreak >null
SET /A Counter = Counter + 1
GOTO File_Check
)
GOTO VERYEND
would probably be better.

Subtract days in batch file

I'll try to subtract 60 days of the date of today but I don't know how I can do that.
I've my date like this :
#echo off
pause
SET currentYear=%date:~9,4%
ECHO %currentYear%
SET month=%date:~6,2%
ECHO %month%
SET day=%date:~3,2%
SET date=%day%%month%%currentYear%
ECHO %day%
ECHO %date%
pause
I've look across internet but I don't find something simple and useful!
Try this:
#echo off
setlocal
Call :GetDateTime Year Month Day
Echo %Year% %Month% %Day%
Call :SubtractDate %Year% %Month% %Day% -60 Ret
echo %Ret%
exit /b
:SubtractDate Year Month Day <+/-Days> Ret
::Adapted from DosTips Functions::
setlocal & set a=%4
set "yy=%~1"&set "mm=%~2"&set "dd=%~3"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
if %a:~0,1% equ + (set /a JD=%JD%+%a:~1%) else set /a JD=%JD%-%a:~1%
set /a L= %JD%+68569, N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447, K= L-2447*J/80, L= J/11
set /a J= J+2-12*L, I= 100*(N-49)+I+L
set /a YYYY= I, MM=100+J, DD=100+K
set MM=%MM:~-2% & set DD=%DD:~-2%
set ret=%MM: =%/%DD: =%/%YYYY: =%
endlocal & set %~5=%ret%
exit /b
:GetDateTime Year Month Day Hour Minute Second
#echo off & setlocal
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
( ENDLOCAL
IF "%~1" NEQ "" set "%~1=%YYYY%"
IF "%~2" NEQ "" set "%~2=%MM%"
IF "%~3" NEQ "" set "%~3=%DD%"
IF "%~4" NEQ "" set "%~4=%HH%"
IF "%~5" NEQ "" set "%~5=%Min%"
IF "%~6" NEQ "" set "%~6=%Sec%"
)
exit /b
Here's a general purpose batch file that is robust - it can be reduced to 8 lines or so if you want to include it in another batch file.
You would use something like: datetime.bat today -60
:: Date forward & 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: for this format YYYY-MM-DD using 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" d=weekday(s)
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)^&_
echo>>"%temp%\%~n0.vbs" d
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& (
set "YY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "daynum=%result:~-1%"
)
if %daynum% EQU 1 set "weekday=Sunday"
if %daynum% EQU 2 set "weekday=Monday"
if %daynum% EQU 3 set "weekday=Tuesday"
if %daynum% EQU 4 set "weekday=Wednesday"
if %daynum% EQU 5 set "weekday=Thursday"
if %daynum% EQU 6 set "weekday=Friday"
if %daynum% EQU 7 set "weekday=Saturday"
set "day=%YY%%separator%%MM%%separator%%DD%"
echo %%day%% is set to "%day%" (without the quotes)
echo %%YY%% is set to %YY%
echo %%MM%% is set to %MM%
echo %%DD%% is set to %DD%
echo The weekday turns out to be: %weekday%
pause
Can be done also with a jscript code embedded into cmd script:
Here's the dayM.bat that accepts only one argument - the days you want to add to the current date and prints the result:
#if (#X) == (#Y) #end /* JScript comment
#echo off
cscript //E:JScript //nologo "%~f0" %*
exit /b %errorlevel%
#if (#X)==(#Y) #end JScript comment */
var days=parseInt(WScript.Arguments.Item(0));
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
var date = new Date();
WScript.Echo(date.addDays(5));
WScript.Echo("Year: " + date.getFullYear());
WScript.Echo("Month: " + date.getMonth());
WScript.Echo("DayOfTeWEek: " + date.getDay());
examaple:
E:\scripts>dayM.bat 7
Sun Nov 8 16:27:48 UTC+0200 2020
Year: 2020
Month: 10
DayOfTeWEek: 2
DayOfTheMonth: 3
You can modify it in way that will be suitable for you.

manipulate string with a batch

I've got a string named "code" and an int named "lenght" which is the lenght of "code"
also int "todo" which has a value of 0,1 or 2.
0 should delete a character at position "lenght".
1 should change the caracter at position "lenght" to a random character.
2 should add a random charachter at position "lenght".
how do i archive this goal?
SET code="%*f0"
set length=0
:loop0
if defined # (
set #=%#:~1%
set /A length += 1
goto loop0
)
SET /A location=%RANDOM% * %lenght% / 32768 + 1
SET /A todo=%RANDOM% * 2 / 32768 + 1
IF %todo% == 0(
)
IF %todo% == 1(
)
IF %todo% == 2(
)
If correctly understood you, then:
#echo off
setlocal enabledelayedexpansion
set "str=%~f0"
set "len=0"
set "buf=%str%"
:loop
if defined buf (set buf=%buf:~1%& set /a "len+=1" & goto:loop)
echo Delete char at %%len%% position^:
set /a "len-=1"
echo !str:~0,%len%!
echo.
echo Change char at %%len%% position for random^:
rem set chars string
set "map=abcdefghijklmnopqrstuvwxyz"
rem convert it to array
set "i=0"
for /l %%i in (0, 1, 25) do (
for /l %%j in (1, 1, 1) do (
set "arr.!i!=!map:~%%i,%%j!" & set /a "i+=1"
)
)
rem random set 0..25
set /a "rnd=0+25*%random%/32768"
echo !str:~0,%len%!!arr.%rnd%!
endlocal
exit /b

how to get yesterday's date in a batch file

I know how to get today's date in Windows 7. here is the command that I am using:
%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%
But I want to get yesterday, I do not know how.
If you're limited to just cmd.exe, then the other solutions, despite their size, are probably as good as you'll get. However, modern Windows (such as your Win7) ships with quite a few other tools which can do the job far easier.
Just create a VBScript yester.vbs script as follows:
d = date() - 1
wscript.echo year(d) * 10000 + month(d) * 100 + day(d)
Then you can call it from your cmd script with:
for /f %%a in ('cscript //nologo yester.vbs') do set yesterday=%%a
and the yesterday variable will be created in the form yyyymmdd for you to manipulate however you desire.
Found a script that will work to ensure you get the previous day even if the year or month changes Dos Yesterday Batch.
#echo off
set yyyy=
set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
set %%x=%%u
set %%y=%%v
set %%z=%%w
set $d1=
set $tok=))
if "%yyyy%"=="" set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100
set CurDate=%mm%/%dd%/%yyyy%
set dayCnt=%1
if "%dayCnt%"=="" set dayCnt=1
REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100
:CHKDAY
if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1
:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through
:SET31
set /A dd=31 + %dd%
goto CHKDAY
:SET30
set /A dd=30 + %dd%
goto CHKDAY
:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29
:SET28
set /A dd=28 + %dd%
goto CHKDAY
:SET29
set /A dd=29 + %dd%
goto CHKDAY
:DONE
if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%
REM Set IIS and AWS date variables
set IISDT=%yyyy:~2,2%%mm%%dd%
set AWSDT=%yyyy%-%mm%-%dd%
#echo off
:: Strip the day of the week from the current date
FOR %%A IN (%Date%) DO SET Today=%%A
:: Parse the date, prefix day and month with an extra leading zero
FOR /F "tokens=1-3 delims=/" %%A IN ("%Today%") DO (
SET Day=0%%A
SET Month=0%%B
SET Year=%%C
)
:: Remove excess leading zeroes
SET Day=%Day:~-2%
SET Month=%Month:~-2%
:: Display the results
SET Day
SET Month
SET Year
:: Convert to Julian date
CALL :JDate %Year% %Month% %Day%
:: Display the result
SET JDate
:: Subtract 1 day
SET /A JPast = JDate - 1
:: Display the result
SET JPast
:: Convert back to "normal" date again
CALL :GDate %JPast%
:: Display the result
::SET GDate=20130121
SET GDate
echo The previous day in form YYYYMMDD is %GDate%
pause
::::::::::::::::::::::::::::::::::::::::::::::::::::::
GOTO:EOF
:JDate
:: Convert date to Julian
:: Arguments : YYYY MM DD
:: Returns : Julian date
::
:: First strip leading zeroes
SET MM=%2
SET DD=%3
IF %MM:~0,1% EQU 0 SET MM=%MM:~1%
IF %DD:~0,1% EQU 0 SET DD=%DD:~1%
::
:: Algorithm based on Fliegel-Van Flandern
:: algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum
:: (http://mathforum.org/library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.
SET /A Month1 = ( %MM% - 14 ) / 12
SET /A Year1 = %1 + 4800
SET /A JDate = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %MM% - 2 -12 * % Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %DD% - 32075
SET Month1=
SET Year1=
GOTO:EOF
:GDate
:: Convert Julian date back to "normal" Gregorian date
:: Argument : Julian date
:: Returns : YYYY MM DD
::
:: Algorithm based on Fliegel-Van Flandern
:: algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum
:: (http://mathforum.org/library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.
::
SET /A P = %1 + 68569
SET /A Q = 4 * %P% / 146097
SET /A R = %P% - ( 146097 * %Q% +3 ) / 4
SET /A S = 4000 * ( %R% + 1 ) / 1461001
SET /A T = %R% - 1461 * %S% / 4 + 31
SET /A U = 80 * %T% / 2447
SET /A V = %U% / 11
SET /A GYear = 100 * ( %Q% - 49 ) + %S% + %V%
SET /A GMonth = %U% + 2 - 12 * %V%
SET /A GDay = %T% - 2447 * %U% / 80
:: Clean up the mess
FOR %%A IN (P Q R S T U V) DO SET %%A=
:: Add leading zeroes
IF 1%GMonth% LSS 20 SET GMonth=0%GMonth%
IF 1%GDay% LSS 20 SET GDay=0%GDay%
:: Return value
:: Here you can define the form that you want
SET GDate=%GYear%%GMonth%%GDay%
GOTO:EOF
Here's a solution that creates the earlierday.vbs file on the fly, uses it and deletes it afterwards.
It stores the result in the NewDate variable
This example calculates 1 day ago, but can easily calculate a date further back by changing the value of the Offset variable.
#echo off
set Offset=1
echo d = date() - WScript.Arguments.Item(0) > earlierday.vbs
echo wscript.echo year(d) * 10000 + month(d) * 100 + day(d) >> earlierday.vbs
for /f %%a in ('cscript //nologo earlierday.vbs %Offset%') do set NewDate=%%a
del earlierday.vbs
echo %NewDate%
pause
You could refine this slightly by using %temp%\earlierday.vbs to create the file in the user's temp folder.
Credits to paxdiablo as this is a simple tweak on his earlier post.
EDIT: Here's something with a loop, close to what I actually need it to do. This will take 14 days off today's date and return that date. Then it will keep going back 7 days at a time until it gets to 35 days day ago.
#echo off
SETLOCAL EnableDelayedExpansion
set BackDaysFrom=14
Set BackDaysTo=35
Set BackDaysStep=7
echo d = date() - WScript.Arguments.Item(0) > earlierday.vbs
echo wscript.echo year(d) * 10000 + month(d) * 100 + day(d) >> earlierday.vbs
for /L %%i in (%BackDaysFrom%, %BackDaysStep%, %BackDaysTo%) do (
for /f %%a in ('cscript //nologo earlierday.vbs %%i') do set NewDate=%%a
echo !NewDate!
)
del earlierday.vbs
pause

Resources