Script to check changes in a folder every 45 minutes

mathayus_ITA
mathayus_ITA used Ask the Experts™
on
Hi,

i'd need a script that check a folder (let's say c:\my_folder) every 45 minutes to know if its content is changed. If its content is changed (there aren' t the same files or the folder is empty) compared to the previous check then it's all ok, otherwise send an email to my_email@my_domain.com: "problem on c:\my_folder".

Thank you for your help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
OK, you want something along the lines of a dir command comparing with a previous copy then, have put this together initially for you but haven't had a chance to do any testing.  Let me know how you get on.

Steve

@echo off
REM Define log file and directory to check
set olddir=C:\scripts\olddir.txt
set thisdir=c:\scripts\nowdir.txt
set check=c:\my_folder

REM Create directory for scripts if doesn't exist
mkdir c:\scripts 2>NUL

REM Rename previous run log file
if exist "%oldir%" del "%olddir%"
if exist "%nowdir%" (
  rename "%nowdir%" "%olddir%"
  dir "%check%" > "%thisdir%"
  fc "%nowdir%" "%olddir%
  if "%errorlevel%"==0 call :email
) ELSE echo This is the first run so no comparison done
exit /b

:email
echo Emailing "no change" email
REM * Uses blat.exe and body.txt as body of message:
REM * Creates a log file in c:\scripts\blatlog.txt

set logfile=c:\scripts\blatlog.txt
set hostname=ServerName
set subject="Files on %computername% have not changed"
set toaddress=deliver-email-to@domain.co.uk

if not exist c:\scripts\blatinstalled.txt (
  set smtpserver=smtpserver
  set fromaddress=fromaddress@domain.co.uk
  c:\scripts\blat -install %smtpserver% %fromaddress%
  echo %date% %time% > c:\scripts\blatinstalled.txt
)

echo Sending email
echo %date% - %time% > %logfile%
c:\scripts\blat C:\scripts\blatbody.txt -subject %subject% -to %toaddress% -hostname %hostname% -log %logfile%
echo Send complete.
exit /b

Commented:
Some other methods of sending an email from batch etc. see my scripts here:
http://scripts.dragon-it.co.uk/links/email-from-batch
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Steve,

Depending on the criteria that determines "a change has been made", you might want to do a DIR /B option, but hard to know.  The original post seems to indicate the check is for files coming or going, rather than being updated in place.  Need further clarification from the original poster.

I also noticed (I think) a logic flaw in that firs post in the compare section, I think this is what you were after.

@echo off
REM Define log file and directory to check
set olddir=C:\scripts\olddir.txt
set nowdir=c:\scripts\nowdir.txt
set check=c:\my_folder

REM Create directory for scripts if doesn't exist
mkdir c:\scripts 2>NUL

REM Rename previous run log file
if exist "%oldir%" del "%olddir%"
if exist "%nowdir%" (
  rename "%nowdir%" "%olddir%"
  dir "%check%" > "%nowdir%"
  fc "%nowdir%" "%olddir%
  if "%errorlevel%"==0 call :email
) ELSE echo This is the first run so no comparison done
exit /b

Open in new window


~bp
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Commented:
Blimey yes several typos and cock ups there Bill!  You've missed a couple more of mine by the looks, have line #11 in yours %oldir% not %olddir% and in #16 unequal " ", should be

if "%errorlevel%"=="0" call :email
or
if %errorlevel%==0 call :email

Sorry guys, got called away just after I typed it in.   Haven't got time to look any more at the moment either so help yourself Bill if you wish otherwise will look back when I can.

Also I guess handling "no files" as being the same as "no files" the previous run as an error may or not be an issue?

BTW personally I would look at seeing if a log could be made at the production side of these files and check the lat mod. time on that say?
Test your restores, not your backups...
Top Expert 2016
Commented:
So I guess that would make it:

@echo off
REM Define log file and directory to check
set olddir=C:\scripts\olddir.txt
set nowdir=c:\scripts\nowdir.txt
set check=c:\my_folder

REM Create directory for scripts if doesn't exist
mkdir c:\scripts 2>NUL

REM Rename previous run log file
if exist "%olddir%" del "%olddir%"
if exist "%nowdir%" (
  rename "%nowdir%" "%olddir%"
  dir "%check%" > "%nowdir%"
  fc "%nowdir%" "%olddir%
  if %errorlevel%==0 call :email
) ELSE echo This is the first run so no comparison done
exit /b

Open in new window


I'll hold off on any more than that until e hear back from the original poster...

~bp

Author

Commented:
@dragon-it: thank you, every time i ran the script it said "This is the first run so no comparison done". Now i use the correct one:

@billprew: yes the check is for files coming or going. In this folder files come and go, so i must not have the same files in every check i do (45 minutes), but i could have an empty folder, so the error occur when i check that files in the first check are equal to files in the second check, is not an error if there are other files or if the folder is empty. These files are never updated, but only copied in this folder and then deleted, then other files aer copied inside the folder and then delete, and so on. I have updated the script:
@echo off
REM Define log file and directory to check
set olddir=C:\scripts\olddir.txt
set nowdir=c:\scripts\nowdir.txt
set check=c:\my_folder

REM Create directory for scripts if doesn't exist
mkdir c:\scripts 2>NUL

REM Rename previous run log file
if exist "%olddir%" del "%olddir%"
if exist "%nowdir%" (
  rename "%nowdir%" "%olddir%"
  dir "%check%" > "%nowdir%"
  fc "%nowdir%" "%olddir%
  if %errorlevel%==0 call :email
) ELSE echo This is the first run so no comparison done
exit /b

but it gives me a syntax error


Commented:
You will still need the section starting :email after th end of what you have there corrrected by Bill.  Been a busy day today so not much time for EE.

Author

Commented:
rename "%nowdir%" "%olddir%"
should be: rename "%nowdir%" olddir.txt

The other thing i don't understand is that the fc command gives me always %errorlevel% = 0 even if nowdir.txt and olddir.txt are different.
Commented:
Well, in the batch file, "%olddir%" has beed defined as "C:\scripts\olddir.txt". Therefore, assuming that your batch file is in the "C:\scripts\" folder, both options are good.

Concerning your "%errorlevel% = 0" Here is the modified script that resolves this.

I did not read previous posts so that's the best contribution I can bring so far.

 
@echo off

SETLOCAL enabledelayedexpansion

REM Define log file and directory to check
set olddir=C:\scripts\olddir.txt
set nowdir=c:\scripts\nowdir.txt
set check=c:\my_folder

REM Create directory for scripts if doesn't exist
mkdir c:\scripts 2>NUL

REM Rename previous run log file
if exist "%olddir%" del "%olddir%"
if exist "%nowdir%" (
  rename "%nowdir%" "%olddir%"
  dir "%check%" > "%nowdir%"
  fc "%nowdir%" "%olddir%
  if !errorlevel! == 0 call :email
) ELSE echo This is the first run so no comparison done
exit /b

Open in new window

Commented:
Well spotted there ReneGe, comes of moving it to () later...

Commented:
Steve, thanks for the cheering. Glad I helped.

Cheers,
Rene

Author

Commented:
But yet it only works if it is - rename "%nowdir%" olddir.txt - , otherwise it gives me a syntax error.
!errorlevel! gives me always -1 whether the content in the folder change or not.
Commented:
Another good point, path specified for both files is not allowed on rename.

fc though SHOULD work to return "0" if the files are the same, and "1" if there are differences.

Another syntax that can be used is using && to say run this if the errorlevel is 0:

fc "%nowdir%" "%olddir%  && call :email

and leave out the errorlevel check.  

I think that makes it as below.  Can't do any more on it today so will leave you with Rene / Bill if needed.
@echo off  
  
REM Define log file and directory to check  
set dir=c:\scripts
set olddir=olddir.txt  
set nowdir=nowdir.txt  
set check=c:\my_folder  
  
REM Create directory for scripts if doesn't exist  
mkdir %dir% 2>NUL  
  
REM Rename previous run log file  
if exist "%dir%\%olddir%" del "%dir%\%olddir%"  
if exist "%dir%\%nowdir%" (  
  rename "%dir%\%nowdir%" "%olddir%"  
  dir "%check%" > "%dir%\%nowdir%"  
  fc "%dir%\%nowdir%" "%dir%\%olddir% && call :email  
) ELSE echo This is the first run so no comparison done  
exit /b

:email
REM bit as before.

Open in new window

Thanks dragon-it (it because you are italian?). I did some change and now it works perfectly:

@echo off
REM Define log file and directory to check
set olddir=C:\scripts\olddir.txt
set nowdir=c:\scripts\nowdir.txt
set check=c:\my_folder

REM Create directory for scripts if doesn't exist
mkdir c:\scripts 2>NUL

REM Rename previous run log file
if exist "%olddir%" del "%olddir%"
if exist "%nowdir%" (
  rename "%nowdir%" olddir.txt
  dir "%check%" > "%nowdir%"
  fc "%nowdir%" "%olddir%" && call :email
) ELSE echo This is the first run so no comparison done
exit /b

Open in new window


Thank you everyone for helping me.

Commented:
Nah sorry, IT = Information Technology :-)  Seemed like a good idea at the time when I was looking for a domain name.

Please split the points up there as I'm sure you were going to anyway omething of a team effort after my original bodge script!

Steve
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Whenever I see Steve's handle of "dragon-it" I always wonder "what's he dragging anyway?" ...

:-)

Author

Commented:
Of course ;)

Author

Commented:
@billprew: you were right! dir /B
In fact, trying the script, i noticed that even if the content of the folder is always the same, no mail is sent because of the summary that, in my case, change but must not be considered.

Author

Commented:
I accepted my own comment because it's the script that works based on the scripts and suggestions in the other posts.

Commented:
thanks for the points, want one of my best dealt with questions!

Commented:
predictive text strikes again... Wasn't not want

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial