Batch script to check if a file is over a certain age - windows

I'm looking for an answer... I've trawled through Google for hours and I'm stuck!
I'm writing a batch script on Windows Server 2008 to be used as an external script by NSClient for a Nagios check.
I've already got the script doing a few different things, I just need it to check a file age aswell! The file is different everyday, I've compensated for that part though. Basically, I'm looking for:
IF %FILE% (insert code to determine over 20 minutes old)
(if it is over 20 minutes old) ECHO BLAH BLAH BLAH
ELSE GOTO :123
Hope you can all help, I'm stuck on this one!!
Thanks
Will

This will return %age% variable set to 1 if older than 20 minutes, otherwise set to 0.
Launch it like this: Checkage.bat "c:\folder\filename.txt"
#echo off
:: Wmic removes regional differences
:: XP Pro can have some filename errors due to the short filename bug
:: XP Home does not have WMIC.EXE
set NumMin=20
call :CheckMins "%~f1"
echo %age%
goto :EOF
:CheckMins
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%"
set "stamp=%YYYY% %MM% %DD% %HH% %Min%"
call :DateToMinutes %stamp% NowMins
set "file=%~sf1"
:: can use CreationDate instead of lastmodified
WMIC DATAFILE WHERE name="%file:\=\\%" get lastmodified | find "." >file.tmp
for /f %%a in (file.tmp) 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%"
set "filestamp=%YYYY% %MM% %DD% %HH% %Min%"
del file.tmp 2>nul
if not defined yyyy goto :EOF
call :DateToMinutes %filestamp% FileMins
set /a MinsOld=%NowMins%-%FileMins%
if %MinsOld% gtr %NumMin% (set age=1) else (set age=0)
goto :EOF
:DateToMinutes
setlocal
set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
if /i {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if /i {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
if /i {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
set /a hh=100%hh%%%100,nn=100%nn%%%100,j=j*1440+hh*60+nn
endlocal&set %6=%j%&goto :EOF

Related

when ever a if condition satisfied then we need to pass ctrl+c to stop current item and start the next one in shell script?

Real scenario which I am facing is below,
I am having a bat file to monitor the last updated file based on last modified time and it will tell the user if the file not getting updated with in last 1 min.
2.But my scenario is ,we are using the last modified bat file to monitor the log file update. If the log file not updated in the last 1 min then the script has to send Ctrl + C thru script itself to stop the current test.
Currently when the file not updated with in 1 min then manually we will do Ctrl+C and then the system will ask us like need to exit ? we will say no then it will continue the next test suite.
Currently I am having script to monitor the log file update based on last modified date and time. But i need to automate the steps which i provided in 3.
To check last modified file pls refer below:
copy the below and save it as .bat file with name as checkfolder.
#echo off
:: Wmic removes regional differences
:: XP Pro can have some filename errors due to the short filename bug
setlocal
:: set the number of minutes in the following line
set minutes=1
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%"
set "stamp=%YYYY% %MM% %DD% %HH% %Min%"
call :DateToMinutes %stamp% NowMins
set "name="
for /f "delims=" %%a in ('dir * /a-d /b /od ^|find /v /i "%~nx0"') do set "name=%%a"
if defined name call :CheckMins "%name%"
if %MinsOld% gtr %minutes% echo Folder hasn't been modified in %minutes% minutes & pause
goto :EOF
:CheckMins
set "filestamp="
set "filemins="
set "MinsOld="
set "YY=" & set "YYYY=" & set "MM=" & set "DD="
set "HH=" & set "Min=" & set "Sec=" & set "dt="
set "file=%~sf1"
:: can use CreationDate instead of lastmodified
WMIC DATAFILE WHERE name="%file:\=\\%" get lastmodified | find "." >test.txt
for /f %%a in (rh5.log) 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%"
set "filestamp=%YYYY% %MM% %DD% %HH% %Min%"
del test.txt 2>nul
if not defined yyyy goto :EOF
call :DateToMinutes %filestamp% FileMins
set /a MinsOld=%NowMins%-%FileMins%
goto :EOF
:DateToMinutes
setlocal
set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
if /i {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if /i {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
if /i {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
set /a hh=100%hh%%%100,nn=100%nn%%%100,j=j*1440+hh*60+nn
endlocal&set %6=%j%&goto :EOF
copy the below and save it as filechecker .bat
#echo off
:loop
call "checkfolder.bat"
timeout /t 60 /nobreak
goto :loop
Then create txt file with the name test.txt. have all the three in same folder and click on checkfolder.bat to verify.
My question is,
Here i got the response as file not modified after that i need to pass ctrl+c thru script and the i have to pass the value as 'n' and hit enter.
if( file not updated)
ctrl+c and then provide n as input to start the next suite
Kindly help me out with this..
see programmatically "press" Ctrl-C / Exit batch inside CALL
Also, you may use a batch and vb hybrid to send keys to whatever window you want... In this sample, keys are sent to current cmd window. You may need to adjust timings to avoid something being echoed between Ctrl+C and s
#echo off
for /L %%a in (1,1,100) do (
echo(Test waiting for Ctrl+C, loop %%a
ping 1.1.1.1 -w 500 -n 1 > NUL
if %%a equ 6 call :sendCTRLC_WAIT
)
echo(This is never reached.
exit/B
rem wait for a second until ctrl-c is sent to this cmd session
:sendCTRLC_WAIT
SetLocal
set "_file1_=%TEMP%\after.vbs"
set "_file2_=%TEMP%\ctrlc.vbs"
>"%_file1_%" (
echo(wScript.Sleep 1300
echo(set oWS = CreateObject("wScript.Shell"^)
echo(oWS.SendKeys "s"
echo(wScript.Sleep 50
echo(oWS.SendKeys "{ENTER}"
)
>"%_file2_%" (
echo(wScript.Sleep 1000
echo(set oWS = CreateObject("wScript.Shell"^)
echo(oWS.SendKeys "^{c}"
)
start /B cmd /C ""%_file1_%"" & rem del /F /Q "%_file1_%" 2>NUL"
start /B cmd /C ""%_file2_%"" & rem del /F /Q "%_file2_%" 2>NUL"
exit/B 0
:sendCTRLC_TESTED
SetLocal
set "_file1_=%TEMP%\after.vbs"
set "_file2_=%TEMP%\ctrlc.vbs"
>"%_file1_%" (
echo(wScript.Sleep 300
echo(set oWS = CreateObject("wScript.Shell"^)
echo(oWS.SendKeys "s"
echo(wScript.Sleep 50
echo(oWS.SendKeys "{ENTER}"
)
>"%_file2_%" (
echo(set oWS = CreateObject("wScript.Shell"^)
echo(oWS.SendKeys "^{c}"
)
start /B cmd /C ""%_file1_%"" & rem del /F /Q "%_file1_%" 2>NUL"
start /B cmd /C ""%_file2_%"" & rem del /F /Q "%_file2_%" 2>NUL"
exit/B 0

"was unexpected at this time." when running .cmd file through command line in Windows

I'm getting the error "1JDa:~1JDa:~1 was unexpected at this time." when trying to run the following .cmd through the command line:
Call :GetDateTime Year Month Day
Call :SubtractDate %Year% %Month% %Day% -1 Ret
SET mes=%Ret:~0,2%
SET dia=%Ret:~3,2%
SET ano=%Ret:~6,4%
:SubtractDate 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=%MM: =%/%DD: =%/%YYYY: =%
endlocal & set %~5=%ret%
: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
I don't have experience with these scripts, so maybe I've got a basic error? Thank you.
You are simply missing some exit after you call your subroutines and get the result. Add exit /b after the SET ano=%Ret:~6,4% line and you will be fine.

Windows batch file check multiple files in a directory with date extension

I am new to command scripting.
I need to check if multiple files are available in a directory.
I am able to check the files with the exact names using below script but need direction when the files are with date in the name as shown. the date is as of yesterday, yyyymmdd.
example: metric_cal_enc.20120415.txt, reg_nyc_enc.20120415.txt, ferm_det_enc.20120415.txt
#echo off
echo Check if files are existent in inbound directory
for /f "tokens=*" %%a in (test.txt) do (
echo Checking path %%a
if EXIST %%a (
echo Exists!
) else (
echo Does Not Exist %%a >> log.txt
)
)
Assuming, for lack of an example, that your test.txt file contains
metric_cal_enc.txt
reg_nyc_enc.txt
ferm_det_enc.txt
and you have %yesterday% set up to yesterday's date (don't use a variable %date%)
if EXIST %%~na.%yesterday%%%~xa (
is the only change you'd need.
And here is how you can get Yesterdays date:
#echo off
setlocal
Call :GetDateTime Year Month Day
Echo %Year% %Month% %Day%
Call :SubtractDate %Year% %Month% %Day% -1 Ret
echo %Ret%
exit /b
:SubtractDate 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

Batch script to delete subfolder and its contents which are older than 30 minutes

I need a Windows 7 batch script which deletes sub-folder and its contents which are older than 30 minutes
e.g : forfiles -p %dump_path% -m . -d -%max_days% -c "cmd /c del /q #path"
The above code is just an example and not the proper solution.
This will echo filenames in the same folder which are over 30 minutes old by last modified date. Remove the echo from echo del "%~1" to enable it after testing.
#echo off
if /i not "%~1"=="debug" set debug=::
:: Wmic removes regional differences
:: XP Pro can have some filename errors due to the short filename bug
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%"
set "stamp=%YYYY% %MM% %DD% %HH% %Min%"
%debug% echo current time "%stamp%"
call :DateToMinutes %stamp% NowMins
for /f "delims=" %%a in ('dir * /a-d /b ^|find /v /i "%~nx0"') do call :CheckMins "%%~fa"
pause
goto :EOF
:CheckMins
set "filestamp="
set "filemins="
set "MinsOld="
set "YY=" & set "YYYY=" & set "MM=" & set "DD="
set "HH=" & set "Min=" & set "Sec=" & set "dt="
set "file=%~sf1"
:: can use CreationDate instead of lastmodified
WMIC DATAFILE WHERE name="%file:\=\\%" get lastmodified | find "." >file.tmp
for /f %%a in (file.tmp) 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%"
set "filestamp=%YYYY% %MM% %DD% %HH% %Min%"
del file.tmp 2>nul
%debug% echo file time "%filestamp%"
if not defined yyyy goto :EOF
call :DateToMinutes %filestamp% FileMins
set /a MinsOld=%NowMins%-%FileMins%
%debug% echo Now:%NowMins% File:%FileMins% Fileage:%minsold% "%~1"
%debug% pause
if %MinsOld% gtr 30 echo del "%~1"
goto :EOF
:DateToMinutes
setlocal
set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
if /i {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if /i {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
if /i {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
set /a hh=100%hh%%%100,nn=100%nn%%%100,j=j*1440+hh*60+nn
endlocal&set %6=%j%&goto :EOF

A batch job which would copy/delete text files created in the past 5 days

I am working on creating a simple batch job which would copy and then delete text files from one specified directory to another. However, I am trying to only copy/delete files created in the past 5 days. Can days be specified using the MOVE batch command?
Here is what I have so far. This script deletes the files from the original directory and copy them to the new directory.
#echo off
C:
cd C:\Air
move c:\Air\*.txt d:\Air_backup
cd C:\
exit
Thanks in advance for your input!
This is not what you'd call simple, but it will delete files older than 5 minutes (using creation date) from the current folder.
At the moment the del command will only be echoed to the screen so remove the echo keyword after the if %MinsOld% gtr 5 to enable the del command and change the 5 to 7200 for 7200 minutes.
XP users will have problems with some filenames occasionally due to a short filename bug in Windows XP.
The code can use CreationDate or LastModified
Change this line:
if %MinsOld% gtr 5 echo del "%~1"
to this to move the 5 day old files to a target folder:
if %MinsOld% gtr 7200 move "%~1" "d:\target\folder"
Here is the essential code:
#echo off
if /i not "%~1"=="debug" set debug=::
:: Wmic removes regional differences
:: XP Pro can have some filename errors due to the short filename bug
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%"
set "stamp=%YYYY% %MM% %DD% %HH% %Min%"
%debug% echo current time "%stamp%"
call :DateToMinutes %stamp% NowMins
for /f "delims=" %%a in ('dir * /a-d /b ^|find /v /i "%~nx0"') do call :CheckMins "%%~fa"
pause
goto :EOF
:CheckMins
set "filestamp="
set "filemins="
set "MinsOld="
set "YY=" & set "YYYY=" & set "MM=" & set "DD="
set "HH=" & set "Min=" & set "Sec=" & set "dt="
set "file=%~sf1"
:: can use CreationDate instead of lastmodified
WMIC DATAFILE WHERE name="%file:\=\\%" get creationdate | find "." >file.tmp
for /f %%a in (file.tmp) 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%"
set "filestamp=%YYYY% %MM% %DD% %HH% %Min%"
del file.tmp 2>nul
%debug% echo file time "%filestamp%"
if not defined yyyy goto :EOF
call :DateToMinutes %filestamp% FileMins
set /a MinsOld=%NowMins%-%FileMins%
%debug% echo Now:%NowMins% File:%FileMins% Fileage:%minsold% "%~1"
%debug% pause
if %MinsOld% gtr 5 echo del "%~1"
goto :EOF
:DateToMinutes
setlocal
set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
if /i {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if /i {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
if /i {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
set /a hh=100%hh%%%100,nn=100%nn%%%100,j=j*1440+hh*60+nn
endlocal&set %6=%j%&goto :EOF

Resources