Search By Seconds Not Minutes - windows

I have a batch file that works pretty well. I originaly set it up to search for files that are a minute or less old. I need to go down further and search for files that are 15 seconds old. I hope thats quick enough, I might have to adjust it later. In any case can anyone help me get it down to the seconds range. Thank you. Your help is appreciated.
#echo off
cd "C:\Users\DS\Downloads"
setlocal
call :DateToMinutes %date:~-4% %date:~-10,2% %date:~-7,2% %time:~0,2% %time:~3,2% NowMins
set flag=0
for /f "delims=" %%a in ('dir *.jpg *.zip *.txt /a-d /b') do call :CheckMins "%%a" "%%~ta"
if %flag% EQU 1 (
msg * "Good-Bye!"
)
set flag=0
goto :EOF
:CheckMins
set File=%1
set TimeStamp=%2
call :DateToMinutes %timestamp:~7,4% %timestamp:~1,2% %timestamp:~4,2% %timestamp:~12,2% %timestamp:~15,2%%timestamp:~18,1% FileMins
set /a MinsOld=%NowMins%-%FileMins%
if %MinsOld% leq 1 del %file%
if %MinsOld% leq 1 set flag=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

I didn't know a simple batch way to get the seconds, but you can get them via WMIC.
WMIC DATAFILE WHERE Name="C:\\windows\\DirectX.log" GET lastmodified,Lastaccessed
The format of the output is a bit odd, but it should work.

Related

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

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

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

CMD date change log

I am making a program and it has a change log. The programs change log must say the version and then show how many days ago it was. So it should say:
[2013/03/19, 2 days ago]
I used this code:
set versionreleasedate1=2013/03/17
set /a verionday=%versionreleasedate1%-%date%
echo Version: 1.0 BETA [2013/03/19, %verionday% days ago]
but I know that the date it is dividing because cmd thinks that the \ symbol is devide buy I want that a little sepricator for the date.
So it would work like this
set /a var1=2013/03/17-%date% (todays date)
and echo how many days ago. Any help would be awesome thanks.
This works for my machine, and hopefully for yours:
#echo off &setlocal
set "versionreleasedate1=2013/03/17"
set "versionreleasetime1=00:00:00,00"
call :GetInternational
call :GetSecs "%versionreleasedate1%" "%versionreleasetime1%" startsec
call :GetSecs "%date%" "%time%" stopsec
set /a versionday=(%stopsec%-%startsec%)/86400
echo Version: 1.0 BETA [%versionreleasedate1%, %versionday% days ago]
goto :eof
:GetInternational
:: Sets a bundle of variables by reading the registry settings
for /f "tokens=1,2*" %%a in (' reg query "HKCU\Control Panel\International"^|find "REG_SZ" ') do set "%%a=%%c"
goto :eof
:GetSecs "dateIn" "timeIn" secondsOut
:: Output: Seconds elapsed since 1th Jan. 1970 00:00:00
:: http://www.dostips.com/forum/viewtopic.php?p=6450#p6450
setlocal
set "dateIn=%~1"
for /f "tokens=2" %%i in ("%dateIn%") do set "dateIn=%%i"
for /f "tokens=1-3 delims=%sDate%" %%a in ("%dateIn%") do (
if %iDate%==0 set /a mm=100%%a%%100,dd=100%%b%%100,yy=10000%%c%%10000
if %iDate%==1 set /a dd=100%%a%%100,mm=100%%b%%100,yy=10000%%c%%10000
if %iDate%==2 set /a yy=10000%%a%%10000,mm=100%%b%%100,dd=100%%c%%100
)
for /f "tokens=1-3 delims=%sTime%%sDecimal% " %%a in ("%~2") do (
set "hh=%%a"
set "nn=%%b"
set "ss=%%c"
)
if 1%hh% lss 20 set hh=0%hh%
if "%nn:~2,1%" equ "p" if "%hh%" neq "12" (set "hh=1%hh%" &set /a hh-=88)
if "%nn:~2,1%" equ "a" if "%hh%" equ "12" set "hh=00"
if "%nn:~2,1%" geq "a" set "nn=%nn:~0,2%"
set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2,j=j/5+dd+y*365+y/4-y/100+y/400-2472633,j=j*86400+hh*3600+nn*60+ss
endlocal &set "%~3=%j%"
goto :eof
endlocal
%versionreleasedate1% & %versionreleasetime1% must be in the same format like %date% & %time% on your machine!

Batch File progress spinning wheel

I have been trying for days and can seem to get this to work. I found an example but it uses a (CryEcho) which will not work. I just wanted to add this to let the user know something is going on while im pinging IP addresses. I did find some code on here but it was confusing to me since im just starting to mess around with batch files for fun.
Anyway, I wanted to have something that used something like the example below but with text like (Waiting...[spinner]). Thanks!
#echo off
setlocal
set COUNT=0
set MAXCOUNT=10
set SECONDS=1
:LOOP
title "\"
call :WAIT
title "|"
call :WAIT
title "/"
call :WAIT
title "-"
if /i "%COUNT%" equ "%MAXCOUNT%" goto :EXIT
set /a count+=1
echo %COUNT%
goto :LOOP
:WAIT
ping -n %SECONDS% 127.0.0.1 > nul
ping -n %SECONDS% 127.0.0.1 > nul
goto :EOF
:EXIT
title FIN!
endlocal
AND I found this code as well:
#echo off
rem Example showing how to use CryEcho to produce a spinning wheel to show activity.
CryEcho Working ...
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
cryecho \s\nFinished.
goto :eof
:DoSomeWork
ping -n 1 localhost > nul
goto :eof
:SpinAlive
if "%Spinner%" == "2" (cryecho \\\b)
if "%Spinner%" == "3" (cryecho -q "|"\b)
if "%Spinner%" == "4" (cryecho /\b set Spinner=0) else (cryecho -\b set Spinner=1)
set /A Spinner=%Spinner%+1
goto :eof
The main trick is here to move the cursor back on the same line.
This can be done with a carriage return or a backspace characters.
#echo off
setlocal EnableDelayedExpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
FOR /L %%n in (1,1,10) DO (
call :spinner
ping localhost -n 2 > nul
)
exit /b
:spinner
set /a "spinner=(spinner + 1) %% 4"
set "spinChars=\|/-"
<nul set /p ".=Waiting !spinChars:~%spinner%,1!!CR!"
exit /b
The <CR> character is created from the output of copy /z.
The output is done by set /p, as this omits the output of CR/LF at the end.
The !CR! is output each time to force the cursor to move back to the first column.
But as Win7 and Vista removes all whitespaces and also CR from the leading output of set /p, it's placed at the end.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
CALL :BACKSPACE $BS
SET /A FULL_COUNT=75
SET /A MAX_COUNT=160
SET /A Spin_Delay=60
SET "_MSG=Process running..."
SET /A CTR=0
IF NOT [%1]==[] SET "_MSG=%~1"
IF NOT [%2]==[] SET /A FULL_COUNT=%2
IF NOT [%3]==[] SET /A SPIN_DELAY=%3
IF %FULL_COUNT% GTR %MAX_COUNT% SET /A FULL_COUNT=%MAX_COUNT%
<nul SET/P="%_MSG%*"
SET "SPINNER=³/Ä\"
FOR /L %%A IN (1,1,%FULL_COUNT%) DO (
CALL :DELAY %SPIN_DELAY%
<nul CALL SET/P="%$BS%%%SPINNER:~!CTR!,1%%"
SET /A CTR=%%A %% 4
)
<nul SET/P="%$BS%*"
ENDLOCAL & EXIT /B %CTR%
:BackSpace
setlocal
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
endlocal&call set %~1=%BS%&exit /b 0
:Delay msec
setlocal
set/a correct=0
set/a msecs=%1+5
if /i %msecs% leq 20 set /a correct-=2
set time1=%time: =%
set/a tsecs=%1/1000 2>nul
set/a msecs=(%msecs% %% 1000)/10
for /f "tokens=1-4 delims=:." %%a in ("%time1%") do (
set hour1=%%a&set min1=%%b&set sec1=%%c&set "mil1=%%d"
)
if /i %hour1:~0,1% equ 0 if /i "%hour1:~1%" neq "" set hour1=%hour1:~1%
if /i %min1:~0,1% equ 0 set min1=%min1:~1%
if /i %sec1:~0,1% equ 0 set sec1=%sec1:~1%
if /i %mil1:~0,1% equ 0 set mil1=%mil1:~1%
set/a sec1+=(%hour1%*3600)+(%min1%*60)
set/a msecs+=%mil1%
set/a tsecs+=(%sec1%+%msecs%/100)
set/a msecs=%msecs% %% 100
:: check for midnight crossing
if /i %tsecs% geq 86400 set /a tsecs-=86400
set/a hour2=%tsecs% / 3600
set/a min2=(%tsecs%-(%hour2%*3600)) / 60
set/a sec2=(%tsecs%-(%hour2%*3600)) %% 60
set/a err=%msecs%
if /i %msecs% neq 0 set /a msecs+=%correct%
if /i 1%msecs% lss 20 set "msecs=0%msecs%"
if /i 1%min2% lss 20 set "min2=0%min2%"
if /i 1%sec2% lss 20 set "sec2=0%sec2%"
set "time2=%hour2%:%min2%:%sec2%.%msecs%"
:wait
set timen=%time: =%
if /i %timen% geq %time2% goto :end
goto :wait
:end
for /f "tokens=2 delims=." %%a in ("%timen%") do set num=%%a
if /i %num:~0,1% equ 0 set num=%num:~1%
set/a err=(%num%-%err%)*10
endlocal&exit /b %err%

Resources