Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 无法根据Name属性复制文件_File_Powershell - Fatal编程技术网

File 无法根据Name属性复制文件

File 无法根据Name属性复制文件,file,powershell,File,Powershell,我编写了一个Powershell脚本来获取文件夹的内容,并检查项目是否符合特定条件。如果他们这样做了,他们应该被移动到一个特定的目录 我面临的挑战是,脚本将移动文件夹中的所有内容,无论它们是否满足标准 $source = "E:\GatewaysRenditionArea\pdf" $fileList = Get-ChildItem -Path $source ForEach ($file in $fileList) { if ($file.FullName-like "*PID*.p

我编写了一个Powershell脚本来获取文件夹的内容,并检查项目是否符合特定条件。如果他们这样做了,他们应该被移动到一个特定的目录

我面临的挑战是,脚本将移动文件夹中的所有内容,无论它们是否满足标准

$source = "E:\GatewaysRenditionArea\pdf"
$fileList = Get-ChildItem -Path $source
ForEach ($file in $fileList) 
{
    if ($file.FullName-like "*PID*.pdf")
    {
        copy-Item -Path $source -Destination "E:\Gateway\GatewaysStagingArea\PID - Piping and Instrumentation Diagram"
    }
    Else
    {
        if ($file.FullName -like "*BOM*.pdf")
        {
            Copy-Item -Path $source -Destination "E:\Gateway\GatewaysStagingArea\BOM - Bill of Materials"
        }
    }
}

您可以大大简化此过程:

$Path = 'E:\GatewaysRenditionArea\pdf'

Get-ChildItem -Path $Path -Filter '*bom*.pdf' |
    Copy-Item -Destination 'E:\Gateway\GatewaysStagingArea\BOM - Bill of Materials'

Get-ChildItem -Path $Path -Filter '*pid*.pdf' |
    Copy-Item -Destination 'E:\Gateway\GatewaysStagingArea\PID - Piping and Instrumentation Diagram'

请记住:左筛选,右格式化。

您正在比较FullName属性。这是故意的吗?我以前比较过Name属性,但由于没有得到预期的结果,我将其更改为Nmae属性。使用这种方法,意味着不需要if/Else语句,对吗?