我引用了这个问题来让我到目前为止: 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
注意:这将用于在最后阶段测试数百万条路径。这是最有效的方法吗?我读到以下版本的测试路径速度更快,但是,我不确定如何将其合并到上面的版本中,因为它输出 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
非常感谢您的帮助。 -守门员
最佳答案
这应该列出缺少的路径:
Get-Content $inputfile |
Where-Object {!(Test-Path $_)} |
Out-File -FilePath $outputmissingfiles -Append
关于powershell - Test-Path 是否有反向选项来输出无效路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69935855/