Reputation: 183
I want to monitor my folder if new file added or not. Then If added I would like to execute some files. But I don't want to use third party app.
I have some ideas but I don't know how to do that.
This is my folder; D:\MonitoringFolder
So every hour batch file will check the files inside of it and writes them into a txt.
dir /b "D:\MonitoringFolder" > old.txt
Old.txt is --> string 1 , string 2, string 3
After one hour, batch file will check it later and writes again into another txt.
dir /b "D:\MonitoringFolder" > new.txt
New.txt is --> string 1, string 2, string 3, string 5
Then it will compare new.txt and old.txt. So string 5 added recently. It will prompt a window and says "String 5" added!. Or new file added (removed).
I want to do that If someone could show me a way to do this I would appreciate that.
Upvotes: 1
Views: 8256
Reputation: 5775
Script MONITOR.cmd scheduled to run every now and then:
IF EXIST NEW.TXT DEL NEW.TXT
FOR /F "tokens=*" %%* IN ('DIR /S /B /ON "D:\MonitoringFolder"') DO ECHO "%%*">>NEW.TXT
FOR /F "tokens=*" %%* IN (NEW.TXT) DO (FIND %%* OLD.TXT >NUL || START CMD /K INSERTED.cmd %%*)
FOR /F "tokens=*" %%* IN (OLD.TXT) DO (FIND %%* NEW.TXT >NUL || START CMD /K DELETED.cmd %%*)
DEL OLD.TXT
REN NEW.TXT OLD.TXT
Script INSERTED.cmd will create new window prompting for action on appearing of a new file:
ECHO Inserted new file %1
DIR %1
PAUSE & EXIT
Script DELETED.cmd will create new window prompting for action on disappearing of an old file:
ECHO Deleted file %1
PAUSE & EXIT
Subfolders are monitored, too. It worked for me even with spaces and accented characters in filename.
Upvotes: 1
Reputation: 943
Since you're already dumping the output every hour, just execute this command from the prompt:
fc /u old.txt new.txt
It will tell you, if any, which differences exist between the two files.
Upvotes: 0
Reputation: 37569
@ECHO OFF &SETLOCAL disableDelayedExpansion
SET "SaveFile=Save.File"
IF NOT EXIST "%SaveFile%" GOTO:cont
DIR /b /a-d | FINDSTR /vg:"%SaveFile%">nul||EXIT /b
ECHO(execute some programs here
:cont
>"%SaveFile%" DIR /b /a-d
Upvotes: 0
Reputation: 1631
Maybe you're going to write batch scripts (for scanning folder and compare results) and schedule them with a scheduler like cron (Linux) or windows task scheduler every hours for e periodical checking. Some documents here : http://support.microsoft.com/kb/308569 , http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800
Upvotes: 0