使用Powershell检测主文件夹中的多个子文件夹

时间:2014-12-19 11:45:20

标签: powershell powershell-v2.0 powershell-v3.0

更新:

您好。如果我的问题听起来很模糊,请道歉。当我在下面同时运行两个脚本时,ES和LTC子文件夹中的所有csv文件最终都在一个文件夹而不是两个文件夹中。我有两个单独的脚本 监视两个子文件夹LTC和ES,并将文件复制到文件夹。

LTC脚本:

 $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") {

  Copy-Item -Path $folder -Destination $destination
 }
}

  $renamed = Register-ObjectEvent $fsw -EventName Renamed -Action {
  $item = Get-Item $eventArgs.FullPath
  If ($item.Name -ilike "LTC") {

  Copy-Item -Path $folder -Destination $destination 
 }
}

ES脚本:

    $folder = 'C:\2014-15'
    $destination = 'N:\Test1'

  $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 "ES") {

  Copy-Item "$item\*.csv" -Destination $destination

  }

 }
  $created = Register-ObjectEvent $fsw -EventName Created -Action {

  $item =  Get-Item $eventArgs.FullPath

  If ($item.Name -ilike "ES") {

  Copy-Item "$item\*.csv" -Destination $destination

   }

 }

1 个答案:

答案 0 :(得分:0)

 $folder = 'C:\2014-15'
 $destination = 'N:\Test'
 $destination1 = 'N:\Test1'

 $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") {

  Copy-Item -Path $folder -Destination $destination
 }
}

  $renamed = Register-ObjectEvent $fsw -EventName Renamed -Action {
  $item = Get-Item $eventArgs.FullPath
  If ($item.Name -ilike "LTC") {

  Copy-Item -Path $folder -Destination $destination 
 }
}


  $fsw = New-Object  System.IO.FileSystemWatcher $folder  -Property @{

  IncludeSubdirectories = $true

  NotifyFilter = [IO.NotifyFilters]'DirectoryName'

 }

  $created = Register-ObjectEvent $fsw -EventName Created -Action {

  $item1 =  Get-Item $eventArgs.FullPath

  If ($item1.Name -ilike "ES") {

  Copy-Item "$item1\*.csv" -Destination $destination1

  }

 }
  $created = Register-ObjectEvent $fsw -EventName Created -Action {

  $item1 =  Get-Item $eventArgs.FullPath

  If ($item1.Name -ilike "ES") {

  Copy-Item "$item1\*.csv" -Destination $destination1

   }

 }