Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

batch file - 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_MACHINESoftwareWow6432NodeKasperskyLabComponents3411031.0.0.0StatisticsAVState
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 "HKLMSoftwareWow6432NodeKasperskyLabComponents3411031.0.0.0StatisticsAVState" /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:ProgramDataCentraStageAEMAgentantivirus.json

:end
@exit
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 "HKLMSoftwareWow6432NodeKasperskyLabComponents3411031.0.0.0StatisticsAVState" /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
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...