Avatar of mathayus_ITA
mathayus_ITAFlag for Italy

asked on 

Script to check changes in a folder every 45 minutes

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.
Microsoft DOSVB Script

Avatar of undefined
Last Comment
Steve Knight
SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Some other methods of sending an email from batch etc. see my scripts here:
http://scripts.dragon-it.co.uk/links/email-from-batch
Avatar of Bill Prew
Bill Prew

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
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

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?
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of mathayus_ITA
mathayus_ITA
Flag of Italy image

ASKER

@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


Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of mathayus_ITA
mathayus_ITA
Flag of Italy image

ASKER

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.
SOLUTION
Avatar of ReneGe
ReneGe
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Well spotted there ReneGe, comes of moving it to () later...
Avatar of ReneGe
ReneGe
Flag of Canada image

Steve, thanks for the cheering. Glad I helped.

Cheers,
Rene
Avatar of mathayus_ITA
mathayus_ITA
Flag of Italy image

ASKER

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.
SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of mathayus_ITA
mathayus_ITA
Flag of Italy image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of Bill Prew
Bill Prew

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

:-)
Avatar of mathayus_ITA
mathayus_ITA
Flag of Italy image

ASKER

Of course ;)
Avatar of mathayus_ITA
mathayus_ITA
Flag of Italy image

ASKER

@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.
Avatar of mathayus_ITA
mathayus_ITA
Flag of Italy image

ASKER

I accepted my own comment because it's the script that works based on the scripts and suggestions in the other posts.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

thanks for the points, want one of my best dealt with questions!
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

predictive text strikes again... Wasn't not want
VB Script
VB Script

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic, but with some important differences. VBScript is commonly used for automating administrative and other tasks in Windows operating systems (by means of the Windows Script Host) and for server-side scripting in ASP web applications. It is also used for client-side scripting in Internet Explorer, specifically in intranet web applications.

39K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo