You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本排除指定格式文件及对应PDF的问题排查

PowerShell文件过滤问题解决

需求说明

  • 排除扩展名为.ipt、.iam、.ipn、.idw的文件
  • 同时排除与上述文件对应的PDF文件(PDF文件名可与原文件同名,或在原文件名基础上添加其他字符)
  • 保留其余PDF文件及文件夹内所有其他类型文件

问题描述

已在PowerShell脚本中构造出目标PDF文件的路径,但无法成功过滤这些PDF;脚本必须固定使用来自SQL数据库的路径\\nml-fp\EngDesignLib\068\068179.001;尝试过多种过滤语句(如-and -not ($_.Name -contains $pdf.Name)等)均无效,原脚本片段如下:

If($testPathEngDesign){
 Get-ChildItem -Path $PathEngDesign -File -Recurse -ErrorAction SilentlyContinue -Force 
 |ForEach-Object{
 $file = $_
 $extension = $_.Extension
 $baseName = $file.Basename
 $filename = $file.Name
 if( $extension -in '.ipt', '.iam', '.ipn', '.idw') {
 $filePdf = "*$($baseName)*.pdf"
 $pdfFilePath = $file.FullName -replace ($file.Name, $filePdf)
 $pdf = Get-ChildItem -Path $pdfFilePath
 $fileNamePdf = $pdf.Name
 $filePdfPath = $pdf.FullName
 $arrayIdwPdf += $filenamePdf
 $excludeList = @("*.ipt", "*.iam", "*.ipn", ".idw",$pdf.Name)
  Get-ChildItem -Path $PathEngDesign -Exclude $excludeList -File -Recurse  | ForEach-Object 
  {if ($_.Name -notlike $pdf.Name ) {
  $csvData = [PSCustomObject]@{
  PartRn          = [String]$partRn
  DocumentNumber = [String]$PartNo
  Revision        = [String]$revision
  Classification  = [String]$classification
  FileName        = [String]$_.Name
  Path            = [String]$_.FullName
  }
  $csvDataArray += $csvData
  $csvData | Export-Csv -Path $csvFilePath -Force -Append 

解决方案

原脚本的问题在于循环内重复遍历目录,且排除逻辑仅针对单个PDF文件,无法覆盖所有需要排除的PDF。可以先一次性收集所有需要排除的规则,再批量过滤:

If($testPathEngDesign){
    # 1. 收集所有需要排除的基础文件名(来自.ipt/.iam/.ipn/.idw文件)
    $excludeBaseNames = Get-ChildItem -Path $PathEngDesign -File -Recurse -ErrorAction SilentlyContinue -Force `
        | Where-Object { $_.Extension -in '.ipt', '.iam', '.ipn', '.idw' } `
        | Select-Object -ExpandProperty BaseName

    # 2. 生成对应的PDF排除模式:*基础名*.pdf
    $excludePdfPatterns = $excludeBaseNames | ForEach-Object { "*$($_)*.pdf" }

    # 3. 组合所有排除项:原格式文件 + PDF排除模式
    $excludeList = @("*.ipt", "*.iam", "*.ipn", "*.idw") + $excludePdfPatterns

    # 4. 一次性获取所有符合要求的文件
    Get-ChildItem -Path $PathEngDesign -File -Recurse -ErrorAction SilentlyContinue -Force -Exclude $excludeList `
    | ForEach-Object {
        $csvData = [PSCustomObject]@{
            PartRn          = [String]$partRn
            DocumentNumber  = [String]$PartNo
            Revision        = [String]$revision
            Classification  = [String]$classification
            FileName        = [String]$_.Name
            Path            = [String]$_.FullName
        }
        $csvDataArray += $csvData
        $csvData | Export-Csv -Path $csvFilePath -Force -Append -NoTypeInformation
    }
}

关键改进点

  • 先一次性收集所有需要排除的基础文件名,避免循环内重复遍历目录,提升效率
  • 使用通配符模式*$baseName*.pdf匹配所有与原文件相关的PDF,不管文件名是否附加其他字符
  • 统一通过-Exclude参数批量过滤,逻辑更清晰,避免嵌套循环的判断错误
  • 添加-NoTypeInformation参数避免Export-Csv生成多余的类型信息行

内容的提问来源于stack exchange,提问作者ITBrute

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 12:25:00