powershell - 识别空文件夹

标签 powershell batch-file

我想在我们的用户配置文件中识别特定的空文件夹。

我有一个文本文件,其中包含我希望脚本引用的所有用户名。该脚本将循环每个用户目录,并输出到文件或屏幕,并说明该目录是否为空。隐藏文件不必计数!

类似的东西

FOR /F %U IN (C:\UserList\UserList.TXT) DO *Find and List Empty Folder* \\Server\Share\%U\Target_Folder

欢迎使用 Powershell 解决方案!

最佳答案

This article Technet 上提供了以下 Powershell 代码片段来识别所有空文件夹:

$a = Get-ChildItem C:\Scripts -recurse | Where-Object {$_.PSIsContainer -eq $True}
$a | Where-Object {$_.GetFiles().Count -eq 0} | Select-Object FullName

将“C:\Scripts”替换为您要搜索的根文件夹。

更新:

以下脚本将帮助您了解大致情况。

$content = Get-Content C:\Temp\FolderList.txt
foreach ($line in $content)
{
    Write-Host $line -NoNewline
    $testObject = Test-Path -Path $line
    if ($testObject)
    {
        $folder = Get-Item -Path $line
        $filesCount = $folder.GetFiles().Count
        if ($filesCount.Equals(0))
        {
            Write-Host " - Empty folder"
        }
        else
        {
            Write-Host " - Contains files"
        }
    }
    else
    {
        Write-Host " - Invalid path"
    }

}

关于powershell - 识别空文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33802865/

相关文章:

batch-file - 使用 cmd 一次重命名多个文件 [Windows 10]

batch-file - 使用 PuTTY Plink 从批处理文件执行 "su"

excel - 使用 power shell 将 excel 工作簿数据从文件夹合并到 csv 文件

powershell - 在 Powershell 中打印对象属性

powershell - foreach 循环的写入进度问题

variables - 在批处理文件的文件路径目录中使用变量

windows - robocopy 导致成功退出(1)

powershell - Get-ChildItem -force 在“我的文档”文件夹和其他连接点上报告 "Access Denied"

Powershell 引号在屏幕上不可见

batch-file - 如何批量循环遍历逗号分隔的字符串?