执行批处理文件,当一个新文件添加到文件夹 [英] Execute batch file when a new file is added to a folder

查看:197
本文介绍了执行批处理文件,当一个新文件添加到文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面,我从<一个得到了批处理文件示例href=\"http://stackoverflow.com/questions/4230976/batch-file-to-monitor-additions-to-download-folder\">batch文件监视增补下载文件夹。我想修改此,以便它遍历所有子文件夹。

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

 关闭@echo
如果不存在C:\\ OldDir.txt回声。 &GT; C:\\ OldDir.txt
DIR / BD:\\我的文件夹&GT; C:\\ NewDir.txt
等于=无
FC C:\\ OldDir.txt C:\\ NewDir.txt |发现/我没有分歧&GT; NUL&功放;&安培;等于= YES
复制/ Y C:\\ Newdir.txt C:\\ OldDir.txt&GT; NUL
如果相等%==%是转到:EOF
REM批处理文件行去这里


解决方案

请....使它PowerShell的?

http://archive.msdn.microsoft.com/PowerShellPack 包含了功能启动FileSystemWatcher的

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


Powershell.com 有样品

下面是从<一个样本href=\"http://social.technet.microsoft.com/Forums/en/ITCG/thread/c75c7bbd-4e32-428a-b3dc-815d5c42fd36\"相对=nofollow>此线程:

  $文件夹='&LT;到文件夹的完整路径看&GT;'
$过滤器=*。*#&LT; - 这根据您的要求设置
$目标='&LT;完整路径目标文件夹&GT;'
$ FSW =新对象IO.FileSystemWatcher $文件夹,$过滤,物业@ {
 IncludeSubdirectories = $#真LT - ; - 将它根据您的要求
 NotifyFilter = [IO.NotifyFilters]'文件名,LastWrite
}
$ onCreated =注册-ObjectEvent $ FSW创建-SourceIdentifier FileCreated -Action {
 $ PATH = $ Event.SourceEventArgs.FullPath
 $名称= $ Event.SourceEventArgs.Name
 $一changeType = $ Event.SourceEventArgs.ChangeType
 $的timeStamp = $ Event.TimeGenerated
 写主机文件'$姓名$一changeType在$时间戳
 布展项目$ PATH -Destination $目的地-Force -Verbose#强制将覆盖同名文件
}

最后,注销订阅:注销-事件-SourceIdentifier FileCreated

I am using the batch file example below that I got from batch file to monitor additions to download folder. I would like to modify this so that it loops through all the subfolders.

Anyone have any ideas on how to do this?

@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

解决方案

Please.... make it powershell?

http://archive.msdn.microsoft.com/PowerShellPack contains the function Start-FileSystemWatcher.

Please don't inflict this batch file on any system.


Powershell.com has samples

Here's a sample from this thread:

$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
}

Eventually, unregister the subscription: Unregister-Event -SourceIdentifier FileCreated

这篇关于执行批处理文件,当一个新文件添加到文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆