【问题标题】:How to find files whose paths do not exist in the text file using powershell如何使用powershell查找文本文件中不存在路径的文件
【发布时间】:2013-03-31 11:22:29
【问题描述】:

我需要检查并返回文件系统中存在但未在给定文本文件中列出的文件。例如,我的文本文件 (sample.txt) 将包含如下路径:

\\SharedDrive\Data\DevS\Common\app_name\subfolder1\Archive\Archive1.vbproj
\\SharedDrive\Data\DevS\NotCommon\app_name\subfolder1\user\WebApp.vbproj
\\SharedDrive\Data\DevS\UnCommon\app_name\subfolder1\Manager\Managerial.vbproj

碰巧有 VB 项目文件存在于驱动器上但不在列表中,我想连同它们的完整路径一起返回。例如:

\\SharedDrive\Data\DevS\Common\app_name\subfolder2\Windows\SharedArchive.vbproj
\\SharedDrive\Data\DevS\NotCommon\app_name2\subfolder1\user2\WebApp2.vbproj

我试过了:

$log = "e:\pshell\notExists.log"

Get-Content "e:\pshell\Sample.txt" | Where-Object {
#Keep only paths that does not exists
!(Test-Path $_)
} | Set-Content $log

但这恰恰相反。

【问题讨论】:

  • 嗯...不,您的代码完全按照您说的做。不过,为了安全起见,您可能希望将 Test-Path $_ 更改为 Test-Path -LiteralPath $_
  • @AnsgarWiechers:此代码实际上返回了属于列表但驱动器上不存在的文件。但我想反过来 - 列表中不存在但驱动器上存在的文件。

标签: powershell directory text-files file-exists


【解决方案1】:

试试这个:

$baseDir = "\\SharedDrive\Data\DevS"
$log = "..."

$paths = Get-Content sample.txt

Get-ChildItem $baseDir -Recurse -Filter *.vbproj | ? {
  -not $_.PSIsContainer -and $paths -notcontains $_.FullName
} | % { $_.FullName } | Set-Content $log

【讨论】:

  • 代码返回基目录下的所有文件夹。我只想返回列表中没有的 vb 项目文件名 (sample.txt) 并且在驱动器上。
猜你喜欢
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 2022-01-23
  • 2022-06-15
  • 2020-02-05
  • 2020-10-30
  • 1970-01-01
  • 2021-02-08
相关资源
最近更新 更多