Editing a batch file to take argument directly from the code - date

I have a batch file that return date minus number of given days.
E.g. : if we are 09/11/2013 batch.bat today -1 will return 08/11/2013
and if it's a weekend it will return the Friday date.
The problem is that I try to make the batch give me the last business day without entering any argument but I failed
Here is the code :
#echo off
if "%~2"=="" (
echo to get yesterdays date use call "%~n0" today -1
echo.
echo Add a third parameter if you want a separator in the date string
echo EG: for this format YYYY-MM-DD using yesterdays date
echo call "%~n0" today -1 -
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 the daynum is a weekend then run the batch file again to get the friday
set "weekend="
if %daynum% EQU 1 set /a weekend=qty - 2
if %daynum% EQU 7 set /a weekend=qty - 1
if defined weekend %0 %1 %weekend%
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.
echo daynum is "%daynum%"
echo date is %YY%%MM%%DD%

#echo off
set qty=-1
:loop4weekends
set "separator="
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,now)
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 the daynum is a weekend then loop to get the friday
set "weekend="
if %daynum% EQU 1 set weekend=1&set "qty=-3"
if %daynum% EQU 7 set weekend=1&set "qty=-2"
if defined weekend goto :loop4weekends
set "day=%YY%%separator%%MM%%separator%%DD%"
echo %%day%% is set to "%day%" (without the quotes)

Related

Comparing registry string value within a date range

I am writing a batch script which needs to compare registry data value string with ±3 days of todays date.
My REG QUERY returns a value:
HKEY_LOCAL_MACHINE\Software\Wow6432Node\KasperskyLab\Components\34\1103\1.0.0.0\Statistics\AVState
Protection_BasesDate REG_SZ 27-08-2018 08-53-00
I need to output to a file, depending if it is within the range or not.
Script:
REG QUERY "HKLM\Software\Wow6432Node\KasperskyLab\Components\34\1103\1.0.0.0\Statistics\AVState" /v "Protection_BasesDate" | Find "2018"
IF %ERRORLEVEL% == 1 goto end
If %ERRORLEVEL% == 0 goto makefile
:makefile
echo "{"product":"Override Antivirus","running":true,"upToDate":true}" > c:\ProgramData\CentraStage\AEMAgent\antivirus.json
:end
#exit
This solution takes this answer as base, so please review such an answer before post further questions here...
#echo off
setlocal EnableDelayedExpansion
rem Define the "Date to Julian Day Number" conversion function
set "DateToJDN(YMD)=( a=(YMD), y=a/10000, a%%=10000, m=a/100, d=a%%100, a=(m-14)/12, (1461*(y+4800+a))/4+(367*(m-2-12*a))/12-(3*((y+4900+a)/100))/4+d-32075 )"
rem Get the JDN of today's date minus/plus 3
for /F "tokens=2 delims==" %%t in ('wmic os get localdatetime /value') do set "dateTime=%%t"
set /A "todayMinus3=!DateToJDN(YMD):YMD=%dateTime:~0,8%!-3, todayPlus3=todayMinus3+6"
reg Get the date from REG QUERY command; the assumed output format is: Protection_BasesDate REG_SZ 27-08-2018 08-53-00
for /F "tokens=3-5 delims=- " %%a in (
'REG QUERY "HKLM\Software\Wow6432Node\KasperskyLab\Components\34\1103\1.0.0.0\Statistics\AVState" /v "Protection_BasesDate"'
) do set /A "BasesDate=!DateToJDN(YMD):YMD=%%c%%b%%a!"
if %BasesDate% geq %todayMinus3% if %basesDate% leq %todayPlus3% (
echo Date in range
)
There are many languages that can do much better than batch, but this might be a solution.
#echo off
set day=-3
for /f "delims=" %%i in ('reg query "HKLM\Software\Wow6432Node\KasperskyLab\Components\34\1103\1.0.0.0\Statistics\AVState" /v Protection_BasesDate" ^| findstr "2018"') do set "regdate=%%i"
for /f "tokens=1-3" %%a in ("%regdate%") do set "actual=%%c"
:check
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 "final=%dd%-%mm%-%yyyy%"
echo %final%
if %day% == 1 goto :EOF
if %actual% == %final% (echo Within 3 days!!) else (set /a day+=1 & goto :check)
We actually get the date of the system using cscript (yes I pipe to file, but you can run hybrid without the temp file) Then we take the past 3 days' dates and match them in the format of the registry key, if the key matches within 3 days, I just echo Within 3 days! There is also an if statement, if the day counter = 1, I exit the script, as we do not want to go into future and this loop forever if the counter if we do not stop it somewhere.
If it does work for you, you can simply remove the line print %final% and replace this line:
if %actual% == %final% (echo Within 3 days!!) else (set /a day+=1 & goto :check)
with this line:
if %actual% == %final% (echo "{"product":"Override Antivirus","running":true,"upToDate":true}" > "c:\ProgramData\CentraStage\AEMAgent\antivirus.json") else (set /a day+=1 & goto :check)

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

Get last business date from a batch file

I found a little code that take the date of today and return the day-1 date (we call it last business day) , if it's Monday or Sunday or Saturday it returns the Friday date
#echo off
set qty=-1
:loop4weekends
set "separator="
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,now)
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 the daynum is a weekend then loop to get the friday
set "weekend="
echo %daynum%
if %daynum% EQU 1 set weekend=1&set "qty=-3"
if %daynum% EQU 7 set weekend=1&set "qty=-2"
if defined weekend goto :loop4weekends
set "day=%YY%%separator%%MM%%separator%%DD%"
echo %%day%% is set to "%day%" (without the quotes)
pause
the problem is that now I need the day-2 so I changed the variable qty=-1 to qty=-2but it doesn't work
how can I do to get the today-2 date ?
You have to change the values of qty in these line to :
if %daynum% EQU 1 set weekend=1&set "qty=-3"
if %daynum% EQU 7 set weekend=1&set "qty=-2"
Here the code for -2 :
#echo off
set qty=-2
:loop4weekends
set "separator="
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,now)
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 the daynum is a weekend then loop to get the friday
set "weekend="
echo %daynum%
if %daynum% EQU 1 set weekend=1&set "qty=-4"
if %daynum% EQU 7 set weekend=1&set "qty=-3"
if defined weekend goto :loop4weekends
set "day=%YY%%separator%%MM%%separator%%DD%"
echo %%day%% is set to "%day%" (without the quotes)
pause

How to change variable in commnd prompt numerically

I have used system date from %date% using this code
set d=20%date:~6,2%%date:~0,2%
Its output is say 201309
How can I get the same for date of 30 days back(so I want 201308)? like is there any operation like addition/substraction for dates? I tried
set /a "date2=%date%-30"
But it gives error :"Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021)." as cmd asumes a value is hexadecimal if it starts with 0.
Using this batch file you should get what you need:
The routines can do some extra date math - the batch file is after the first 9 lines and can be self contained.
#echo off
call datebatch today -30
set "var=%day:0,6%"
echo "%var%"
pause
goto :eof
:datebatch
:: 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: 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" 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 "YY=%result:~0,4%"&set "MM=%result:~4,2%"&set "DD=%result:~6,2%"
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%

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.