FINE THE FILE

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
raj_repos
Posts: 8
Joined: 08 Oct 2007 23:03

FINE THE FILE

#1 Post by raj_repos » 08 Oct 2007 23:16

Hi
I am looking for a script whic can check one file in the directory
and that particular file need to compare with current time and
if it's 20 mimutes less than current time the this script need to send an email

for example
current time in windows -- 12:30

in my c:\ dir i have the following files

test1.txt 12:00
sap.txt 12:10
reco.txt 12:30


this script need to chek for sap.txt file this file is less than 20mins to the current time
then this script need to send email

rds

raj_repos
Posts: 8
Joined: 08 Oct 2007 23:03

#2 Post by raj_repos » 10 Oct 2007 20:23

ANY UPDATE PLS

jeb
Expert
Posts: 914
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#3 Post by jeb » 11 Oct 2007 14:39

Hi raj_repos,

take a look at the function library
special
:CmpFTime -- compares the time of two files, succeeds if condition is met, fails otherwise

you have to change the compare mechanism a bit, but then you can test if a file is newer than 20minutes.

How to send is another problem, i suppose you will need a seperate mail-client like sendmail or something else.

Ciao

jeb

raj_repos
Posts: 8
Joined: 08 Oct 2007 23:03

HI

#4 Post by raj_repos » 17 Oct 2007 21:47

Hi jeb
Thanks for your reply
I have external mail client , is it is possibe i can have the script
using that function
rds

jeb
Expert
Posts: 914
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#5 Post by jeb » 19 Oct 2007 03:08

Hi Raj,

try this

@echo off
setlocal

set now=0
for /F "tokens=1,2 delims=: " %%a IN ("%time%") DO (
set /a now=%%a*60 + %%b
)

set ftime=0
for /F "tokens=2,3 delims=: " %%f IN ("%~t1") DO (
set /a ftime=%%f * 60 + %%g
)

set /a minunte_diff = now-ftime
if %minunte_diff% GTR 20 echo More than 20 minutes

ciao
jeb

raj_repos
Posts: 8
Joined: 08 Oct 2007 23:03

#6 Post by raj_repos » 25 Oct 2007 01:44

oK thanks i try this
where i need to put the file name
rds

raj_repos
Posts: 8
Joined: 08 Oct 2007 23:03

#7 Post by raj_repos » 25 Oct 2007 01:53

when i run i get this error between the code


C:\>@echo off
setlocal
set now=0
for /F "tokens=1,2 delims=: " %%a IN ("%time%") DO (
%%a was unexpected at this time.
set /a now=%%a*60 + %%b
Missing operand.
)
set ftime=0
for /F "tokens=2,3 delims=: " %%f IN ("%~t1") DO (
%%f was unexpected at this time.
set /a ftime=%%f * 60 + %%g
Missing operand.
)
set /a minunte_diff = now-ftime
0if %minunte_diff% GTR 20 echo More than 20 minutes

jeb
Expert
Posts: 914
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#8 Post by jeb » 25 Oct 2007 06:05

Hi,

start your batch like
test.bat sap.txt

on my system (WinXP) it works.

Your errors can occour when the time (hours or minutes) are 08 or 09.
Then the parser try to use these numbers as octal(the "0" prefix),
but 08 and 09 aren't octal numbers, therefore it does not work every time.

ok we need some corrections (DosItHelp: Your library needs also corrections at the date/time functions).
I simply prefix a "1" then i got for the time "08:09" the line
set /a now=1%%a*60 + 1%%b - 700
set /a now=108*60 + 109 - 700

700 is 100*60+100, so i got the correct minute value



@echo off
setlocal enableextensions

set now=0
for /F "tokens=1,2 delims=: " %%a IN ("%time%") DO (
set /a now=1%%a*60 + 1%%b - 700
)

set ftime=0
for /F "tokens=2,3 delims=: " %%f IN ("%~t1") DO (
set /a ftime=1%%f * 60 + 1%%g - 700
)

set /a minunte_diff = now-ftime
if %minunte_diff% GTR 20 (
echo More than 20 minutes, exactly %minunte_diff% minutes
) ELSE (
echo only %minunte_diff% minutes diff
)

raj_repos
Posts: 8
Joined: 08 Oct 2007 23:03

CHECK THE FILE

#9 Post by raj_repos » 04 Nov 2007 21:47

Hi Jeb

it works , but i put the file name which is not exists also it gives result as follows

C:\>test.bat ak4.txt
More than 20 minutes, exactly 6109 minutes

actually there is no such file (ak4.txt) in this folder

then i put one file called ak.txt and run this batch file the result is

C:\>test.bat ak.txt
only 19 minutes diff

this is correct

why it give the result for the file which is not exists

any place i need to change to look for particular file

rds

jeb
Expert
Posts: 914
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#10 Post by jeb » 05 Nov 2007 15:53

Hi raj_repos,

why it give the result for the file which is not exists

Code: Select all

set ftime=0
for /F "tokens=2,3 delims=: " %%f IN ("%~t1") DO (
  set /a ftime=1%%f * 60 + 1%%g - 700
)

Will result for a non existing file in ftime=0, because the for will do nothing.

you can add

Code: Select all

if not exist "%~1" (
  echo No file - no time - good bye
  goto :eof
)


Good look

jeb

raj_repos
Posts: 8
Joined: 08 Oct 2007 23:03

#11 Post by raj_repos » 09 Nov 2007 03:24

Hi jeb
sorry for disturbing again
the scripts ia not working at all
if i type without any file name also it gives the results as follows
if i put any new file also same results
pls help

C:\>test.bat
More than 20 minutes, exactly 582 minutes

C:\>test.bat
More than 20 minutes, exactly 6445 minutes

C:\>test.bat
More than 20 minutes, exactly 6446 minutes

C:\>test.bat file1.txt
More than 20 minutes, exactly 721 minutes


the code
------------------------------

@echo off
setlocal enableextensions

set now=0
for /F "tokens=1,2 delims=: " %%a IN ("%time%") DO (
set /a now=1%%a*60 + 1%%b - 700
)

set ftime=0
for /F "tokens=2,3 delims=: " %%f IN ("%~t1") DO (
set /a ftime=1%%f * 60 + 1%%g - 700
)

set /a minunte_diff = now-ftime
if %minunte_diff% GTR 20 (
echo More than 20 minutes, exactly %minunte_diff% minutes
) ELSE (
echo only %minunte_diff% minutes diff
)

-------------------
rds

raj_repos
Posts: 8
Joined: 08 Oct 2007 23:03

#12 Post by raj_repos » 09 Nov 2007 03:27

after i added the new code for checkong if the file is not exists
that particular this is working but not for file exists

==============
@echo off
setlocal enableextensions

set now=0
for /F "tokens=1,2 delims=: " %%a IN ("%time%") DO (
set /a now=1%%a*60 + 1%%b - 700
)

if not exist "%~1" (
echo No file - no time - good bye
goto :eof
)

set ftime=0
for /F "tokens=2,3 delims=: " %%f IN ("%~t1") DO (
set /a ftime=1%%f * 60 + 1%%g - 700
)

set /a minunte_diff = now-ftime
if %minunte_diff% GTR 20 (
echo More than 20 minutes, exactly %minunte_diff% minutes
) ELSE (
echo only %minunte_diff% minutes diff
)
===================================

This is working for if the file is not exists

C:\>testak.bat file111.txt
No file - no time - good bye

--

C:\>testak.bat file1.txt
More than 20 minutes, exactly 727 minutes

actually the file was created only one minutes before

jeb
Expert
Posts: 914
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#13 Post by jeb » 09 Nov 2007 05:23

Hi raj_repos,
C:\>testak.bat file1.txt
More than 20 minutes, exactly 727 minutes

actually the file was created only one minutes before
interessting ...

On my system it works, so I suppose it could be a problem with different time-formats or so.
Simply add some debugcode, like this

Code: Select all

@echo off
setlocal enableextensions

    echo ------- Now: %time%

set now=0
for /F "tokens=1,2 delims=: " %%a IN ("%time%") DO (
set /a now=1%%a*60 + 1%%b - 700
)

if not exist "%~1" (
echo No file - no time - good bye
goto :eof
)

    for /F %%f IN ("%~t1") DO echo ------- Filetime: %%f
set ftime=0
for /F "tokens=2,3 delims=: " %%f IN ("%~t1") DO (
    echo ------- found: %%f:%%g
set /a ftime=1%%f * 60 + 1%%g - 700
)

set /a minunte_diff = now-ftime
if %minunte_diff% GTR 20 (
echo More than 20 minutes, exactly %minunte_diff% minutes
) ELSE (
echo only %minunte_diff% minutes diff
)


if you can't see the problem, post the results and perhaps I can find a solution.

jeb

Post Reply