PS script to get the folder with no files

Welcome Forums General PowerShell Q&A PS script to get the folder with no files

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

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #104815

    Participant
    Points: 0
    Rank: Member

    hi,
    I am trying to create a PS script which will loop through the folder and subfolder inside a dir and then get the folder name where there is no files or files which are older than 90 days. Below is the folder structure. Output should be VendorA or VendorB or VendorC. I tried using gci but did not work.

    FTP
    	VendorA
    		folderA
    			folder D
    		folderB
    			Folder E
    			Folder F
    			Folder G
    		folderC
    	VendorB
    		folderA
    			folder D
    		folderB
    			Folder E
    			Folder F
    			Folder G
    		folderC
    	VendorC
    		folderA
    			folder D
    		folderB
    			Folder E
    			Folder F
    			Folder G
    		folderC
    

    Here is the code that i wrote however want to have the output in csv file in tabular form – Name, Creation time.

    $gci = gci -Path F:\ftp\NEWFTP -Directory
     
    foreach ($g in $gci)
    {
       $child =  Get-ChildItem -Path $g.FullName -Recurse -File
           if ($child -eq $null)
           {
             $g.Name, ($g.CreationTime).ToShortDateString() | Out-File C:\Users\test\Documents\folders.txt -Append
           }
    }
    
  • #104824

    Participant
    Points: 134
    Helping Hand
    Rank: Participant

    ... I tried using gci but did not work. ...

    What exactly have you tried so far and why / how hasn't it worked as expected / needed? (That means: Please show your code 😉 )

  • #104851

    Participant
    Points: 158
    Helping Hand
    Rank: Participant

    Try something like this. Get all of the Folders, then you can use a calculated expression to do another Get-ChildItem with the folder path to get the files to get a count. Once you have all of the counts, then you can just use a simple where to find the empty folders.

    $results = Get-ChildItem -Path C:\Scripts -Recurse -Directory |
               Select-Object -Property Name,
                                       FullName,
                                       @{Name="FileCount";Expression={@(Get-ChildItem -Path $_.FullName -File).Count}}
    
    $results | Where{$_.FileCount -eq 0}
    

    If it's a ton of files, there could be some better options like System.IO.Directory. The * is the extension, so you could just get a count of *.txt if wanted:

    $results = Get-ChildItem -Path C:\Scripts -Recurse -Directory |
               Select-Object -Property Name,
                                       FullName,
                                       @{Name="FileCount";Expression={[System.IO.Directory]::GetFiles($_.FullName, "*").Count}}
    
    $results | Where{$_.FileCount -eq 0}
    
  • #104857

    Participant
    Points: 206
    Helping Hand
    Rank: Participant

    Rob Simmers, very true, except, despite his post title, the OP has stated two criteria.


    dir and then get the folder name where there is no files or files which are older than 90 days.

    Yours easily takes care of the empty folder thing, but not the second part.

    So, maybe this...

    cls
    Get-ChildItem -Path D:\Temp -Directory -Recurse | 
    Where-Object -FilterScript {
    (($_.GetFiles().Count -eq 0) -or ($_.GetFiles().CreationTime -le (Get-Date).AddDays(-90)))
    } | 
    Select-Object -Property FullName,CreationTime,@{Name ='TotalFilesInFolder';Expression={$_.GetFiles().Count}}
    
  • #105062

    Participant
    Points: 0
    Rank: Member

    Thanks Rob and Postanote, this helped. Initially we were considering the folder with files older than 90 days, but now we only need folder that has no files in it. I was able to get this using the above code.

The topic ‘PS script to get the folder with no files’ is closed to new replies.