Monitor a folder for file changes. If changed then run bat

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Andrius
Posts: 37
Joined: 26 Jun 2012 15:15

Monitor a folder for file changes. If changed then run bat

#1 Post by Andrius » 26 Jan 2015 17:07

Hey everyone,

Id like to monitor a folder and all of it's sub folders. If a file is added or modified within this folder structure then run another bat file I already have written.

Example:
Monitor c:\art_assets
if anything changes within this folder or any of it's sub folders then run "build_assets.bat"

Id really appreciate the help :) You guys are the best and have already saved me a ton of time in the past.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Monitor a folder for file changes. If changed then run

#2 Post by Squashman » 26 Jan 2015 17:13

Perfect application for this is called Watch Directory. License is cheap and it works great.

Andrius
Posts: 37
Joined: 26 Jun 2012 15:15

Re: Monitor a folder for file changes. If changed then run

#3 Post by Andrius » 26 Jan 2015 17:26

Thanks for the tip Squashman. Ive spoken to my management about this application but was told not to concern myself with such matters. So whatever I go with has to be made by myself :( Ill definitly look into this app though for home / personal use! Hopefully a bat file isnt too difficult for this task. I have no idea if what I am asking is a huge undertaking or not unfortunately :(

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Monitor a folder for file changes. If changed then run

#4 Post by Squashman » 26 Jan 2015 18:17

It is your concern because you are the one who has to implement this task. I pretty much work for the largest printing company in the world and we have dozens upon dozens of licenses for Watch Directory. They use it here in the states and our facilities in Europe as well. It has so much bang for the buck it is not even funny.

I would not be able to automate 600+ tasks daily without it.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Monitor a folder for file changes. If changed then run

#5 Post by Squashman » 27 Jan 2015 08:28

Here is an option for you.

Create a text file with the contents of the TREE of folders and files.
The text file will contain the full path to the file, the date modified and the file size.
This can easily be done with the DIR command executed within the FOR command.

Code: Select all

@echo off
pushd "c:\art_assets"
FOR /F "delims=" %%G IN ('DIR /B /S') DO >>"C:\path to BatchFiles\TreeExisting.txt" ECHO %%~G,%%~tG,%%~zG
popd

You would run the above code once you know everything is up to date and you have already run your build command. This code would only be run once. This will give you a base listing of what is currently in your folder and sub folders to compare to another file that will be created.

Now every X minutes you could schedule this batch file to run to compare the previous tree structure to the current tree structure.

Code: Select all

@echo off
pushd "c:\art_assets"
FOR /F "delims=" %%G IN ('DIR /B /S') DO >>"C:\path to BatchFiles\TreeCurrent.txt ECHO %%~G,%%~tG,%%~zG
popd

FC TreeExisting.txt TreeCurrent.txt >nul

IF %ERRORLEVEL% GTR 0 THEN (
    CALL build_assets.bat
    del TreeExisting.txt
    ren TreeCurrent.txt TreeExisting.txt
)


Put all your Batch files that you will be running into a folder outside of your folder tree that you are monitoring. This is where the Tree listings will exist as well.

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

Re: Monitor a folder for file changes. If changed then run

#6 Post by penpen » 27 Jan 2015 09:23


Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Monitor a folder for file changes. If changed then run

#7 Post by Squashman » 27 Jan 2015 09:58

Some PowerShell and Vbscript options here as well.
http://superuser.com/questions/226828/h ... is-created

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Monitor a folder for file changes. If changed then run

#8 Post by Samir » 02 Feb 2015 11:02

Squashman wrote:Here is an option for you.

Create a text file with the contents of the TREE of folders and files.
The text file will contain the full path to the file, the date modified and the file size.
This can easily be done with the DIR command executed within the FOR command.

Code: Select all

@echo off
pushd "c:\art_assets"
FOR /F "delims=" %%G IN ('DIR /B /S') DO >>"C:\path to BatchFiles\TreeExisting.txt" ECHO %%~G,%%~tG,%%~zG
popd

You would run the above code once you know everything is up to date and you have already run your build command. This code would only be run once. This will give you a base listing of what is currently in your folder and sub folders to compare to another file that will be created.

Now every X minutes you could schedule this batch file to run to compare the previous tree structure to the current tree structure.

Code: Select all

@echo off
pushd "c:\art_assets"
FOR /F "delims=" %%G IN ('DIR /B /S') DO >>"C:\path to BatchFiles\TreeCurrent.txt ECHO %%~G,%%~tG,%%~zG
popd

FC TreeExisting.txt TreeCurrent.txt >nul

IF %ERRORLEVEL% GTR 0 THEN (
    CALL build_assets.bat
    del TreeExisting.txt
    ren TreeCurrent.txt TreeExisting.txt
)


Put all your Batch files that you will be running into a folder outside of your folder tree that you are monitoring. This is where the Tree listings will exist as well.
This is the type of methodology I was thinking of. Or just write the whole dir /s to a file and then run it again after x mins and compare the files. If they don't compare perfect, something has changed.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Monitor a folder for file changes. If changed then run

#9 Post by Squashman » 02 Feb 2015 11:22

Samir wrote:This is the type of methodology I was thinking of. Or just write the whole dir /s to a file and then run it again after x mins and compare the files. If they don't compare perfect, something has changed.

That is basically what I said I was doing.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Monitor a folder for file changes. If changed then run

#10 Post by Samir » 02 Feb 2015 12:03

Squashman wrote:
Samir wrote:This is the type of methodology I was thinking of. Or just write the whole dir /s to a file and then run it again after x mins and compare the files. If they don't compare perfect, something has changed.

That is basically what I said I was doing.
Agh, I missed the FC somehow. I only skimmed your code.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Monitor a folder for file changes. If changed then run

#11 Post by thefeduke » 22 May 2015 02:01

I have had success in doing just this kind of monitoring using a open freeware software package that i got from
https://sourceforge.net/projects/freefilesync/?source=directory
This web page has an extensive list of features.

FreeFileSync has two components. One does various kinds of backups(which I did not use) and is triggered by the activity monitoring component RealtimeSync which presents a menu window to specify
    The folders to watch
    A command line

This command line is just the name of my previously written batch file with parameters as would appear on a call statement. I set a monitor idle time of a few minutes from first activity to trigger the backup to external media.

This has triggered flawlessly since I installed it into the startup folder last year.

John

Post Reply