File 无法根据Name属性复制文件
我编写了一个Powershell脚本来获取文件夹的内容,并检查项目是否符合特定条件。如果他们这样做了,他们应该被移动到一个特定的目录 我面临的挑战是,脚本将移动文件夹中的所有内容,无论它们是否满足标准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
$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语句,对吗?