Powershell 中只知道部分文件名时如何过滤文件?

问题描述 投票:0回答:1

这个问题还有更多内容。

我们在 EngDesignLibrary 中有 Inventor 文件和所有其他类型的文件。 还有

.idw
也以
.pdf
格式存在。
.pdf 文件的名称可以与 .idw 文件名完全相同,也可以不同。 这种类型的文件肯定会具有 .idw 的文件名,但可以在其左侧或右侧添加字符。字符实际上可以是任何类型,字母数字、括号、连字符、逗号、特殊字符,没有任何模式。

最终结果需要具有以下类型的文件,同时保持目录结构:

  1. FileName.idw 与 FileName.pdf 并不完全相同。基本上,当
    FileName.idw -like "*FileName*.pdf" 
    我想要这些时。
  2. 所有其他类型的文件,但不包括以下内容: (2.1) 扩展名是 ('.ipt', '.iam', '.ipn', '.idw') (2.2) 其中
    .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部分中的过滤条件起作用。

powershell get-childitem
1个回答
0
投票

考虑您的 2 个标准:

  1. 扩展名是
    .idw
    带有扩展名
    .pdf
    的确切文件名存在。
  2. 扩展不属于以下内容:
    • .ipt
    • .iam
    • .ipn

试试这个:

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'))
    )
}
© www.soinside.com 2019 - 2024. All rights reserved.