powershelldirectoryfile-managementrename-item-cmdlet

adding text to folders with no files/and with no subfolders with files


I have code I put together from different sources found online. That code, I believe, is supposed to search and find all folders who have no files/and whose subfolders have no files, then add the text "NA -" to the beginning of these folders. When I checked it looked like some folders that were obviously empty were not marked. Is it something I'm missing?? Any help would be greatly appreciated, thank you.

$CMfolder=get-childitem "Z:\folder\subfolder\subfolder2" -Recurse |
Where-Object {$_.PsIsContainer -eq $true}
$CMfolder | Where-Object {$_.GetFiles().Count -eq 0 -and
$_.GetDirectories().count -eq 0} | 
where-object {$_.Name -Notlike "NA -*"} |

Rename-Item -NewName {"NA -" + $_.Name}

Solution

  • try this :

        Get-ChildItem "c:\temp" -recurse -directory -Force | where Name -Notlike "NA -*" |
     select *, @{N="HasChild";E={((Get-ChildItem $_.FullName -recurse -force -file).Count -ne 0)}} | where {! $_.HasChild}