简体   繁体   English

在PowerShell中新子文件夹中检测CSV文件

[英]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文件的新子文件夹,即

  1. C:\\2014-15\\Month 1\\LTC C:\\ 2014-15 \\ Month 1 \\ LTC
  2. C:\\2014-15\\Month 2\\LTC C:\\ 2014-15 \\ Month 2 \\ LTC
  3. C:\\2014-15\\Month 3\\LTC C:\\ 2014-15 \\ Month 3 \\ LTC

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

Credit to this post. 归功于此职位。

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.

相关问题 使用Powershell将文件夹结构中的文件复制到其各自的子文件夹 - Copy files in a folder structure to their respective sub-folder with Powershell 如何使用powershell从文件夹的根目录中仅删除子文件夹和子文件夹内容而不是单个文件 - How to delete only the sub-folders and sub-folder contents but not individual files from the root of a folder using powershell 扫描Powershell中每个文件夹的特定子文件夹 - Scan specific sub-folder of every folder in powershell 在每个文件夹中创建一个子文件夹然后将所有文件和文件夹移动到该子文件夹中的代码是什么? - What is a code to create a sub-folder in each folder and then move all the files and folders into that sub-folder? 将子文件夹名称导出为路径,并为不同位置的每个父子文件夹创建 csv - Export sub-folder name as path and create a csv for each parent sub-folder in a different location 创建子文件夹结构 - Creating a sub-folder structure 使用 powershell 将子文件夹内容上移一级; 不同的父文件夹 - Using powershell to move sub-folder contents up one level; different parent folders 具有最新写访问权限的输出子文件夹 - Output sub-folder with the latest write access Powershell 尝试读取新创建的文件夹中的文件时出现“权限被拒绝” - Powershell 'Permisson Denied' when trying to read files in a newly created folder Powershell将CSV中的数据与文件夹中的文件进行比较 - Powershell comparing data in a CSV against files in a folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM