将新文件添加到文件夹时执行批处理文件

sam*_*mmy 3 batch-file

我正在使用下面的批处理文件示例,该示例是从批处理文件中获取的,以监视下载文件夹的添加内容。我想修改它,使其循环遍历所有子文件夹。

有人对如何做到这一点有任何想法吗?

@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
Run Code Online (Sandbox Code Playgroud)

seh*_*ehe 5

请....使它成为powershell?

http://archive.msdn.microsoft.com/PowerShellPack 包含该函数Start-FileSystemWatcher

请不要在任何系统上使用此批处理文件。


Powershell.com有示例

这是该线程的示例:

$folder = '<full path to the folder to watch>'
$filter = '*.*'                             # <-- set this according to your requirements
$destination = '<full path to the destination folder>'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
 IncludeSubdirectories = $true              # <-- set this according to your requirements
 NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
 $path = $Event.SourceEventArgs.FullPath
 $name = $Event.SourceEventArgs.Name
 $changeType = $Event.SourceEventArgs.ChangeType
 $timeStamp = $Event.TimeGenerated
 Write-Host "The file '$name' was $changeType at $timeStamp"
 Move-Item $path -Destination $destination -Force -Verbose # Force will overwrite files with same name
}
Run Code Online (Sandbox Code Playgroud)

最后,取消注册订阅: Unregister-Event -SourceIdentifier FileCreated