【发布时间】: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