windows 批处理文件以监视添加到下载文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4230976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
batch file to monitor additions to download folder
提问by Nick Sinas
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.
我不想计算子文件夹,只计算目录中的文件夹和文件。
我一直在看这个:dir "C:\folder" /b/a |find /v /c "::"
但我不知道如何存储这个值并将其测试为 < 或 >。
也许有更好的方法来做到这一点,但我现在想不出一个。也许维护一个列表,如果新列表有一个新文件,则运行批处理脚本,用新列表替换旧列表,我不确定如何解决这个问题。
回答by Dean Taylor
Answer 1:
答案 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.
下面的代码片段应该能让你朝着正确的方向前进。它用于dir /b
获取文件的原始列表并使用fc
(文件比较)来检查每次执行检查之间的差异。
You could use the Task Scheduler to launch this batch file once every x minutes:
您可以使用任务计划程序每 x 分钟启动一次此批处理文件:
@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:
答案 2:
I have always liked a library of batch functionsby Ritchie Lawrence. One of those functions is called GetDirStats
.
我一直很喜欢Ritchie Lawrence的批处理函数库。其中一个函数称为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.
该GetDirStats
函数返回指定目录的文件数、子目录数和总大小。可能会很方便以供将来参考。虽然它只在 NT4/2000/XP/2003 上测试过。
只需更改compact/s
为compact
不扫描子文件夹即可。
回答by Nick Sinas
: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