这个问题还有更多内容。
我们在 EngDesignLibrary 中有 Inventor 文件和所有其他类型的文件。 还有
.idw
也以 .pdf
格式存在。最终结果需要具有以下类型的文件,同时保持目录结构:
FileName.idw -like "*FileName*.pdf"
我想要这些时。.idw
filname 正是与.pdf
相同。基本上,应该排除 FileName.idw -like FileName.pdf
。对于上述结果,我将文件分为两部分。 第 1 部分是
.idw
文件名与 .pdf'
不完全相同且 .idw
与 .pdf
完全相同
第 2 部分是我排除不需要的文件以及第 1 部分中的文件的地方
问题是,在第 2 部分中,我还获得了 .idw 的 .pdf 等效项
脚本
$sourceFolder = "F:\Departments\EngDesignLib\"
$fileExtensions = @('.ipt', '.iam', '.ipn', '.idw')
$destination = "\\SharedFolder\Eng_3\All_Pdfs"
$csvFilePath = "C:\Users\Desktop\CSVOutput\MoveTo03\MoveTo03_1.csv"
Get-ChildItem -Path $sourceFolder -File -Recurse | ForEach-Object{
$file = $_
$extension = $_.Extension
$baseName = $file.Basename
$filename = $file. Name
#Part 1
If ($_.Extension -in '.idw'){
#change the extension of the file in the path itself
$pdfFileName = "*$basename*.pdf"
$pdfFileNameEaxctMatch = [System.IO.Path]::ChangeExtension($file.BaseName, "pdf")
$pdfFilePath = $file.fullname -Replace ($file.Name, $pdfFileName)
$pdfFilePathExactMatch = $file.fullname -Replace ($file.Name, $pdfFileNameEaxctMatch)
$csvData = [PSCustomObject]@{
FileName = $file.Name
FilePath = $file.FullName
IsInventorFile = 1
FileNamePdfOFIDW = If (Test-Path $pdfFilePath){ $pdfFileName} Else{'PDF Not Found'}
PdfFilePathOfPDFIDW = If (Test-Path $pdfFilePath){ $pdfFilePath} Else{'PDF Not Found'}
}
$csvData| Export-Csv -Path $csvFilePath -NoTypeInformation -Force -Append
}
#Part 2
If ( !( $_.Extension -in $fileExtensions ) -and ($fileName -notLike ($pdfFileName -or $pdfFileNameEaxctMatch))){
$csvData = [PSCustomObject]@{
FileName = $file.Name
FilePath = $file.FullName
IsInventorFile = 0
FileNamePdf = 'Not an Iventor File'
PdfFilePath = 'Not an Iventor File'
}
$csvData| Export-Csv -Path $csvFilePath -NoTypeInformation -Force -Append
}
}
我已经尝试了大多数条件运算符 -like、-notLike、-contains、-notcontains,但它不起作用。 另外,不完全确定这行 ` $pdfFileName = "$basename.pdf"' 是如何工作的。因为输出显示带有astrix的文件名,但当没有带有astrix的文件时,Test-path为true。 它还返回 .idw 文件名与 .pdf 完全相同的文件 这搞乱了我在第 1 部分中获得的文件
此特定情况的输出示例:
F:\部门\工程\EngDesignLib 89 89705.000 89705-01.idw F:\部门\工程\EngDesignLib 89 89705.000*089705-01*.pdf
如何获取 .idw 文件名与 .pdf 名称不完全相同的文件?并使第2部分中的过滤条件起作用。
考虑您的 2 个标准:
.idw
且带有扩展名 .pdf
的确切文件名不存在。试试这个:
Get-ChildItem -Path $sourceFolder -File -Recurse | Where-Object {
$_.Extension -notin '.ipt', '.iam', '.ipn' -and
-not (
$_.Extension -eq '.idw' -and
(Test-Path -LiteralPath ($_.FullName -replace '\.idw$', '.pdf'))
)
}
说明:
$_.Extension -notin '.ipt', '.iam', '.ipn'
测试扩展是否不是列出的扩展之一。
-not (
$_.Extension -eq '.idw' -and
(Test-Path -LiteralPath ($_.FullName -replace '\.idw$', '.pdf'))
)
我将其分解为 4 件事:
$_.Extension -eq '.idw'
测试扩展名是否等于
.idw
。
$_.FullName -replace '\.idw$', '.pdf'
获取文件的完整路径,将文件扩展名
.idw
替换为.pdf
。
Test-Path -LiteralPath ($_.FullName -replace '\.idw$', '.pdf')
测试替换的文件路径是否存在(例如:是否存在以
.pdf
结尾的相同文件)。
-not ( ... )
反转括号内测试的
$true
/$false
结果。因此,如果文件确实具有扩展名.idw
并且存在以.pdf
结尾的相同文件,则通常会返回$true
,因此-not
将其翻转为$false
,从而导致整个Where-Object
过滤为 $false
,因此它不会返回此文件。