Comparing registry string value within a date range - date

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)

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

Editing a batch file to take argument directly from the code

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)

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

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.