Folder Monitoring with Batch File

两盒软妹~` 提交于 2019-12-03 22:46:03

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.

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.

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

@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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!