【问题标题】:Is there a inverse option for Test-Path to output invalid paths?Test-Path 是否有反向选项来输出无效路径?
【发布时间】:2021-11-12 23:03:31
【问题描述】:

我已经参考了这个问题来让我走到这一步: redirecting test-path output to text file

我基本上使用的是提供的相同示例,我只需要创建一个无效文件路径的输出。

我正在尝试通过 UNC 路径检查文件是否存在。所以我针对特定文件(C:\Users\Goalie\Desktop\folder\1.txt、\2.txt、\3.txt)我已经知道每个文件路径,我只是想看看文件是否那些 UNC 路径实际上是否存在。

\paths.txt 包含多个 UNC 文件路径。 例如:

C:\Users\Goalie\Desktop\folder\1.txt
C:\Users\Goalie\Desktop\folder\2.txt
C:\Users\Goalie\Desktop\folder\3.txt

以下代码目前仅适用于有效的文件路径。我也需要无效的文件路径。

$inputfile = "C:\Users\Goalie\Desktop\folder\paths.txt"
$outputexistingfiles = "C:\Users\Goalie\Desktop\folder\existingfiles.txt"
$outputmissingfiles = "C:\Users\Goalie\Desktop\folder\missingfiles.txt"


Get-Content $inputfile |
    Where-Object {Test-Path $_} |
        Out-File -FilePath $outputexistingfiles -Append

注意:这将用于在最后阶段测试数百万条路径。这是最有效的方法吗?我读过下面版本的 test-path 更快,但是,我不确定如何将它合并到上面,因为它输出 True 或 False,而不是实际的 UNC 路径。

$inputfile = "C:\Users\Goalie\Desktop\folder\paths.txt"
$filepaths = Get-Content $inputfile

$filepaths.ForEach({Test-path $_ -PathType leaf}) | Out-File -FilePath $outputmissingpaths -Append

非常感谢您的帮助。 -守门员

【问题讨论】:

  • 我假设,考虑到许多路径,您想一次性找到现有文件和丢失文件?是否有(m)任何路径是彼此的后代?如果您发现C:\X\ 不存在,那么您知道C:\X\Y\C:\X\Y\Z\ 也不存在。
  • 是的,理想情况下,我希望一次性识别两者。我可能应该详细说明我的问题。我正在尝试通过 UNC 路径检查文件是否存在。所以我针对特定文件(C:\Users\Goalie\Desktop\folder\1.txt、2.txt、3.txt)我已经知道每个文件路径,我只是想看看那些文件UNC 路径实际上是否存在。希望澄清。
  • 对,从问题中可以清楚地看出这一点。无论是 UNC 路径、映射驱动器还是本地驱动器,都不会真正改变任何东西(只要当前用户有权访问网络共享)。所有这些文件是要测试是否存在于相同的目录中(即C:\Users\Goalie\Desktop\folder\ 和任何 UNC 目录),还是到处都是?
  • 好的,抱歉,第一次使用 :)。它们都在一个主目录下,但是下面有几个子目录。

标签: powershell


【解决方案1】:

这应该列出缺少的路径:

Get-Content $inputfile |
    Where-Object {!(Test-Path $_)} |
        Out-File -FilePath $outputmissingfiles -Append

【讨论】:

    【解决方案2】:

    您帖子标题中问题的答案是:Test-Path 本身没有反转其逻辑的开关,但您可以将调用的输出传递给-not (!) operator-not (Test-Path $_)

    这将用于在其最后阶段测试数百万条路径。这是最有效的方法吗?

    您的用例要求:

    $inputfile = 'C:\Users\Goalie\Desktop\folder\paths.txt'
    
    # Initialize the output file writers.
    # Note: Be sure to use *full* paths, because .NET's working directory
    #       usually differs from PowerShell's
    $outExisting = [System.IO.StreamWriter] 'C:\Users\Goalie\Desktop\folder\existingfiles.txt'
    $outMissing =  [System.IO.StreamWriter] 'C:\Users\Goalie\Desktop\folder\missingfiles.txt'
    
    switch -File $inputfile {
      default {
        if ([System.IO.File]::Exists($_)) {
          $outExisting.WriteLine($_)
        } else {
          $outMissing.WriteLine($_)
        }
      }
    }
    
    $outExisting.Close(); $outMissing.Close()
    

    【讨论】:

    • 这很棒,我在我的环境中测试过,它工作正常。感谢您的意见和帮助。附带问题,是否有任何指标可以汇总完成一项工作需要多长时间?
    • 捕捉开始和结束的时间然后减去。是最简单的方法。当然,您可以使用 Measure-Object,但似乎有很多不必要的开销。
    • 我很高兴听到这个消息,@Goalie;我的荣幸。 PowerShell 用于计时命令执行的 cmdlet 是 Measure-Command(不要与 Measure-Object 混淆)。但是,单个时间可能具有欺骗性,因此最好平均 多个时间 - 请参阅this answer
    • 谢谢,我会看看Measure-Command,看看我的选择是什么。再次感谢您的快速帮助!
    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 2018-06-09
    • 2014-09-02
    • 2019-07-17
    相关资源
    最近更新 更多