Check date file age - output on message box (help)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kadkam
Posts: 14
Joined: 26 Aug 2013 06:32

Check date file age - output on message box (help)

#1 Post by kadkam » 26 Aug 2013 06:38

Hi

I need to compare date file with actual date.

I need an example like this:

If the file (%1) is older than 1hour a message box appear
"The file age is 1 days 2 hour 35minutes"
(if the file age is lower than 1hour no message appear)

Someone can post the batch?

Thanks

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Check date file age - output on message box (help)

#2 Post by penpen » 26 Aug 2013 08:51

This may help you:
http://www.dostips.com/forum/viewtopic.php?f=3&t=4846
You just have to take care of the date/time format.

Assumed, that the variable diff holds the age of the file in seconds,
this is the code to do a formated output of files older than 1 hour:

Code: Select all

set /A "diffDays=diff/86400", "diffHours=(diff%86400)/3600", "diffMins=(diff%3600)/60"
if %diff% GTR 3600 echo "The file age is %diffDays% days %diffHours% hour %diffMins%minutes"

penpen

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Check date file age - output on message box (help)

#3 Post by penpen » 26 Aug 2013 15:42

Ups, i've missed the open message box part... .
Just add/change the code to:

Code: Select all

@if (true == false) @end /* added this harmless hybrid batch/JScript line

@echo off
set "fileName=test.bat"

call :GetInternational
for %%a in ("%fileName%") do call :GetSecs %%~ta startTime
call :GetSecs "%date%" "%time%" endTime
set /a diff=(%endTime%-%startTime%)

set /A "diffDays=diff/86400", "diffHours=(diff%%86400)/3600", "diffMins=(diff%%3600)/60"
::instead of using echo, execute the JScript code below passing the message as a parameter
if %diff% GTR 3600 cscript //E:JScript //Nologo "%~f0" "The file age is %diffDays% day(s) %diffHours% hour(s) %diffMins% minute(s)."
goto :eof


:GetInternational
for /f "tokens=1,2*" %%a in ('reg query "HKCU\Control Panel\International"^|find "REG_SZ" ') do set "%%a=%%c"
exit /b

:GetSecs "dateIn" "timeIn" secondsOut
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%"
exit /b

:: Added the below JScript code below to display a message given as a parameter value encapsulated in doublequotes
*/

if (WScript.Arguments.Unnamed.length == 1) {
   var wshShell = WScript.CreateObject("WScript.Shell");
   var btn = wshShell.Popup(WScript.Arguments.Unnamed.Item(0), 0, "Information.");
}

penpen

Edit: Added missing code above instead of using placeholder comments, to make the result more readable.
Using placehodler in this way was bad style, sorry for that.
Last edited by penpen on 27 Aug 2013 02:00, edited 1 time in total.

kadkam
Posts: 14
Joined: 26 Aug 2013 06:32

Re: Check date file age - output on message box (help)

#4 Post by kadkam » 27 Aug 2013 06:51

thanks PenPen

Work great.
I did not imagine a program so long. it is the fault of the support of all types of international date?

Do you know a good bat to exe free converter?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Check date file age - output on message box (help)

#5 Post by penpen » 27 Aug 2013 08:03

kadkam wrote:it is the fault of the support of all types of international date?
That is one cause.
The other cause is extracting the single values out of the date and time representation
without running into trouble with the different number formats that "set /A" supports.
This may be done with lesser code, but i didn't want to "reinvent the wheel",
as i then maybe have to change the given implementation of the Euler date formular.
Additionally i don't think that such a reimplementation results in a significant speed up.

kadkam wrote:Do you know a good bat to exe free converter?
Sorry, i don't know any converter no matter if free or not.

penpen

kadkam
Posts: 14
Joined: 26 Aug 2013 06:32

Re: Check date file age - output on message box (help)

#6 Post by kadkam » 27 Aug 2013 08:18

stand alone work ok.
Now, I have integrate this batch in to another and
I obtain this error:
Jscript : Conditional compilation disabled

??

for now, I had to change this:

Code: Select all

if %diff% GTR 3600 echo msgbox" days%diffDays%  hours %diffHours% minutes %diffMins%">diff.vbs&diff.vbs&del diff.vbs

I not love to create/delete a .vbs file...

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Check date file age - output on message box (help)

#7 Post by penpen » 27 Aug 2013 09:11

kadkam wrote:stand alone work ok.
Now, I have integrate this batch in to another and
I obtain this error:
Jscript : Conditional compilation disabled

??
I assume you have just appended the batch code above to another batch file, that starts with "@command" (probably "@echo off").
The problem here is that this is a hybrid JScript/batch file (to avoid creating a temporary file).
The first line in the above script should be the first line in your combined batch file,
then between the "/*" and the "*/" (JScript multiline comment delimiter) there is normal batch code,
and at the bottom there is some JScript code.
So if you want to add more batch code, you have to do it within the JScript comment.
Prior to the JScript comment end delimiter "*/" there should be an exit, exit /b, or an goto :eof to be sure the batch interpreter doesn't compute the JScript lines.

kadkam wrote:for now, I had to change this:

Code: Select all

if %diff% GTR 3600 echo msgbox" days%diffDays%  hours %diffHours% minutes %diffMins%">diff.vbs&diff.vbs&del diff.vbs

I not love to create/delete a .vbs file...
i am not sue what this should do, but i assume if you extend your batch code as described above, this change should be unneccessary.

penpen

kadkam
Posts: 14
Joined: 26 Aug 2013 06:32

Re: Check date file age - output on message box (help)

#8 Post by kadkam » 27 Aug 2013 10:18

my solution work good but write/delete temporary file.

your solution is this (tested and is ok now)

Code: Select all

@if (true == false) @end /* added this harmless hybrid batch/JScript line



[ALL BATCH CODE ("AGE CODE" + ALL OTHER CODE)]



*/

if (WScript.Arguments.Unnamed.length == 1) {
   var wshShell = WScript.CreateObject("WScript.Shell");
   var btn = wshShell.Popup(WScript.Arguments.Unnamed.Item(0), 0, "Information.");
}


thanks

agaswoodstock
Posts: 7
Joined: 03 Sep 2013 21:33

Re: Check date file age - output on message box (help)

#9 Post by agaswoodstock » 03 Sep 2013 21:38

how to calculate total time of network down using bat

example 8.00 am, status: request time out
9.00 am status: reply

total time out = 1 hour

kadkam
Posts: 14
Joined: 26 Aug 2013 06:32

Re: Check date file age - output on message box (help)

#10 Post by kadkam » 04 Sep 2013 00:14

uhm ... I think this is another case, you must open another thread.

Post Reply