Date arithmetic in cmd scripting - date

I need to write a script to change a filename from aDate.txt to bDate.txt where:
aDate is the current system date in yyyymmdd format and
bDate is the current system date - 1 in yyyymmdd format.
I currently have:
set yy=%date:~6,2%
set mm=%date:~3,2%
set dd=%date:~0,2%
if "%date:~6,1%"==" " set yy=0%yy:~1,1%
if "%date:~3,1%"==" " set mm=0%mm:~1,1%
if "%date:~0,1%"==" " set dd=0%dd:~1,1%
SET sys_date=20%yy%%mm%%dd%
ECHO %sys_date%
REM still have to do this bit properly
SET sys_date_yesterday=%sys_date%a
move %sys_date%.txt %sys_date_yesterday%.txt
but I have no idea how to do the date -1 thing (other than the long winded) subtract 1 from the day and if that is = 0 then subtract one from the month and set the day = to the last day of the new month and so on for years.
Any ideas?

You have to do it the difficult way. I suggest to use this solution by SteveGTR. I copy the text below, because at least at least I cannot always see the solution on that site.
Here's a batch file I developed to subtract any number of days from the current date. It accepts a command line parameter of the number of days. The default is 1 day (yesterday):
#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%
echo Date %dayCnt% day(s) before %CurDate% is %mm%/%dd%/%yyyy%
Good Luck,
Steve

Easily Add or Subtract Days from a Date with a Windows Batch Script
Here's a solution I came up with for calculating date (add or subtract) with a batch script. Set the variables accordingly for your needs and then adjust the logic as need for your needs as well. This works very well for my needs and it's all contained to the same one batch script without too much logic.
To add: You can also use this script to add a number of days to the current date by deleting the minus (-)
symbol from the below batch script in the :DynamicVBSScriptBuild routine, so where you see this,-%MinusDay%, you simple remove the minus symbol to get ,%MinusDay%, on each of those lines and now the MinusDay= variable value will equal the number of days you want to add.
Important Note: It seems that five 9's (99999) is the limit on the batch script when subtracting with the MinusDays= value. It also seems that six 9's (999999) is the limit on the batch script when adding with the MinusDays= value.
Batch Script
#ECHO ON
::// Minus days is the number of days to subtract from the CURRENT DAY i.e. 2 for minus 2 days or 99999 for minus 99999 days from when it's run
SET MinusDay=2
:: This calls the temp vbs script routine that will be used to set YYYY-MM-DD values for the subtracted days date you specify
CALL :DynamicVBSScriptBuild
FOR /F "TOKENS=*" %%A IN ('cscript//nologo "%YYYYTmpVBS%"') DO SET YYYY=%%A
FOR /F "TOKENS=*" %%A IN ('cscript//nologo "%MMTmpVBS%"') DO SET MM=%%A
FOR /F "TOKENS=*" %%A IN ('cscript//nologo "%DDTmpVBS%"') DO SET DD=%%A
::// Set your web server log file path in the below variable
SET WebServerLogPath=C:\WebServer\Logs
::// Set web server log file name where YYYY MM DD variables are set to the values after the day numbers setup above are subtracted
SET YYYY=%YYYY%
SET MM=%MM%
SET DD=%DD%
ECHO %YYYY%%MM%%DD%
PAUSE
GOTO EOF
:DynamicVBSScriptBuild
SET YYYYTmpVBS=%temp%\~tmp_yyyy.vbs
SET MMTmpVBS=%temp%\~tmp_mm.vbs
SET DDTmpVBS=%temp%\~tmp_dd.vbs
IF EXIST "%YYYYTmpVBS%" DEL /Q /F "%YYYYTmpVBS%"
IF EXIST "%MMTmpVBS%" DEL /Q /F "%MMTmpVBS%"
IF EXIST "%DDTmpVBS%" DEL /Q /F "%DDTmpVBS%"
ECHO dt = DateAdd("d",-%MinusDay%,date) >> "%YYYYTmpVBS%"
ECHO yyyy = Year(dt) >> "%YYYYTmpVBS%"
ECHO WScript.Echo yyyy >> "%YYYYTmpVBS%"
ECHO dt = DateAdd("d",-%MinusDay%,date) >> "%MMTmpVBS%"
ECHO mm = Right("0" ^& Month(dt),2) >> "%MMTmpVBS%"
ECHO WScript.Echo mm >> "%MMTmpVBS%"
ECHO dt = DateAdd("d",-%MinusDay%,date) >> "%DDTmpVBS%"
ECHO dd = Right("0" ^& Day(dt),2) >> "%DDTmpVBS%"
ECHO WScript.Echo dd >> "%DDTmpVBS%"
GOTO EOF
Further Resources
FOR /F
CSCRIPT
WSCRIPT
DateAdd
Right

I needed something that would subtract days from the current date while checking leap years, etc. and this worked great.
I just call it from those scripts with the needed parameter (number of days to subtract), and then have it call back the calling script with substitutions and pass a parameter back to the original script for the modified (subtracted) date.
Here are examples:
Script needing date calculation variable set:
IF "%1"=="" goto modifydate
:modifydate
SET subtractdays=5
SET ModDateScript=\\servershare\path\Called_Scripts\ModDate.cmd
CALL "%ModDateScript%" %subtractdays% "%~fnx0"
Script which will calculate and pass back a %moddate% parameter to the original calling script to be set as a variable for it to process accordingly. You will simply put this at the end of the script you call to modify/subtract days from the current date (ModDate.cmd).
SET moddate=%mm%/%dd%/%yyyy%
Call %2 %moddate%
GOTO EOF
I was able to test and determine that these lines from the original script posted:
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
Can be replaced with just this one single line and it works just as well:
FOR /F "tokens=2-4 delims=/ " %%A IN ("%date%") DO SET "mm=%%A" DO (& SET "dd=%%B") DO (& SET "yyyy=%%C")
Please explain what those lines (the ones I changed to just the one line with and statements) do anyways because I cannot tell the difference quickly testing. I subtracted back to the 19th century and it appeared accurate to me. I thought perhaps it helped handle the calculations where the modified year would be less than 2000 -- but I didn't see that unless I'm missing something.
Otherwise this one script can be easily called and pass back the %mm%/%dd%/%yyyy% as a parameter for several scripts which need their own calculations. Great and very efficient batch solution. I can pass the argument as %1, %2, %3, etc. and still use the setlocal in that script for the current date -- just make a variable something like moddate=%1, etc.
Lastly, I challenge any batch script expert to optimize this script even further and post back the results for batch people to test.
Thanks,
P

Try with this code in other words. You could use as a script subroutine or use this with the CALL and parameters functions to pass back to the original batch file:
:: Pass 1st parameter as number of days (whole numbers) to subtract from current day in date
:: This script is able to subtract days to any date of the current date
:: This script will check for leap years, etc. as well
#echo on
::for /f "tokens=2-4 delims=/ " %%A in ("%date%") do set "mm=%%A" do & set "dd=%%B" do & set "yyyy=%%C"
set "mm=%date:~4,2%" & set "dd=%date:~7,2%" & set "yyyy=%date:~10,4%"
set CurDate=%mm%/%dd%/%yyyy%
set dayCnt=%1
if "%dayCnt%"=="" set dayCnt=1
:: 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
:: ** 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%
echo Date %dayCnt% day(s) before %CurDate% is %mm%/%dd%/%yyyy%
SET DirDate=%mm%/%dd%/%yyyy%
:: The %2 parameter is passed from the calling script as the full path and name of the file to call back
:: %2 equals %~fnx0
:: The dirdate variable is passed as parameter %1 back to the calling script
Call %2 %dirdate%
GOTO EOF
I'm going to look for a vb or something more efficient I can still incorporate or call from a batch to dynamically calculate dates.

Found this script on ss64.com: https://ss64.com/nt/syntax-datemath.html (license: https://ss64.com/docs/copyright.html)
You can keep it separate and call it from your batch files without cluttering your code, it will fill some environments variables with the operation result.
For example, this will subtract one day to the current date (on my system date is returned in the "dd/mm/yyyy" format):
set YY=%date:~-4,4%
set MM=%date:~-7,2%
set DD=%date:~-10,2
call datemath.bat %YY% %MM% %DD% - 1
echo year=%_yy_int%, month=%_mm_int%, day=%_dd_int%
echo padded date:%_ymd_str%, padded month:%_mm_str%, padded day:%_dd_str%
The script:
#ECHO off
SETLOCAL
:: DateMath, a general purpose date math routine
:: If DateMath detects an error, variable _dd_int is set to 999999.
SET v_dd_int=0
SET v_mm_int=0
SET v_yy_int=0
SET v_ymd_str=
SET v_mm_str=
SET v_dd_str=
IF "%3"=="" goto s_syntax
IF "%4"=="+" goto s_validate_year
IF "%4"=="-" goto s_validate_year
IF "%4"=="" goto s_validate_year
:s_syntax
echo:
echo DATEMATH SYNTAX:
echo _______________
echo:
echo DateMath will set the variables as listed below
echo 'str' variables include leading zeros e.g. "01"
echo 'int' variables leading zeros are stripped e.g. "1"
echo:
echo CALL DateMath YY MM DD - YY2 MM2 DD2
echo:
echo Will set variable _dd_int to the signed difference
echo between the 2 dates (measured in days)
echo:
echo:
echo CALL DateMath YY MM DD +/- Days
echo:
echo Will set the following variables to the result of
echo adding or substracting days from the initial date:
echo _ymd_str, _yy_int
echo _mm_str, _mm_int,
echo _dd_str, _dd_int
echo:
echo:
echo ___________________________________
pause
echo:
echo:
echo CALL DateMath YY MM DD
echo:
echo Will set the following variables:
echo _ymd_str, _yy_int
echo _mm_str, _mm_int,
echo _dd_str, _dd_int
echo:
echo ___________________________________
echo:
echo _ymd_str is in YYYYMMDD format.
echo:
echo _yy_int is in YYYY format, even if YY format was originally supplied.
echo This conversion is useful for FAT/NTFS file dates which are in YY format.
echo:
ENDLOCAL & SET /a _dd_int=999999
goto :eof
:s_validate_year
::strip leading zeros
SET v_yy=%1
if %v_yy:~0,1% EQU 0 set v_yy=%v_yy:~1%
:: Check for Y2K
IF %v_yy% LSS 100 IF %v_yy% GEQ 80 SET /A v_yy += 1900
IF %v_yy% LSS 80 SET /A v_yy += 2000
:: at this point v_yy contains a 4 digit year
::validate month and day
if %2 GTR 12 goto s_syntax
if %3 GTR 31 goto s_syntax
SET v_mm=%2
SET v_dd=%3
::strip leading zeros
if %v_mm:~0,1% EQU 0 set v_mm=%v_mm:~1%
if %v_dd:~0,1% EQU 0 set v_dd=%v_dd:~1%
:: Set the int variables
SET /a v_dd_int=%v_dd%
SET /a v_yy_int=%v_yy%
SET /a v_mm_int=%v_mm%
:: Determine which function to perform - ADD, SUBTRACT or CONVERT
If not "%6"=="" goto s_validate_2nd_date
if "%4"=="" goto s_convert_only
:: Add or subtract days to a date
SET /a v_number_of_days=%5
goto s_add_or_subtract_days
:s_convert_only
SET /a v_dd_int=%v_dd%
IF %v_dd% LEQ 9 (SET v_dd_str=0%v_dd%) ELSE (SET v_dd_str=%v_dd%)
IF %v_mm% LEQ 9 (SET v_mm_str=0%v_mm%) ELSE (SET v_mm_str=%v_mm%)
SET v_ymd_str=%v_yy%%v_mm_str%%v_dd_str%
ECHO DATEMATH - Convert date only (no maths)
goto s_end
::::::::::::::::::::::::::::::::::::::::::::::::::
:s_validate_2nd_date
If "%4"=="+" goto s_syntax
:: Subtracting one date from another ::::::
:: strip leading zero
SET v_yy2=%5
if %v_yy2:~0,1% EQU 0 set v_yy2=%v_yy2:~1%
if %v_yy2% GTR 99 goto s_validate2nd_month
if %v_yy2% GTR 49 goto s_prefix_2_1950_1999
if %v_yy2% LSS 10 goto s_prefix_2_2000_2009
SET v_yy2=20%v_yy2%
goto s_validate2nd_month
:s_prefix_2_2000_2009
SET v_yy2=200%v_yy2%
goto s_validate2nd_month
:s_prefix_2_1950_1999
SET v_yy2=19%v_yy2%
:s_validate2nd_month
::strip leading zeros
::SET /a v_yy2=%v_yy2%
if %v_yy2:~0,1% EQU 0 set v_yy2=%v_yy2:~1%
::v_yy2 now contains a 4 digit year
if %6 GTR 12 goto s_syntax
SET v_mm2=%6
if %7 GTR 31 goto s_syntax
SET v_dd2=%7
::strip leading zeros
::SET /a v_mm2=%v_mm2%
if %v_mm2:~0,1% EQU 0 set v_mm2=%v_mm2:~1%
::SET /a v_dd2=%v_dd2%
if %v_dd2:~0,1% EQU 0 set v_dd2=%v_dd2:~1%
call :s_julian_day %v_yy_int% %v_mm_int% %v_dd_int%
SET v_sumdays1=%v_JulianDay%
call :s_julian_day %v_yy2% %v_mm2% %v_dd2%
SET v_sumdays2=%v_JulianDay%
SET /a v_dd_int=%v_sumdays1% - %v_sumdays2%
ECHO DATEMATH - Subtracting one date from another = days difference
ECHO ~~~~~~
ECHO %v_dd_int%
ECHO ~~~~~~
goto s_end_days
::::::::::::::::::::::::::::::::::::::::::::::::::
:s_add_or_subtract_days
if /i "%4"=="+" goto s_add_up_days
:: Subtract all days ::::::
SET /a v_dd=%v_dd% - %v_number_of_days%
:s_adjust_month_year
if %v_dd% GEQ 1 goto s_add_subtract_days_DONE
SET /a v_mm=%v_mm% - 1
if %v_mm% GEQ 1 goto s_add_days_%v_mm%
SET /a v_yy=%v_yy% - 1
SET /a v_mm=%v_mm% + 12
goto s_add_days_%v_mm%
:s_add_days_2
SET /a v_dd=%v_dd% + 28
SET /a v_leapyear=%v_yy% / 4
SET /a v_leapyear=%v_leapyear% * 4
if %v_leapyear% NEQ %v_yy% goto s_adjust_month_year
SET /a v_dd=%v_dd% + 1
goto s_adjust_month_year
:s_add_days_4
:s_add_days_6
:s_add_days_9
:s_add_days_11
SET /a v_dd=%v_dd% + 30
goto s_adjust_month_year
:s_add_days_1
:s_add_days_3
:s_add_days_5
:s_add_days_7
:s_add_days_8
:s_add_days_10
:s_add_days_12
SET /a v_dd=%v_dd% + 31
goto s_adjust_month_year
:s_add_up_days
:: add all days ::::::
SET /a v_dd=%v_dd% + %v_number_of_days%
:s_subtract_days_
goto s_subtract_days_%v_mm%
:s_adjust_mth_yr
SET /a v_mm=%v_mm% + 1
if %v_mm% LEQ 12 goto s_subtract_days_%v_mm%
SET /a v_yy=%v_yy% + 1
SET /a v_mm=%v_mm% - 12
goto s_subtract_days_%v_mm%
:s_subtract_days_2
SET /a v_leapyear=%v_yy% / 4
SET /a v_leapyear=%v_leapyear% * 4
If %v_leapyear% EQU %v_yy% goto s_subtract_leapyear
if %v_dd% LEQ 28 goto s_add_subtract_days_DONE
SET /a v_dd=%v_dd% - 28
goto s_adjust_mth_yr
:s_subtract_leapyear
if %v_dd% LEQ 29 goto s_add_subtract_days_DONE
SET /a v_dd=%v_dd% - 29
goto s_adjust_mth_yr
:s_subtract_days_4
:s_subtract_days_6
:s_subtract_days_9
:s_subtract_days_11
if %v_dd% LEQ 30 goto s_add_subtract_days_DONE
SET /a v_dd=%v_dd% - 30
goto s_adjust_mth_yr
:s_subtract_days_1
:s_subtract_days_3
:s_subtract_days_5
:s_subtract_days_7
:s_subtract_days_8
:s_subtract_days_10
:s_subtract_days_12
if %v_dd% LEQ 31 goto s_add_subtract_days_DONE
SET /a v_dd=%v_dd% - 31
goto s_adjust_mth_yr
:s_add_subtract_days_DONE
SET /a v_dd_int=%v_dd%
SET /a v_mm_int=%v_mm%
SET /a v_yy_int=%v_yy%
IF %v_dd% GTR 9 (SET v_dd_str=%v_dd%) ELSE (SET v_dd_str=0%v_dd%)
IF %v_mm% GTR 9 (SET v_mm_str=%v_mm%) ELSE (SET v_mm_str=0%v_mm%)
SET v_ymd_str=%v_yy%%v_mm_str%%v_dd_str%
ECHO DATEMATH - add or subtract days from a date = new date
goto s_end
::::::::::::::::::::::::::::::::::::::::::::::::::
:s_julian_day
SET v_year=%1
SET v_month=%2
SET v_day=%3
SET /a v_month=v_month
SET /a v_day=v_day
SET /A a = 14 - v_month
SET /A a /= 12
SET /A y = v_year + 4800 - a
SET /A m = v_month + 12 * a - 3
SET /A m = 153 * m + 2
SET /A m /= 5
SET /A v_JulianDay = v_day + m + 365 * y + y / 4 - y / 100 + y / 400 - 32045
ECHO The Julian Day is [%v_JulianDay%]
goto :eof
::::::::::::::::::::::::::::::::::::::::::::::::::
:s_end
ECHO ~~~~~~~~~~~~
ECHO [%v_ymd_str%] YY=[%v_yy_int%] MM=[%v_mm_str%] DD=[%v_dd_str%]
ECHO ~~~~~~~~~~~~
:s_end_days
ENDLOCAL&SET /a _yy_int=%v_yy_int%&SET /a _mm_int=%v_mm_int%&SET /a _dd_int=%v_dd_int%&SET _ymd_str=%v_ymd_str%&SET _mm_str=%v_mm_str%&SET _dd_str=%v_dd_str%

Can be done with adding jscript code to a batch file.
Here's the dayAdder.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 and output:
E:\scripts>dayAdder.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.

Related

Batch command - adding date at the end of folder

I currently run a batch command to create a folder 1 day in advanced and label it as MMDDYY.
Everything is working as intended except single digit days. Currently it named the next day folder has 12214, is it possible to have it name it as 120214?
#echo off
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%"
:loop
set /a DD+=1
if %DD% gtr 31 (
set DD=1
set /a MM+=1
if %MM% gtr 12 (
set MM=1
set /a YY+=1
set /a YYYY+=1
)
)
xcopy /d:%MM%-%DD%-%YYYY% /l . .. >nul 2>&1 || goto loop
echo %DD%/%MM%/%YYYY%
mkdir "C:\Users\Name\Desktop\%mm%%dd%%yy%\"
pause
You need to pad again the data once the operations have been done. Also you will need some more logic to handle the month change
#echo off
setlocal enableextensions disabledelayedexpansion
rem Retrieve data
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%"
rem Remove padding from date elements and increase day
set /a "y=%YYYY%", "m=100%MM% %% 100", "d=(100%DD% %% 100)+1"
rem Calculate month length
set /a "ml=30+((m+m/8) %% 2)" & if %m% equ 2 set /a "ml=ml-2+(3-y %% 4)/3-(99-y %% 100)/99+(399-y %% 400)/399"
rem Adjust day / month / year for tomorrow date
if %d% gtr %ml% set /a "d=1", "m=(m %% 12)+1", "y+=(%m%/12)"
rem Pad date elements and translate again to original variables
set /a "m+=100", "d+=100"
set "YYYY=%y%"
set "YY=%y:~-2%"
set "MM=%m:~-2%"
set "DD=%d:~-2%"
echo Tomorrow: %YYYY% / %MM% / %DD%
Just add the folder creation in the required format
Batch is cumbersome with date math. Leap years, month / year changes / etc can be a pain to deal with. I suggest using a JScript Date() object, where all such conversions are handled automatically.
As follows is a batch / JScript hybrid script. Save it with a .bat extension and run it as you are used to running your typical batch scripts.
#if (#a==#b) #end /* JScript ignores this multiline comment
:: batch portion
#echo off
setlocal
for /f "tokens=1-3" %%I in ('cscript /nologo /e:JScript "%~f0"') do (
set "MM=%%I"
set "DD=%%J"
set "YYYY=%%K"
)
xcopy /d:%MM%-%DD%-%YYYY% /l . .. >nul 2>&1 || goto loop
echo %MM%/%DD%/%YYYY%
mkdir "%userprofile%\Desktop\%MM%%DD%%YYYY:~-2%\"
pause
goto :EOF
:: end batch portion / begin JScript */
function zeroPad(what) { return (what+'').length < 2 ? '0'+what : what; }
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
WSH.Echo([
zeroPad(tomorrow.getMonth() + 1),
zeroPad(tomorrow.getDate()),
tomorrow.getFullYear()
].join(' '));

How to get yesterday's date in batch file? [duplicate]

This question already has answers here:
how to get yesterday's date in a batch file
(4 answers)
Closed 8 years ago.
I use below code to get today's date and month (0411)
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set FileNameDatePrefix=%%a%%b)
But I want to get yesterday's date in FileNameDatePrefix.
I am not sure how we can do this. Any suggestions are welcome.
Here is a batch file method using VBS which is robust in any locale
:: yesterdays date
#echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "data=%yyyy%-%mm%-%dd%"
echo Yesterday was "%data%"
pause
Here is one way:
#echo off
setlocal
Call :GetDateTime Year Month Day
Echo %Year% %Month% %Day%
Call :AddSubtractDate %Year% %Month% %Day% -1 Ret
echo %Ret%
exit /b
:AddSubtractDate 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=%YYYY: =%%MM: =%%DD: =%
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
This code will only work on Vista and later, as robocopy is used to retrieve the date of today. But will avoid the problem of locale formats in the %date% variable, the administrator requirement of wmic and the temporary file of VBScript. Anyway, the subroutine that gets the today date can be replaced with any other code. The :getYesterdayDate will handle all the calcs to substract one day from today.
#echo off
setlocal enableextensions enabledelayedexpansion
call :getYesterdayDate yesterday
echo %yesterday%
exit /b
:getYesterdayDate returnVar
setlocal enableextensions disabledelayedexpansion
call :getTodayDate today
for /f "tokens=1-3 delims=/ " %%a in ("%today%") do set /a "y=%%a", "m=1%%b-100", "d=1%%c-100"
if %d% gtr 1 ( set /a "d-=1" ) else (
if %m% equ 1 ( set /a "y-=1" , "m=12" ) else ( set /a "m-=1" )
set /a "d=30+((m+m/8) %% 2)"
if %m%==3 set /a "d=d-2+!(y%%4)-!(y%%100)+!(y%%400)"
)
set "d=0%d%" & set "m=0%m%"
endlocal & set "%~1=%y%/%m:~-2%/%d:~-2%" & exit /b
:getTodayDate returnVar
setlocal enableextensions disabledelayedexpansion
set "today=" & for /f %%a in ('robocopy "|" . /njh') do if not defined today set "today=%%a"
endlocal & set "%~1=%today%" & exit /b
You could try this:
#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 %IISDT%
echo %AWSDT%
pause
Source http://www.powercram.com/2010/07/get-yesterdays-date-in-ms-dos-batch.html

Currentdate and Nearest date

How do i make a batch file that is echo-ing out the nearest date from a text file?
For example a test file called test2.txt, in text2.text you can see this:
11.12.2013 historie (kapittel1, kapittel 2 og kapittel 3).
21.12.2013 matte (kapittelq1, kapittel 2 og kapiwettel 3).
01.12.2013 naturfag (kapittel1q, kapittel 2 og kapiwettel 3).
16.12.2013 example (kapittelq1, kapittel 2 og kapittelwe 3).
12.10.2013 example 2(kapittel1w, kapittel 2 og kapittweel 3).
then it would echo out after analyzing the file, and echoing out the nearest date ( from current date) like this:
"
The Next test is X days (12.10.2013) and the subject is (example2, and the topics is (kapittel1w, kapittel 2 og kapittweel 3).
this means it is: X hours, X minutes, X secounds *( countdown) *
"
How do i do this?
with :
11.12.2013 historie (kapittel1, kapittel 2 og kapittel 3).
21.12.2013 matte (kapittelq1, kapittel 2 og kapiwettel 3).
01.12.2013 naturfag (kapittel1q, kapittel 2 og kapiwettel 3).
16.12.2013 example (kapittelq1, kapittel 2 og kapittelwe 3).
12.10.2013 example 2(kapittel1w, kapittel 2 og kapittweel 3).
result is 20131012
#echo off &setlocal EnableDelayedExpansion
set "file_path=.\test2testtesttest.TXT"
if not exist "!file_path!" echo File "!file_path!" Does Not Exist >&2 & exit /b 2
for /f %%F in ("!file_path!") do set file_path=%%~sfF
copy nul "%TEMP%\~.ddf" >nul
:: date function provided by carlos
:: http://www.dostips.com/forum/viewtopic.php?f=3&t=4555&start=15
makecab /D RptFileName="%TEMP%\~.rpt" /D InfFileName="%TEMP%\~.inf" -f "%TEMP%\~.ddf">nul
for /f "tokens=3-7" %%a in ('type "%TEMP%\~.rpt"') do (
if not defined current-date set "current-date=%%e-%%b-%%c"
if not defined current-time set "current-time=%%d"
if not defined weekday set "weekday=%%a"
)
del /q "%TEMP%\~.*"
rem echo %weekday% %current-date% %current-time%
set Jan=01
set Feb=02
set Mar=03
set Apr=04
set May=05
set Jun=06
set Jul=07
set Aug=08
set Sep=09
Set Oct=10
set Nov=11
set Dec=12
for /F "tokens=1,2,3 delims=-" %%A in ("%current-date%") do (
set comparable_date=%%A!%%B!%%C
)
rem echo %comparable_date%
set /a nearest_date=99999999
for /F "tokens=1,2,3 delims=. " %%A in (%file_path%) do (
set /a possible_nearest_date=%%C%%B%%A
set /a old_result=!nearest_date!-comparable_date
set /a new_result=!possible_nearest_date!-comparable_date
if !new_result! LSS !old_result! set /a nearest_date=!possible_nearest_date!
)
if !nearest_date! EQU 99999999 echo something wrong with the file>&2 && endlocal && exit /b 3
set nearest_date=!nearest_date:~-2!.!nearest_date:~4,2!.!nearest_date:~0,4!
echo(
echo(
rem type %file_path%
for /f "tokens=1,2* delims=()" %%L in ('type %file_path%^|find "!nearest_date!"') do (
for /f "tokens=1,2" %%t in ("%%L") do (
echo The Next Test is on (!nearest_date!^) and subject is ( %%u with topics %%M^)
)
)
echo(
echo(
endlocal
Condensed get next event part:
FOR /F "TOKENS=1,* DELIMS==" %%c IN ('WMIC OS GET /FORMAT:VALUE') DO IF /I "%%c" == "LocalDateTime" SET CurrentDate=%%d
SET CurrentDate=%CurrentDate:~0,8%
SET NextDate=99999999
FOR /F "TOKENS=1,2,3,* DELIMS=. " %%i IN (text2.txt) DO IF %%k%%j%%i GTR %CurrentDate% FOR /F %%r IN ('SET /A NextDate-=%%k%%j%%i') DO IF %%r GTR 0 (
SET NextDate=%%k%%j%%i
SET NextSubject=%%l
)
ECHO The Next test is (%NextDate:~6,2%.%NextDate:~4,2%.%NextDate:~0,4%) and the subject is %NextSubject%.
Count down timer:
This is a hard part. I already did date logic in batch (as in here), but it is very time consuming.
Use VBScripting alternative or create a small program to make this hard work.
EDIT: As #npocmaka noticed WMIC is not standard in all Windows versions, I'm adding solutions for both parts using VBS.
Batch file:
FOR /F "TOKENS=*" %%d IN ('CSCRIPT /NOLOGO Now.vbs') DO SET CurrentDate=%%d
SET NextDate=99999999
FOR /F "TOKENS=1,2,3,* DELIMS=. " %%i IN (text2.txt) DO IF "%%k %%j %%i" GTR "%CurrentDate%" FOR /F %%r IN ('SET /A NextDate-=%%k%%j%%i') DO IF %%r GTR 0 (
SET NextDate=%%k%%j%%i
SET "NextSubject=%%l"
)
FOR /F "TOKENS=1,2,3,4" %%d IN ('CSCRIPT /NOLOGO DateDiff.vbs %NextDate:~0,4% %NextDate:~4,2% %NextDate:~6,2% 00 00 00 %CurrentDate%') DO ECHO The Next test is %%d days (%NextDate:~6,2%.%NextDate:~4,2%.%NextDate:~0,4%) and the subject is %NextSubject%.
FOR /F "TOKENS=1,2,3,4" %%d IN ('CSCRIPT /NOLOGO DateDiff.vbs %NextDate:~0,4% %NextDate:~4,2% %NextDate:~6,2% 00 00 00 %CurrentDate%') DO ECHO this means it is: %%e hours, %%f minutes, %%g seconds
Now.vbs:
d=Now
WScript.Echo Right("000"&Year(d),4)&" "&Right("0"&Month(d),2)&" "&Right("0"&Day(d),2)&" "&Right("0"&Hour(d),2)&" "&Right("0"&Minute(d),2)&" "&Right("0"&Second(d),2)
DateDiff.vbs:
If WScript.Arguments.Count <> 12 Then
WScript.Echo "Syntax: DAYSDIFF yyyy mm dd hh mm ss yyyy mm dd hh mm ss"
Else
d=DateSerial(WScript.Arguments(0),WScript.Arguments(1),WScript.Arguments(2))+TimeSerial(WScript.Arguments(3),WScript.Arguments(4),WScript.Arguments(5))-DateSerial(WScript.Arguments(6),WScript.Arguments(7),WScript.Arguments(8))-TimeSerial(WScript.Arguments(9),WScript.Arguments(10),WScript.Arguments(11))
WScript.Echo Cdbl(Fix(d))&" "&Right("0"&Hour(d),2)&" "&Right("0"&Minute(d),2)&" "&Right("0"&Second(d),2)
End IF

Tomorrows date with leading zeros?

I'm looking to have a method of printing tomorrows date in a DD/MM/YYYY format. I'm currently running the following set of cmds:
#echo off
set /a d=%date:~0,2%
set /a m=%date:~3,2%
set /a y=%date:~6,4%
:loop
set /a d+=1
if %d% gtr 31 (
set d=1
set /a m+=1
if %m% gtr 12 (
set m=1
set /a y+=1
)
)
xcopy /d:%m%-%d%-%y% /h /l "%~f0" "%~f0\" >nul 2>&1 || goto loop
echo The date tomorrow is "%d%/%m%/%y%".
pause
The above works nicely, printing "The date tomorrow is "8/12/2012". However, I need both my DAY and MONTH values to come out with leading zeros when less than 10. I can't appear to figure out how this can be done. Could anyone help me?
Cheers,
EL
Your grabbing the wrong information for the sets for day/month/year.
Here are the correct sets
set /a d=%date:~7,2%
set /a m=%date:~4,2%
set /a y=%date:~10,4%
you should be able to easily figure out what variables go where by the following:
#echo off
:testing
set /a a=%date:~0,1%
set /a b=%date:~1,1%
set /a c=%date:~2,1%
set /a d=%date:~3,1%
set /a e=%date:~4,1%
set /a f=%date:~5,1%
set /a g=%date:~6,1%
set /a h=date:~7,1%
set /a i=%date:~8,1%
set /a j=%date:~9,1%
set /a k=%date:~10,1%
set /a l=%date:~11,1%
set /a m=%date:~12,1%
set /a n=%date:~13,1%
echo.%a%-a
echo.%b%-b
echo.%c%-c
echo.%d%-d
echo.%e%-e
echo.%f%-f
echo.%g%-g
echo.%h%-h
echo.%i%-i
echo.%j%-j
echo.%k%-k
echo.%l%-l
echo.%m%-m
echo.%n%-n
then when you get the first value you want it will be
set /a var=%date:~NUM1,NUM2%
Where NUM1 is the start NUM2 is how many characters to move to the right and VAR is the variable you want to set it to.
Alternatively using FOR statements might work better.
I saw that someone had mentioned that the user might be from EU or British. If that is the case their date setup would be (using FOR):
#ECHO OFF
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
set day=%dd%
set /a day+=01
SET date=%day%/%mm%/%yyyy%
echo.%date%
pause
Opps. I think I've just fixed my own problem by adding:
IF 1%d% LSS 100 SET d=0%d%
IF 1%m% LSS 100 SET m=0%m%
Before xcopy. So it should now read:
#echo off
set /a d=%date:~0,2%
set /a m=%date:~3,2%
set /a y=%date:~6,4%
:loop
set /a d+=1
if %d% gtr 31 (
set d=1
set /a m+=1
if %m% gtr 12 (
set m=1
set /a y+=1
)
)
IF 1%d% LSS 100 SET d=0%d%
IF 1%m% LSS 100 SET m=0%m%
xcopy /d:%m%-%d%-%y% /h /l "%~f0" "%~f0\" >nul 2>&1 || goto loop
echo The date tomorrow is "%d%/%m%/%y%".
pause
Hope that helps anyone with a similar issue :)
I tested your code... it did not work
Here is a proper portion of batch code that will work for what you need.
:getdate
set /a day=%date:~7,2%
IF %day% LSS 10 set day=0%day%
rem echo.The day is - %day%
set /a month=%date:~4,2%
IF %month% LSS 10 set month=0%month%
rem echo.The month is - %month%
set /a year=%date:~10,4%
rem echo.The year is - %year%
REM Setting Month / Days
set jan=31
set feb=28
set mar=31
set apr=30
set may=31
set jun=30
set jul=31
set aug=31
set sep=30
set oct=31
set nov=30
set dec=31
REM If Months are # set Month to Days
IF %month% EQU 1 set mon=%jan%
IF %month% EQU 2 set mon=%feb%
IF %month% EQU 3 set mon=%mar%
IF %month% EQU 4 set mon=%apr%
IF %month% EQU 5 set mon=%may%
IF %month% EQU 6 set mon=%jun%
IF %month% EQU 7 set mon=%jul%
IF %month% EQU 8 set mon=%aug%
IF %month% EQU 9 set mon=%sep%
IF %month% EQU 10 set mon=%oct%
IF %month% EQU 11 set mon=%nov%
IF %month% EQU 12 set mon=%dec%
echo.Today's Date is %month%/%day%/%year%
set /a day+=1
if %day% GTR %mon% set /a month+=1
if %day% GTR %mon% set day=1
IF %day% LSS 10 set day=0%day%
IF %month% GTR 12 set month=1& set /a year+=1
IF %month% LSS 10 set month=0%month%
echo.Tomorrow's Date is %month%/%day%/%year%
This will set the days in a month (except leap years) and should solve any problems you have, assuming you don't know this yet, but if a month only has 30 days your current code will set the day to 31, regardless if it's say Feburary and there are only 28 days.
You will have to insert your other code into here, as I'm not sure what your loop is doing, otherwise I just changed the d = day, m = month and y = year (mon is comparing the days in the month).
Good luck, HTH

batch file replace date with yesterday date

i working on a script that detect weird date format in the raw file and replace them with yesterday date.
here the raw data
assuming yesterday date is 2012-07-19
Input.txt
AAAAAAAA 0001-01-01-00.00.00.000000 Change Password RSO part
BBBBBBBB 0001-01-01-00.00.00.000000 Change Password RSO part
CCCCCCC 0001-01-01-00.00.00.000000 Set Nbr try of password
DDDDDDD 2012-07-19-09.44.25.634000 Change Password
output.txt
AAAAAAAA 2012-07-19-00.00.00.000000 Change Password RSO part
BBBBBBBB 2012-07-19-00.00.00.000000 Change Password RSO part
CCCCCCC 2012-07-19-00.00.00.000000 Set Nbr try of password
DDDDDDD 2012-07-19-09.44.25.634000 Change Password
I am new to dos batch file. However, come out a basic strategy which i will first loop through each line to detect this scenario (0001-01-01) in fact there is only this scenario
I then replace the date to yesterday.
i have try to write the code as follow
REM Code to Find YEsterday's date
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
call :process <..\input.txt>> ..\output.txt
:process
set line=
if "!line:~12,22!" neq "0001-01-01" goto process
## for /F between line:~12,22 do (
------------------------------------------------
this part onward i not very sure already.
intend to do a for loop each line replace "0001-01-01" to %yyyy%-%mm%-%dd%
)
anyone has idea please help!!
thanks
Read HELP FOR and HELP SET; then try this code to get you started...
#echo off
setlocal enabledelayedexpansion
set newdate=9999-99-99
for /f "tokens=*" %%a in (input.txt) do (
set line=%%a
set onlydate=!line:~12,10!
if !onlydate!==0001-01-01 (
echo !line:0001-01-01=%newdate%!
) else (
echo !line!
)
)
endlocal
The FOR iterates over all the lines in input.txt. Then, for each line, it extracts the date substring at position 12, and checks if the date is 0001-01-01, in that case, it substitutes it with the contents of newdate variable.