Author Posts

July 20, 2018 at 12:34 pm

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
       }
}

July 20, 2018 at 1:26 pm

... 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 😉 )

July 20, 2018 at 5:50 pm

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}

July 20, 2018 at 7:23 pm

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}}

July 24, 2018 at 12:04 pm

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.