简体   繁体   English

使用特定文件名移动文件

[英]Moving Files with certain File Name

I wrote a script which detects a folder called Post within the subfolders of W:\\SUS Test2 and moves CSV file which has "EMMain" in the filename. 我编写了一个脚本,该脚本在W:\\SUS Test2的子文件夹中检测到名为Post的文件夹,并移动文件名中包含“ EMMain”的CSV文件。 But the problem I have is the CSV files doesn't copy to the destination folder W:\\SUS Test3 it copies it to W:\\SUS Test2 folder. 但是我的问题是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
  }
}

You are trying to access the $destination variable in a portion of code that it is not available in. The script block in your Register-OjbectEvent will not have access to this variable. 您正在尝试在一部分不可用的代码中访问$destination变量Register-OjbectEvent的脚本块将无权访问此变量。 Try declaring $destination in the script block directly. 尝试直接在脚本块中声明$ 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 
    } 
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM