Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
762 views
in Technique[技术] by (71.8m points)

windows - batch file to monitor additions to download folder

I need a batch file that monitors additions to my Downloads folder, but only new additions. Something like this:

:START  

NumOldFiles = GetNumberOfFilesOld  

Delay_30_Seconds  

NumNewFiles = GetNumberOfFilesNew  

if(NumFilesOld < NumFilesNew)  
  run_another_batch_file_I_wrote
  goto START
else
  goto START

I do not want to count subfolders, just the folders and files in the directory.
I have been looking at this:
dir "C:folder" /b/a |find /v /c "::"
but I don't know how to store this value and test it as < or >.
Maybe there is a better way to do this, but I can't think of one right now. Maybe maintain a list and if the new list has a new file run the batch script, replace the old list with the new list, I'm not really sure how to go about this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Answer 1:

The following snippet should get you going in the right direction. It uses dir /b to get a raw list of files and uses fc (file compare) to check for differences between each execution of the check.

You could use the Task Scheduler to launch this batch file once every x minutes:

@echo off
if not exist c:OldDir.txt echo. > c:OldDir.txt
dir /b "d:My Folder" > c:NewDir.txt
set equal=no
fc c:OldDir.txt c:NewDir.txt | find /i "no differences" > nul && set
equal=yes
copy /y c:Newdir.txt c:OldDir.txt > nul
if %equal%==yes goto :eof
rem Your batch file lines go here

Answer 2:

I have always liked a library of batch functions by Ritchie Lawrence. One of those functions is called GetDirStats.

The GetDirStats function returns the number of files, subdirectories and total size of a specified directory. Might be handy for future reference. Although it's only tested on NT4/2000/XP/2003.
Just change compact/s to compact to not scan subfolders.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...