Find folders that don't contain a file

Welcome Forums General PowerShell Q&A Find folders that don't contain a file

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
4 years ago.

  • Author
    Posts
  • #19586

    Participant
    Points: 0
    Rank: Member

    Hi,

    I am trying to get a list of all folders in my music collection that do not have a file called 'folder.jpg'. I have come up with this, but it still shows folders that do contain 'folder.jpg.

    Clear-Host
    $ThePath = "$Env:USERPROFILE\CloudStation" #Change this
    Get-ChildItem -Path $ThePath -Recurse |
        Where-Object { ($_.PSIsContainer -eq $True) -and ($_.Name -notlike "folder.jpg") } |
        Sort-Object FullName | Select-Object FullName | Format-Table * -AutoSize

    I know the issue is with the statement [$_.Name -notlike "folder.jpg"] as I think it is looking for a Folder named called folder.jpg, but I'm nt sure on what syntax I should be using.

    Thanks
    Tony

  • #19590

    Participant
    Points: 1
    Rank: Member

    Hi.

    You are testing for folders not named folder.jpg.

    You can try this snippet to return only the folders that does not contain a file named folder.jpg

    Clear-Host
    $ThePath = "$Env:USERPROFILE\CloudStation" #Change this
    $dirs = Get-ChildItem -Path $ThePath -Recurse -Directory
    
    foreach ($dir In $dirs.Fullname) {
        if(-not(Test-Path "$dir/folder.jpg")) {
            Write-Output $dir
        }
    }
    

    Stein

  • #19594

    Participant
    Points: 0
    Rank: Member

    Thanks Stein

The topic ‘Find folders that don't contain a file’ is closed to new replies.