2016-01-25 37 views
0

我編寫了一個腳本,用於檢測W:\SUS Test2子文件夾中名爲Post的文件夾,並移動文件名中包含「EMMain」的CSV文件。但我的問題是CSV文件不會複製到目標文件夾W:\SUS Test3它將其複製到W:\SUS Test2文件夾。使用特定文件名移動文件

$path = "W:\SUS Test2\" 
$destination = "W:\SUS Test3\" 
$fsw = New-Object System.IO.FileSystemWatcher $path -Property @{ 
    IncludeSubdirectories = $true 
    NotifyFilter = [IO.NotifyFilters]'DirectoryName 
} 

$createdLTC = Register-ObjectEvent $fsw -EventName Created -Action { 
    $item = Get-Item $eventArgs.FullPath 

    if ($item.Name -like "Post") { 
    Get-ChildItem $item -filter "*EMMain*" | Copy-Item -dest $destination 
    } 
} 

回答

0

您試圖訪問的代碼的一部分,它是不是在現有的$destination變量。在你Register-OjbectEvent將不能訪問此變量的腳本塊。嘗試直接在腳本塊中聲明$ destination。

$createdLTC = Register-ObjectEvent $fsw -EventName Created -Action { 
    $destination = "W:\SUS Test3\" 
    $item = Get-Item $eventArgs.FullPath 
    If ($item.Name -like "Post") { 
      Get-ChildItem $item -filter "*EMMain*" | Copy-Item -dest $destination 
    } 
    } 
相關問題