开发者

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 "::"

b开发者_C百科ut 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.


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.


:START
cls
set /a Old = 0
set /a New = 0
echo Counting files in folder..
for /f "tokens=*" %%P IN ('dir "C:\Users\..." /A /b') do (set /a Old += 1)
set Old
:: delay 60 sec
echo Delaying 60 seconds... (drop new file in)
ping 1.1.1.1 -n 1 -w 60000>nul
echo Checking for new files..
for /f "tokens=*" %%P IN ('dir "C:\Users\S..." /A /b') do (set /a New += 1)
set New
goto COMPARE

:COMPARE
echo Comparing number of files
if %New% gtr %Old% goto NEWF
goto OLDF

:NEWF
echo New File Detected.
echo.
goto START

:OLDF
echo No New Files.
PAUSE
echo Restarting
echo.
goto START
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜