I am trying to write a batch script to detect for missing files from a list of files on Windows. Given the format of the file names to be "day_month_date_hh_mm_00_yyyy.enf", and the hours of the files will be different, I have to identify if there's a file of a particular hour is missing.
I have written down the following to find out the number of days in a given year and month.
set /p m="Enter month: "
set /p y="Enter year: "
REM call :DaysOfMonth %y% %m%
setlocal DisableDelayedExpansion
set /a "yy = %y%, mm = 100%m% %% 100"
set /a "n = 30 + !(((mm & 9) + 6) %% 7) + !(mm ^ 2) * (!(yy %% 4) - !(yy %% 100) + !(yy %% 400) - 2)"
set maxDay=%n%
echo Days Of Month: %maxDay%
However, I have no idea how to generate the Day of a date when I run the loop to check for missing files. I have search abit on the net and combined a few codes which will definitely not work as I am still very new to batch. Below is the part of the script where I am stuck at and need help on.
for /L %%d in (1,1,%maxDay%) do (
for /L %%f in (0,1,%max%) do (
if %%f < 10 (
set hour=0%%f
) else (
set hour=%%f
)
set "num=%%Day_%m%_%%d_%hour%_00_00_%y%"
if not exist "num.enf" (
echo num.enf
set /A "cnt=!cnt!+1"
)
)
)
NUMBER MISSING: !cnt!
The full script is as per below:
#echo off
set max=24
set cnt=0
set /p m="Enter month: "
set /p y="Enter year: "
setlocal DisableDelayedExpansion
set /a "yy = %y%, mm = 100%m% %% 100"
set /a "n = 30 + !(((mm & 9) + 6) %% 7) + !(mm ^ 2) * (!(yy %% 4) - !(yy %% 100) + !(yy %% 400) - 2)"
set maxDay=%n%
echo Days Of Month: %maxDay%
setlocal ENABLEDELAYEDEXPANSION
pause
for /L %%d in (1,1,%maxDay%) do (
for /L %%f in (0,1,%max%) do (
if %%f < 10 (
set hour=0%%f
) else (
set hour=%%f
)
set "num=%%Day_%m%_%%d_%hour%_00_00_%y%"
if not exist "num.enf" (
echo num.enf
set /A "cnt=!cnt!+1"
)
)
)
NUMBER MISSING: !cnt!
endlocal &exit /b %n%
Please help advise me how do I rectify the errors in the script and how to generate the day of the specified date in the loop to identify missing file. Thanks in advance!
ok, so I just made some amendments to your code to get this to work. It is not the fastest as we get the date for each file by running a powershell command, but it will do the job.
I am sure I can write something a little better and quicker, but I would need to find some time.
#echo off
set max=24
set cnt=0
set /p m="Enter month: "
set /p y="Enter year: "
set /a "yy = %y%, mm = 100%m% %% 100"
set /a "n = 30 + !(((mm & 9) + 6) %% 7) + !(mm ^ 2) * (!(yy %% 4) - !(yy %% 100) + !(yy %% 400) - 2)"
set maxDay=%n%
echo Days Of Month: %maxDay%
setlocal enabledelayedexpansion
pause
for /L %%d in (1,1,%maxDay%) do (
if %%d leq 9 set "day=0%%d"
if %%d gtr 9 set "day=%%d"
for /L %%f in (0,1,%max%) do (
if %%f leq 9 set hour=0%%f
if %%f gtr 9 set hour=%%f
for /f "tokens=1*" %%i in ('PowerShell -Command "& {Get-Date "%m%/!day!/%y%" -format "ddd_MMMM_dd"}"') do (
if not exist "%%i_!hour!_??_%y%.enf" (
echo Missing: %%i_!hour!_00_%y%.enf
)
)
)
)
NUMBER MISSING: !cnt!
exit /b %n%
Note
I am testing:
if not exist "%%i_!hour!_??_%y%.enf" (..
The ?? will test for any minute. So whether a file exists as:
Wed_January_01_05_00_2020.enf or Wed_January_01_05_13_2020.enf
It will detect a file for the specific hour.. If that is not what you intend to do, then just revert back to 00 but there is a risk of a file arriving at 01 instead.
Related
I am trying to use For Loops that will start and end based on values entered by user in batch. However, when using the user input variables, the loop fail to work. May I know what have I done wrong and how do I correct it? Thanks in advance!
P.S. I have tried both ! and %
Here's the my source code which shows the for loop which is having problem:
setlocal EnableDelayedExpansion
set /p sy="Enter start year (e.g. 2020): "
set /p sm="Enter start month (e.g. 08): "
set /p ey="Enter end year (e.g. 2020): "
set /p em="Enter end month (e.g. 08): "
echo ***LIST OF MISSING FILES FROM %sm% %sy% to %em% %ey%*** > checkFile_%SUBFILENAME%.txt
for /L %%y in (%sy%,1,%ey%) do (
for /L %%m in (1,1,%maxMonth%) do (
if %%m leq 9 set month=0%%m
if %%m gtr 9 set month=%%m
if %%y equ %sy% if !month! leq %sm% (
goto :continue
)
if %%y equ %ey% if !month! equ %em% (
goto :breakloop
) else (
call :DaysOfMonth %%y !month!
set maxDay=!errorlevel!
echo Days Of Month !month!: !maxDay!
Here's the full script:
setlocal EnableDelayedExpansion
set maxHour=23
set maxMonth=12
set missing=0
set /p sy="Enter start year (e.g. 2020): "
set /p sm="Enter start month (e.g. 08): "
set /p ey="Enter end year (e.g. 2020): "
set /p em="Enter end month (e.g. 08): "
set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
set CUR_HH=%time:~0,2%
if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%)
set CUR_NN=%time:~3,2%
set CUR_SS=%time:~6,2%
set CUR_MS=%time:~9,2%
set SUBFILENAME=%CUR_YYYY%%CUR_MM%%CUR_DD%-%CUR_HH%%CUR_NN%%CUR_SS%
echo ***LIST OF MISSING FILES FROM %sm% %sy% to %em% %ey%*** > checkFile_%SUBFILENAME%.txt
for /L %%y in (%sy%,1,%ey%) do (
for /L %%m in (1,1,%maxMonth%) do (
if %%m leq 9 set month=0%%m
if %%m gtr 9 set month=%%m
if %%y equ %sy% if !month! leq %sm% (
goto :continue
)
if %%y equ %ey% if !month! equ %em% (
goto :breakloop
) else (
call :DaysOfMonth %%y !month!
set maxDay=!errorlevel!
echo Days Of Month !month!: !maxDay!
set missingMonth=0
for /L %%d in (1,1,!maxDay!) do (
if %%d leq 9 set day=0%%d
if %%d gtr 9 set day=%%d
for /f "tokens=1*" %%i in ('PowerShell -Command "& {Get-Date "!day!/!month!/%%y" -format "ddd_MMM_dd"}"') do (
for /L %%f in (0,1,!maxHour!) do (
if %%f leq 9 set hour=0%%f
if %%f gtr 9 set hour=%%f
if not exist "%%i_!hour!_00_00_%%y.enf" (
echo Missing: %%i_!hour!_00_00_%%y.enf
echo Missing: %%i_!hour!_00_00_%%y.enf >> checkFile_%SUBFILENAME%.txt
set /A missing = missing+1
set /A missingMonth = missingMonth+1
)
)
)
)
)
echo NUMBER OF FILES MISSING FOR !month!: !missingMonth!
echo ***NUMBER OF FILES MISSING FOR !month!: !missingMonth!*** >> checkFile_%SUBFILENAME%.txt
echo. >> checkFile_%SUBFILENAME%.txt
:continue
)
)
:breakloop
echo TOTAL NUMBER OF FILES MISSING FROM %sm% %sy% to %em% %ey%: %missing%
echo. >> checkFile_%SUBFILENAME%.txt
echo ***TOTAL NUMBER OF FILES MISSING FROM %sm% %sy% to %em% %ey%*** >> checkFile_%SUBFILENAME%.txt
pause
:DaysOfMonth Year Month
setlocal DisableDelayedExpansion
set /a "yy = %~1, mm = 100%~2 %% 100"
set /a "n = 30 + !(((mm & 9) + 6) %% 7) + !(mm ^ 2) * (!(yy %% 4) - !(yy %% 100) + !(yy %% 400) - 2)"
endlocal &exit /b %n%
I am creating a batch file to execute some exe file. In that exe i need to specify first date(mm/dd/yyyy) and last day(mm/dd/yyyy) of last month.
#echo off
start "Testing" "c:\Program Files\app-cmd\bin\admincmd\imagelist -d 08/01/2015 -e 01/09/2015 > c:\test.txt"
Imagelist is an exe file where I need to pass value in -d and -e parameter.
I will add this script in schedule task so, it will run as schedule.
I can't use powershell as machine is win server 2003.
Please advise.
Assuming the date format on your system is MM/DD/YYYY as shown by date /t command:
#echo off
setlocal enableDelayedExpansion
for /f "delims=/ tokens=1,3" %%a in ("%date%") do set month=%%a& set year=%%b
if %month:~0,1%==0 set month=%month:~1%
set /a month-=1 && if !month!==0 set month=12&set /a year-=1
if not !month!==2 (
set /a last_day="31 - (month - 1) %% 7 %% 2"
) else (
set /a y4="year %% 4" & if !y4!==0 (
set /a y100="year %% 100" & if not !y100!==0 set is_leap_year=1
set /a y400="year %% 400" & if !y400!==0 set is_leap_year=1
)
if "!is_leap_year!"=="1" (set last_day=29) else set last_day=28
)
set month=0!month!
start "Testing" "c:\Program Files\app-cmd\bin\admincmd\imagelist" ^
-d !month:~-2!/01/!year! -e !month:~-2!/!last_day!/!year! > c:\test.txt
The code detects leap years as (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) for February and uses 31 - (month - 1) % 7 % 2 for other months.
You can try with
#echo off
setlocal enableextensions
call :getLastMonthLimits start end
echo %start% - %end%
goto :eof
:getLastMonthLimits startDateReturnVar endDateReturnVar
setlocal enableextensions disabledelayedexpansion
for %%t in ("%temp%\%~nx0.%random%%random%%random%.rpt") do (
for /f "tokens=4,7" %%a in ('
^>nul makecab /f nul /d RptFileName^="%%~ft" /V0
^& type "%%~ft" ^| find "MakeCAB Report:"
') do (
set /a "jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,nov=11,dec=12"
set /a "m=(%%a+10) %% 12 + 1", "y=%%b-m/12", "d=30+((m+m/8) %% 2)", "m+=100"
if /i %%a==mar set /a "d=d-2 +(3-y %% 4)/3 -(99-y %% 100)/99 +(399-y %% 400)/399"
set /a "d+=100"
)
) & del "%%~ft"
endlocal & set "%~1=%m:~-2%/01/%y%" & set "%~2=%m:~-2%/%d:~-2%/%y%"
goto :eof
This uses a simplified version of the getDate function by carlos to get the date in a locale independent format (I prefer to use robocopy for it, but it is not a native tool in 2003). The rest are just arithmetic operations to calculate the month limits.
edited For the robocopy version (instead of the makecab to retrieve the date in locale independent format), just in case someone could need it
#echo off
call :getLastMonthLimits start end
echo %start% - %end%
goto :eof
:getLastMonthLimits startDateReturnVar endDateReturnVar
setlocal enableextensions disabledelayedexpansion
for /f "tokens=1-2 delims=/ " %%a in ('robocopy "|" . /njh ^| find "|"') do (
set /a "m=((1%%b-100)+10) %% 12 + 1", "y=%%a-m/12", "d=30+((m+m/8) %% 2)", "m+=100"
if %%b==03 set /a "d=d-2 +(3-y %% 4)/3 -(99-y %% 100)/99 +(399-y %% 400)/399"
set /a "d+=100"
)
endlocal & set "%~1=%m:~-2%/01/%y%" & set "%~2=%m:~-2%/%d:~-2%/%y%"
goto :eof
How can I get the day of year from the current date in a Windows batch file?
I have tried
SET /A dayofyear=(%Date:~0,2%*30.5)+%Date:~3,2%
But it does not work with leap years, and it is always off by a few days. I would not like to use any third-party executables.
If you want the Julian Day Number, you may use the method posted in my accepted answer given at previous link. However, the "day of year" is just a number between 1 and 365 (366 for leap years). The Batch file below correctly calculate it:
#echo off
setlocal EnableDelayedExpansion
set /A i=0, sum=0
for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) do (
set /A i+=1
set /A accum[!i!]=sum, sum+=%%a
)
set /A month=1%Date:~0,2%-100, day=1%Date:~3,2%-100, yearMOD4=%Date:~6,4% %% 4
set /A dayOfYear=!accum[%month%]!+day
if %yearMOD4% equ 0 if %month% gtr 2 set /A dayOfYear+=1
echo %dayOfYear%
Note: This relies on the date format MM/DD/YYYY.
EDIT 2020/08/10: Better method added
I modified the method so it now uses wmic to get the date. The new method is also shorten, but no simpler! ;):
#echo off
setlocal
set "daysPerMonth=0 31 28 31 30 31 30 31 31 30 31 30"
for /F "tokens=1-3" %%a in ('wmic Path Win32_LocalTime Get Day^,Month^,Year') do (
set /A "dayOfYear=%%a, month=%%b, leap=!(%%c%%4)*(((month-3)>>31)+1)" 2>NUL
)
set /A "i=1, dayOfYear+=%daysPerMonth: =+(((month-(i+=1))>>31)+1)*%+leap"
echo %dayOfYear%
#Aacini's answer has got two weak points (although it suffices for many applications, and it is a great approach after all!):
the retrieved system date relies on format MM/DD/YYYY, but %Date% is locale-dependent;
the calculation does not fully comply with the definition of leap years (see this article);
The following is a revised version of #Aacini's batch script (described within explanatory remarks):
#echo off
setlocal EnableDelayedExpansion
set /A i=0, sum=0
rem accumulate day-offsets for every month in %accum[1...12]%
for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) do (
set /A i+=1
set /A accum[!i!]=sum, sum+=%%a
)
rem check for availability of alternative date value given via (1st) command line argument,
rem just for convenient batch testing
if "%1"=="" (
rem WMIC retrieves current system date/time in standardised format;
rem parse its output by FOR /F, then store it in %CurrDate%
for /F "tokens=2 delims==" %%D in ('wmic OS GET LocalDateTime /VALUE ^| find "="') do (
set CurrDate=%%D
)
) else (
rem apply date value from command line in YYYYMMDD format (not checked for validity)
set CurrDate=%1
)
rem extract %month% and %day%;
set /A month=1%CurrDate:~4,2%-100, day=1%CurrDate:~6,2%-100
rem compute several moduli needed for determining whether year is leap year
set /A yearMOD4=%CurrDate:~0,4% %% 4
set /A yearMOD100=%CurrDate:~0,4% %% 100, yearMOD400=%CurrDate:~0,4% %% 400
rem calculate %dayOfYear% as it were not a leap year
set /A dayOfYear=!accum[%month%]!+day
rem adapt %dayOfYear% only in case %month% is past February (29th)
if %month% gtr 2 (
rem check for leap year and adapt %dayOfYear% accordingly
if %yearMOD4% equ 0 set /A dayOfYear+=1
if %yearMOD400% neq 0 if %yearMOD100% equ 0 set /A dayOfYear-=1
)
rem compound statement to let %dayOfYear% survive SETLOCAL/ENDLOCAL block
endlocal & set dayOfYear=%dayOfYear%
rem return result
echo %dayOfYear%
This use the laps years in editable in loop | for /l %%L in (2020 4 2100) | < or more!
#echo off & setlocal enabledelayedexpansion && set "_cmd=Get Day^,Month^,Year^"
for /l %%L in (2020 4 2100)do set "_array_leap_year_=!_array_leap_year_!%%L,"
for /f "tokens=1-3delims= " %%a in ('wmic Path Win32_LocalTime !_cmd! ^| findstr /r "[0-9]"')do (
set "_yy=%%c" & set "_mm=0%%b" & set "_dd=0%%a" && set "_mm=!_mm:~-2!" & set "_dd=!_dd:~-2!" & set _date=!_yy!_!_mm!_!_dd!)
echo/!_array_leap_year_!|findstr /lic:"!_date:~0,4!," >nul&&(set "_leap_=29" & set "_year_=366")||(set "_leap_=28" & set "_year_=365")
set "_mm_dd_year_=01-31,02-!_leap_!,03-31,04-30,05-31,06-30,07-31,08-31,09-30,10-31,11-30,12-31" && set /a "_cnt=0" & set /a "_loop=!_mm! * 6"
(for /l %%d in (0 6 !_loop!)do set "_sum=!_mm_dd_year_:~%%d,5!" & if "9!_sum:~,2!" lss "9!_mm!" set /a "_day_year_+=!_sum:~-2!")
set /a "_day_year_+=!_dd!" && set /a "_remain=!_day_year_! - !_year_!" && echo/Today: !_date! ^| Day of Year: !_day_year_! ^| Days Remaining: !_remain:-=!
| Result | Today: 2019_04_21 | Day of Year: 111 | Days Remaining: 254
Conventional formatting:
#echo off & setlocal enabledelayedexpansion
set "_cmd=Get Day^,Month^,Year^"
for /l %%L in (2020 4 2100)do set "_array_leap_year_=!_array_leap_year_!%%L,"
for /f "tokens=1-3delims= " %%a in ('wmic Path Win32_LocalTime !_cmd! ^| findstr /r "[0-9]"')do (
set "_yy=%%c"
set "_mm=0%%b"
set "_dd=0%%a"
set "_mm=!_mm:~-2!"
set "_dd=!_dd:~-2!"
set _date=!_yy!_!_mm!_!_dd!
)
echo/!_array_leap_year_!|findstr /lic:"!_date:~0,4!," >nul && (
set "_leap_=29" & set "_year_=366" )||( set "_leap_=28" & set "_year_=365" )
set "_mm_dd_year_=01-31,02-!_leap_!,03-31,04-30,05-31,06-30,07-31,08-31,09-30,10-31,11-30,12-31"
set /a "_loop=!_mm! * 6"
for /l %%d in (0 6 !_loop!)do set "_sum=!_mm_dd_year_:~%%d,5!" && (
if "9!_sum:~,2!" lss "9!_mm!" set /a "_day_year_+=!_sum:~-2!" )
set /a "_day_year_+=!_dd!"
set /a "_remain=!_day_year_! - !_year_!"
echo/Today: !_date! ^| Day of Year: !_day_year_! ^| Days Remaining: !_remain:-=!
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
I would like to set the date in a Windows batch file to 7 days ago from today. I would like to do this in the following format.
set today=%date:~10,4%-%date:~4,2%-%date:~-7,2%
Any ideas how to subract the 7 day time delta here ?
I posted the description below in some site some time ago:
The following Batch files convert from Date to Julian Day Number an viceversa:
DATETOJULIAN.BAT:
#ECHO OFF
REM CONVERT DATE TO JULIAN DAY NUMBER
REM ANTONIO PEREZ AYALA
REM GET MONTH, DAY, YEAR VALUES
FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ("%1") DO SET MM=%%A& SET DD=%%B& SET YY=%%C
REM ELIMINATE LEFT ZEROS
SET /A DD=10%DD% %% 100, MM=10%MM% %% 100
REM CALCULATE JULIAN DAY NUMBER
IF %MM% LSS 3 SET /A MM+=12, YY-=1
SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10, JDN=C+DD+E+F-1524
JULIANTODATE.BAT:
REM CONVERT JULIAN DAY NUMBER TO MONTH, DAY, YEAR
REM ANTONIO PEREZ AYALA
SET /A W=(%1*100-186721625)/3652425, X=W/4, A=%1+1+W-X, B=A+1524, C=(B*100-12210)/36525, D=36525*C/100, E=(B-D)*10000/306001, F=306001*E/10000, DD=B-D-F, MM=E-1, YY=C-4716
IF %MM% GTR 12 SET /A MM-=12, YY+=1
REM INSERT LEFT ZEROS, IF NEEDED
IF %DD% LSS 10 SET DD=0%DD%
IF %MM% LSS 10 SET MM=0%MM%
REM SHOW THE DATE
ECHO %MM%/%DD%/%YY%
This way, to add/subtract a number of days to a date use the following lines:
CALL DATETOJULIAN %DATE%
SET /A NEWDATE=JDN+DAYS
CALL JULIANTODATE %NEWDATE%
Regards...
Reference: http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html
You just need to adjust your date format if it is not MM/DD/YYYY.
AdamEstrada asked about subtracting dates.
I had a tough time subtracting two Julian dates because of the SETLOCAL in the Julian functions. I did it by calling a function.
call:sub_Julians !Julian! %Today_Julian%
:sub_Julians
set /a delta_dates=%~1-%~2
...
goto:eof ::end:age_of_EPSdate
Ok, I needed a batch file to display the current JDAY for an DoD operations center. You can double-click the file and it will display in a CMD window. Then, press any key to exit.
Here's what I came up with:
#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%"
:: Call the day ordinal number subroutine
call :JDdayNumber %DD% %MM% %YYYY% DayOrdinalNumber
:: Display the result
echo.
echo Today is JDay %DayOrdinalNumber%
echo.
pause,
endlocal & goto :EOF
:: set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
:: set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
:: echo datestamp: "%datestamp%"
:: echo timestamp: "%timestamp%"
:: echo fullstamp: "%fullstamp%"
:: ============================================================
:: Subroutine: Calculate a day's ordinal number within the year
::JDdayNumber day month year return_
setlocal enableextensions enabledelayedexpansion
if %2 LEQ 2 (
set /a f=%1-1+31*^(%2-1^)
) else (
set /a a=%3
set /a b=!a!/4-!a!/100+!a!/400
set /a c=^(!a!-1^)/4-^(!a!-1^)/100+^(!a!-1^)/400
set /a s=!b!-!c!
set /a f=%1+^(153*^(%2-3^)+2^)/5+58+!s!
)
set /a return_=%f%+1
endlocal & set "%4=%return_%" & goto :EOF
I refactored the code of the JDate and GDate subroutines a little bit from http://www.robvanderwoude.com/datetimentmath.php.
Usage example:
Enter ISO date:
Enter number of days to add: 7
2017-01-01 + 7 days = 2017-01-08
Enter ISO date:
Enter number of days to add:
2017-01-08 + 7 days = 2017-01-15
Enter ISO date:
Enter number of days to add:
2017-01-15 + 7 days = 2017-01-22
Enter ISO date:
Enter number of days to add:
2017-01-22 + 7 days = 2017-01-29
Enter ISO date:
Enter number of days to add:
2017-01-29 + 7 days = 2017-02-05
Enter ISO date: 2017-02-12
Enter number of days to add: -7
2017-02-12 + -7 days = 2017-02-05
Code:
"Date math.bat":
#echo off
call :main %*
goto :eof
:main
setlocal
call :initialize "2017-01-01" "1"
endlocal
goto :eof
:initialize
setlocal
set "previousDate=%~1"
set /a "numberOfDays=%~2"
set /p "previousDate=Enter ISO date: "
set /p "numberOfDays=Enter number of days to add: "
set "currentDate="
call :addIsoDateDays "%previousDate%" "%numberOfDays%" currentDate
echo %previousDate% + %numberOfDays% days = %currentDate%
echo.
call :initialize "%currentDate%" "%numberOfDays%"
endlocal
goto :eof
:stripLeadingZero
setlocal
set "number=%~1"
if %number:~0,1% equ 0 (
set "number=%number:~1%"
)
(
endlocal
set "%~2=%number%"
)
goto :eof
:addLeadingZero
setlocal
set "number=%~1"
if %number% lss 10 (
set "number=0%number%"
)
(
endlocal
set "%~2=%number%"
)
goto :eof
:gregorianToJulianDate
setlocal
set "gregorianYear=%~1"
set "gregorianMonth=%~2"
set "gregorianDay=%~3"
call :stripLeadingZero "%gregorianMonth%" gregorianMonth
call :stripLeadingZero "%gregorianDay%" gregorianDay
set /a "julianYear=(%gregorianYear% + 4800)"
set /a "julianMonth=((%gregorianMonth% - 14) / 12)"
set /a "julianDate=((1461 * (%julianYear% + %julianMonth%) / 4) + (367 * (%gregorianMonth% - 2 - (12 * %julianMonth%)) / 12) - ((3 * ((%julianYear% + %julianMonth% + 100) / 100)) / 4) + (%gregorianDay% - 32075))"
(
endlocal
set "%~4=%julianDate%"
)
goto :eof
:isoToJulianDate
setlocal
set "date=%~1"
set "year="
set "month="
set "day="
for /f "tokens=1-3 delims=-" %%a in ("%date%") do (
set "year=%%a"
set "month=%%b"
set "day=%%c"
)
set /a "julianDate=0"
call :gregorianToJulianDate "%year%" "%month%" "%day%" julianDate
(
endlocal
set "%~2=%julianDate%"
)
goto :eof
:julianToGregorianDate
setlocal
set /a "julianDate=%~1"
set /a "p=(%julianDate% + 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 "gregorianYear=((100 * (%q% - 49)) + %s% + %v%)"
set /a "gregorianMonth=(%u% + 2 - (12 * %v%))"
set /a "gregorianDay=(%t% - (2447 * %u% / 80))"
call :addLeadingZero "%gregorianMonth%" gregorianMonth
call :addLeadingZero "%gregorianDay%" gregorianDay
(
endlocal
set "%~2=%gregorianYear%"
set "%~3=%gregorianMonth%"
set "%~4=%gregorianDay%"
)
goto :eof
:julianToIsoDate
setlocal
set /a "julianDate=%~1"
set "year="
set "month="
set "day="
call :julianToGregorianDate "%julianDate%" year month day
set "isoDate=%year%-%month%-%day%"
(
endlocal
set "%~2=%isoDate%"
)
goto :eof
:addIsoDateDays
setlocal
set "previousIsoDate=%~1"
set /a "numberOfDays=%~2"
set /a "previousJulianDate=0"
call :isoToJulianDate "%previousIsoDate%" previousJulianDate
set /a "currentJulianDate=(%previousJulianDate% + %numberOfDays%)"
set "currentIsoDate="
call :julianToIsoDate "%currentJulianDate%" currentIsoDate
(
endlocal
set "%~3=%currentIsoDate%"
)
goto :eof
A simpler option is to call a PowerShell command from within your batch script to manipulate the date. The batch script can set the date as 7 days in the past with a single line.
powershell -command "((Get-date).AddDays(-7)).ToString('yyyy-MM-dd')">captureVar && set /p Today=<captureVar
The line starts out by instructing the cmd line to use PowerShell for the commands contained within the double quotes.
powershell -command "the powershell command(s)"
Next it used the PowerShell cmdlet Get-Date , and uses AddDays to change the date from the current value. A negative number will subtract and a positive number will add. The default format looks like
Friday, December 20, 2019 6:18:29 PM
To change the format you must change the date into a string with format instructions
.ToString('dddd MM/dd/yyyy HH:mm:ss.ffff K')
The output of the PowerShell command is redirected into a file named captureVar. Another option would have been to have PowerShell write it to a file.
powershell -command "((Get-date).AddDays(-7)).ToString('yyyy-MM-dd') | set-content 'captureVar'" && set /p Today=<captureVar
I used && to make it a one liner. But you can set the var anytime after value has been written to the file.
set /p Today=<captureVar
You should write in whatever scripting language you are most comfortable in. But remember that your options when writing a batch script isn't just batch commands. Windows has made it very easy to invoke PowerShell commands at anytime within your batch script. You can often find easy solutions to a given obstacle with PowerShell, insert that command into your script, then carry on as usual to finish your batch script.
A couple of things to remember when invoking PowerShell into your batch script:
Batch sees everything in double quotes as the PowerShell command. If you find a solution written in PowerShell that uses double quotes in the command, you must substitute those with single quotes. To escape characters, follow batch scripting rules. Batch variables can be read in the PowerShell command in the same way as batch (%var%). But values created in PowerShell must be redirected to be used later in your batch script. Any $var created in the PowerShell command is lost once the closing quote closes the PowerShell session.
There's an answer with calling powershell.
Though there are still machines running without powershell installed (at the moment of writing this XP,Vista,Windows7,Windows2003 and Windows2008 still have a descent OS share and are coming without installed powershell)
Other option will be using jscript within a bat script
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());
usage and the output:
E:\scripts>call 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.