【问题标题】:Comparing File Names with different extensions using Powershell使用 Powershell 比较具有不同扩展名的文件名
【发布时间】:2017-03-01 23:05:41
【问题描述】:

我想比较同一目录中存在相同名称但扩展名不同的文件。

示例:一个位置有 6 个文件。

1234.pdf

1234.xml

abcd.pdf

abcd.xml

5678.pdf

efgh.xml

我想将所有 .pdf/.xml 文件与具有相同名称的 .xml/.pdf 扩展名的文件进行比较,并找出是否缺少任何 .pdf 或 .xml 文件,就像上面的这里一样例如 5678.xml 文件丢失和 efgh.pdf 文件丢失。

我尝试将所有 .pdf 文件复制到一个文本文件中,并将所有 .xml 文件复制到另一个文本文件中,并尝试比较其中的字符串,但它不起作用。

谁能告诉我如何比较不同扩展名的文件名?

【问题讨论】:

    标签: powershell powershell-3.0


    【解决方案1】:
    Push-Location "\\Cifintgfsfs001\gfs\MXPDFXML\Data\Test"
    Get-childitem *.xml | ForEach { 
      if (!(test-path "$($_.BaseName).pdf")){ 
        "$($_.BaseName).pdf missing"
      }
    } 
    Get-childitem *.pdf | ForEach { 
      if (!(test-path "$($_.BaseName).xml")){ 
        "$($_.BaseName).xml missing"
      }
    } 
    Pop-Location
    

    【讨论】:

    • 我尝试了您提供的代码,但它只是列出了所有后缀为“缺失”的文件名。代码:Get-childitem -Path "\\Cifintgfsfs001\gfs\MXPDFXML\Data\Test"*.xml | ForEach { if (!(test-path "$($_.BaseName).pdf")){ "$($_.BaseName).pdf 缺失" } } Get-childitem -Path "\\Cifintgfsfs001\gfs\MXPDFXML \数据\测试" *.pdf | ForEach { if (!(test-path "$($_.BaseName).xml")){ "$($_.BaseName).xml 缺失" } } 输出:abcd.pdf 缺失 1234.pdf 缺失 abcd.xml缺少 1234.xml 缺少 6789.xml 缺少
    • 在上面的例子中,只有“6789.pdf”丢失应该是输出,因为其余所有文件都有一个对应的 .pdf 或 .xml 文件。你能指导我吗?
    • 代码假定当前文件夹中的文件。我会看看你的代码。
    • @ShaikShahabuddin 临时更改位置更容易-否则test-path 必须使用Join-Path 获得完整路径。见上文
    • 非常感谢您的帮助。您提供的代码正在运行。
    猜你喜欢
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 2017-10-25
    • 1970-01-01
    相关资源
    最近更新 更多