获取员工文件夹内所有特定空子文件夹的列表

Get a list of all specific empty subfolders inside employee's folders

能帮我做个目录操作吗?

我有一个员工目录,在该目录中,大约有 200 多个员工子目录以员工代码命名。在每个员工的子目录中,大约有 20 个子文件夹涉及各种文档。例如,名为 'Educational Documents' 的子文件夹。这个 'Educational Documents' 子文件夹存在于这 200 多名员工的文件夹中。

我想输出一个文本或 csv 文件,列出 200 多名员工中的所有此类 'Educational Documents' 子文件夹,这些子文件夹是空的,换句话说,扫描的 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 
}

您可以试试这个代码:

$Search = Get-ChildItem -Recurse -Path "D:\Employees" -Directory 
foreach ($path in $Search.fullname) 
{ 
  $directoryInfo = Get-ChildItem -Path $path | Measure-Object
  if($directoryInfo.count -eq 0)
  { 
    $path | Out-File "D:\Filelist.txt" -append 
    Write-Output "------- Next Folder --------------" | Out-File "D:\Filelist.txt" -append 
  }
}

我使用了这个问题的一些代码:Test if folder is empty

如果我没理解错的话,您需要一个名为 'Educational Documents'.

的所有空文件夹的文件列表

为此,您可以像这样使用 Get-ChildItem 返回的 DirectoryInfo 对象的 GetFileSystemInfos() 方法:

$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'