首页 > 解决方案 > 获取员工文件夹中所有特定空子文件夹的列表

问题描述

你能帮我做一个目录操作吗?

我有一个员工目录,在该目录中,大约有 200 多个员工子目录,由他们的员工代码命名。在每个员工的子目录中,大约有 20 个子文件夹,涉及各种文件。例如,名为“教育文档”的子文件夹。这个“教育文档”子文件夹存在于这 200 多个员工的每个文件夹中。

我想输出一个文本或 csv 文件,其中列出了 200 多名员工中的所有此类“教育文档”子文件夹,这些员工是空的,或者换句话说,扫描的 PDF 文件尚未被复制。通过这样做,我将能够使用该输出文件作为我自己的任务列表,通过为丢失的员工数据放置扫描的 PDF 文档来填充所有这些空文件夹。

我曾尝试将 DOS 命令与 /S 开关一起使用,但这并不能完全满足我的需求,因此我正在寻找一些可以完成此任务的 Powershell 脚本。

到目前为止我的代码:

$Search = gci -Filter "Educational Documents" -Recurse -Path "D:\Employees" -Directory 
Foreach ($path in $Search.fullname) 
{ 
  Write-Output $path | Out-File d:\Filelist.txt -append 
  $file = gci -path $path | select name 
  $file.name | Out-File d:\filelist.txt -append 
  Write-Output "------- Next Folder --------------" | Out-File d:\Filelist.txt -append 
}

标签: powershellpowershell-5.0

解决方案


如果我理解正确,您需要一个名为“教育文档”的所有空文件夹的文件列表。

为此,您可以使用返回的 DirectoryInfo 对象的GetFileSystemInfos()方法,Get-ChildItem如下所示:

$Search = Get-ChildItem -Path "D:\Employees" -Filter "Educational Documents" -Recurse -Directory |
          Where-Object { $_.GetFileSystemInfos().Count -eq 0 } | Select-Object -ExpandProperty FullName

# add '-PassThru' to also output this list on screen 
$Search | Set-Content -Path 'D:\Empty_EducationalDocuments_Folders.txt'

希望有帮助


根据您的评论,您想列出空文件夹和没有Graduation名称中包含该单词的文件的文件夹,您可以将上面的内容编辑为

$Search = Get-ChildItem -Path "D:\Employees" -Filter "Educational Documents" -Recurse -Directory |
          Where-Object { $_.GetFileSystemInfos().Count -eq 0 -or 
                         $_.GetFiles("*Graduation*", "TopDirectoryOnly").Count -eq 0 } | 
          Select-Object -ExpandProperty FullName

# add '-PassThru' to also output this list on screen 
$Search | Set-Content -Path 'D:\EducationalDocuments_Folders_without_Graduation_File.txt'

推荐阅读