[英]Detecting csv files in newly sub-folder in PowerShell
I have a folder called C:\\2014-15 and new sub folders are created every month which contain csv files ie 我有一个名为C:\\ 2014-15的文件夹,每个月都会创建一个包含CSV文件的新子文件夹,即
How to write a script which would detect when a LTC sub folder is created for every month and move the csv files to N:\\Test? 如何编写一个脚本来检测每个月何时创建LTC子文件夹并将csv文件移动到N:\\ Test?
UPDATED: 更新:
$folder = 'C:\2014-15'
$filter = '*.*'
$destination = 'N:Test\'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubdirectories = $true
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
Copy-Item -Path $path -Destination $destination
}
The error I get is: 我得到的错误是:
Register-ObjectEvent : Cannot subscribe to event. Register-ObjectEvent:无法订阅事件。 A subscriber with source identifier 'FileCreated' already exists.
具有源标识符“ FileCreated”的订户已经存在。 At line:8 char:34 + $onCreated = Register-ObjectEvent <<<< $fsw Created -SourceIdentifier FileCreated -Action { + CategoryInfo : InvalidArgument: (System.IO.FileSystemWatcher:FileSystemWatcher) [Register-ObjectEvent], ArgumentException + FullyQualifiedErrorId : SUBSCRIBER_EXISTS,Microsoft.PowerShell.Commands.RegisterObjectEventCommand
在第8行:char:34 + $ onCreated = Register-ObjectEvent <<<< $ fsw创建-SourceIdentifier FileCreated -Action {+ CategoryInfo:InvalidArgument:(System.IO.FileSystemWatcher:FileSystemWatcher)[Register-ObjectEvent],ArgumentException + FullyQualifiedErrorId :SUBSCRIBER_EXISTS,Microsoft.PowerShell.Commands.RegisterObjectEventCommand
Notify on a different event: [IO.NotifyFilters]'DirectoryName'
. 通知其他事件:
[IO.NotifyFilters]'DirectoryName'
。 This removes the need for $filter
since filename events are not relevant. 由于文件名事件不相关,因此不需要
$filter
。
You should also notify for renamed folders and created folders, making your final script something like this 您还应该通知重命名的文件夹和创建的文件夹,使最终脚本如下所示
$folder = 'C:\2014-15'
$destination = 'N:\Test'
$fsw = New-Object System.IO.FileSystemWatcher $folder -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'DirectoryName'
}
$created = Register-ObjectEvent $fsw -EventName Created -Action {
$item = Get-Item $eventArgs.FullPath
If ($item.Name -ilike "LTC") {
# do stuff:
Copy-Item -Path $folder -Destination $destination
}
}
$renamed = Register-ObjectEvent $fsw -EventName Renamed -Action {
$item = Get-Item $eventArgs.FullPath
If ($item.Name -ilike "LTC") {
# do stuff:
Copy-Item -Path $folder -Destination $destination
}
}
From the same console you can unregister since that console knows about $created
and $renamed
: 您可以从同一控制台注销,因为该控制台知道
$created
和$renamed
:
Unregister-Event $created.Id
Unregister-Event $renamed.Id
Otherwise you need to use this which is bit uglier: 否则,您将需要使用以下代码:
Unregister-Event -SourceIdentifier Created -Force
Unregister-Event -SourceIdentifier Renamed -Force
Also, thanks for the question. 另外,感谢您的提问。 I didn't realise these event captures existed in powershell until now...
直到现在我才意识到这些事件捕获都存在于powershell中...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.