Find folders that don't contain a file

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Tony Wainwright Tony Wainwright 2 years, 1 month ago.

  • Author
    Posts
  • #19586
    Profile photo of Tony Wainwright
    Tony Wainwright
    Participant

    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 [i][$_.Name -notlike "folder.jpg"] [/i] as I think it is looking for a Folder named called [i]folder.jpg[/i], but I'm nt sure on what syntax I should be using.

    Thanks
    Tony

  • #19590
    Profile photo of Stein Petersen
    Stein Petersen
    Participant

    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
    Profile photo of Tony Wainwright
    Tony Wainwright
    Participant

    Thanks Stein

You must be logged in to reply to this topic.