powershell - 获取员工文件夹中所有特定空子文件夹的列表
问题描述
你能帮我做一个目录操作吗?
我有一个员工目录,在该目录中,大约有 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
}
解决方案
如果我理解正确,您需要一个名为“教育文档”的所有空文件夹的文件列表。
为此,您可以使用返回的 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'
推荐阅读
- php - is_uploaded_file 函数在 linux 中有效,但在 Windows 中无效
- arrays - reactjs中如何将数组数据转换为对象?
- javascript - Content-Security-Policy - 无法加载外部脚本
- docusignapi - 如何将 Zapier 中的特定变量分配到 Docusign 的文本字段中以自动填充合同?
- git - 无法运行 git pull 命令
- android - 什么是清洁架构(领域)中的业务逻辑
- bash - 如何从文本文件中读取一行,将其保存到变量中,然后在 bash 命令中使用它
- ssl - 如何使用 httpclient 从 windows 窗体中使用损坏的 ssl 连接到 url
- xml - 在文档具有可选子元素的情况下,使用 Rust 和 Serde 反序列化 XML 的困难
- cuda - CUDA-为什么它不能打印 cuda 代码中的信息?