简体   繁体   中英

Search multiple folder to check if they are empty using powershell

I have a bunch of directories

C:\\RI1

C:\\RI2

C:\\RI3

... C:\\RI21

How can I check if they are all empty? I want to go further into the script only if one or more of them have files. If not, I want to exit. I tried this but it is searching for folder names and is giving me 21 as the answer

$directoryInfo = Get-ChildItem C:\RI* | Measure-Object
$directoryInfo.count

if ($directoryInfo.count -eq 0)
{

    Write-host "Empty"
}

else
{
    Write-host "Not Empty"
}

When you run Get-ChildItem C:\\RI* you get all the child items in C:\\ and filter the results with items which begin with "RI". You get the answer 21 since there are 21 folders in C:\\ that starts with "RI".

I suggest that you run through all the folders using a foreach loop.

$folders = @("RI1", "RI2", "RI3")

foreach ($folder in $folders)
{
    $path = "C:\$folder"
    $directoryInfo = Get-ChildItem $path
    if ($directoryInfo.count -eq 0)
    {
        Write-Host "Empty"
    }
    else
    {
        Write-Host "Not Empty"
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM