Powershell检查文件是否存在于多个文件夹和输出中

时间:2015-09-18 10:38:26

标签: powershell

我有几百个文件夹,如下所示:

\\\uat.xxx.com\FileExport\New Collections\LCTS

\\\uat.xxx.com\FileExport\New Collections\GBSS

\\\uat.xxx.com\FileExport\New Collections\TRGS

我需要检查它们是否有特定文件,例如“Results 20150722New.dat

我需要知道不包含该文件的文件夹,如果可以将其输出到文件,可能会很好。 log.txt但更多我需要一个不包含它的文件夹列表。

我一直在尝试使用Test-Path,但实际上并没有得到任何结果

任何人都可以帮助我开始这个

1 个答案:

答案 0 :(得分:0)

作为一次性操作,您可以使用以下命令查找不包含此类文件名的所有目录的名称(字符串):

Get-ChildItem "\\uat.xxx.com\FileExport\New Collections\" | 
Where {$_.PSIsContainer } | 
ForEach { if (-not(Test-Path "$($_.FullName)\Results 20150722New.dat")) {Echo $_.FullName } }

(可选)指定-Recurse开关以递归方式搜索文件夹。

如果以后需要对结果进行操作,我宁愿将DirectoryInfo对象保存到clollection,而不是将它们转换为带有Echo cmdlet的字符串。

$dirs_not_containing_file = @()
$dirs_not_containing_file +=
Get-ChildItem "\\uat.xxx.com\FileExport\New Collections\" | 
Where {$_.PSIsContainer } | 
ForEach { if (-not(Test-Path "$($_.FullName)\Results 20150722New.dat")) {$_} }    

为了便于阅读,将第二个语句拆分为多行。