有条件地将文件从多个文件夹复制到多个文件夹

时间:2016-07-13 19:45:06

标签: .net powershell

我需要一个脚本,允许我将文件放入监视文件夹中的4个文件夹中的任何一个文件夹中,然后根据文件夹中的文件夹将该文件复制到两个文件夹中。

  

观看文件夹:C:\ Users \ Username \ Desktop \ PS DROP FOLDERS

     

C:\ Users \ Username \ Desktop \ PS DROP FOLDERS \ 18x23   
复制到C:\ Users \ Username \ Desktop \ IJET RIP FOLDERS \ LARGE 18x23   
复制到C:\ Users \ Username \ Desktop \ STE RIP - HR FOLDERS \ LARGE 16x20-18x23

     

C:\ Users \ Username \ Desktop \ PS DROP FOLDERS \ LARGE   
复制到C:\ Users \ Username \ Desktop \ IJET RIP FOLDERS \ LARGE 16x20   
复制到C:\ Users \ Username \ Desktop \ STE RIP - HR FOLDERS \ LARGE 16x20-18x23

     

C:\ Users \ Username \ Desktop \ PS DROP FOLDERS \ MANUAL   
复制到C:\ Users \ Username \ Desktop \ IJET RIP FOLDERS \ MANUAL 15x15   
复制到C:\ Users \ Username \ Desktop \ STE RIP - HR FOLDERS \ MANUAL 15x15

     

C:\ Users \ Username \ Desktop \ PS DROP FOLDERS \ SMALL   
复制到C:\ Users \ Username \ Desktop \ IJET RIP FOLDERS \ SMALL 16x20   
复制到C:\ Users \ Username \ Desktop \ STE RIP - HR FOLDERS \ SMALL 16x20

下面是我目前对于监视文件夹部分的代码(取自另一个Q / A),但我无法找出有条件地复制到多个文件夹的逻辑。似乎比IF语句有更好的方法。

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Users\Username\Desktop\PS DROP FOLDERS"
$watcher.Filter = "*.ps"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER A EVENT IS DETECTED
$action = {

HELP INQUIRY CODE HERE

}
### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY  
$created = Register-ObjectEvent $watcher "Created" -Action $action
### $changed = Register-ObjectEvent $watcher "Changed" -Action $action
### $deleted = Register-ObjectEvent $watcher "Deleted" -Action $action
### $renamed = Register-ObjectEvent $watcher "Renamed" -Action $action
while ($true) {sleep 5}

抱歉缺乏技巧。我不指望解决方案,但一些指导会很好。谢谢。

P.S。这是为两个不同的成像系统将文件路由到RIP程序的热文件夹。

1 个答案:

答案 0 :(得分:0)

感谢TheMadTechnician我使用switch命令提出了一个解决方案。希望这也有助于其他人。

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\Users\Username\Desktop\PS DROP FOLDERS"
    $watcher.Filter = "*.ps"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER A EVENT IS DETECTED
       $action = { 
                   #GET NAME OF PARENT DIRECTORY FOR SWITCH AND OUTPUT VALUES IN CONSOLE
                   $item = $Event.SourceEventArgs.FullPath
                   $path = Split-Path (Split-Path $item -Parent) -leaf
                   Write-Host "item : " $item
                   Write-Host "path : " $path

      switch ($path)
    { 
        "LARGE" {
                   $destination = @("C:\Users\Username\Desktop\IJET RIP FOLDERS\LARGE 16x20","C:\Users\Username\Desktop\STE RIP - HR FOLDERS\LARGE 16x20-18x23")

                   foreach ($dir in $destination)
                   {
                   Copy-Item -Path $item -Destination $dir
                   }
                } 
        "SMALL" {
                   $destination = @("C:\Users\Username\Desktop\IJET RIP FOLDERS\SMALL 16x20","C:\Users\Username\Desktop\STE RIP - HR FOLDERS\SMALL 16x20")

                   foreach ($dir in $destination)
                   {
                   Copy-Item -Path $item -Destination $dir
                   }
                }
        "MANUAL" {
                   $destination = @("C:\Users\Username\Desktop\IJET RIP FOLDERS\MANUAL 15x15","C:\Users\Username\Desktop\STE RIP - HR FOLDERS\MANUAL 15x15")

                   foreach ($dir in $destination)
                   {
                   Copy-Item -Path $item -Destination $dir
                   }
                }
        "18x23" {
                   $destination = @("C:\Users\Username\Desktop\IJET RIP FOLDERS\LARGE 18x23","C:\Users\Username\Desktop\STE RIP - HR FOLDERS\LARGE 16x20-18x23")

                   foreach ($dir in $destination)
                   {
                   Copy-Item -Path $item -Destination $dir
                 }
              }
            }
          }
### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY  
    $created = Register-ObjectEvent $watcher "Created" -Action $action
### $changed = Register-ObjectEvent $watcher "Changed" -Action $action
### $deleted = Register-ObjectEvent $watcher "Deleted" -Action $action
### $renamed = Register-ObjectEvent $watcher "Renamed" -Action $action
    while ($true) {sleep 5}