0

Could you please help me with a directory operation?

I have an employee directory and in that directory, there are about 200+ employees subdirectories named by their employee code. And within each employee's subdirectory, there are about 20 subfolders referring to various documents. For example, subfolder named 'Educational Documents'. This 'Educational Documents' subfolder exists in each of these 200+ employee's folders.

I want to output a text or csv file listing all such 'Educational Documents' subfolders out of those 200+ employees which are empty or in other words where scanned PDF files have not been copied as yet. By doing so, I will be able to use that output file as a task list for myself to populate all those empty folders by putting scanned PDF documents for the missing employee data.

I have tried to use DOS commands with /S switch but that does not precisely cater to my needs and therefore I am looking at some Powershell script which could get this done.

My code so far:

$Search = gci -Filter "Educational Documents" -Recurse -Path "D:\Employees" -Directory 
Foreach ($path in $Search.fullname) 
{ 
  Write-Output $path | Out-File d:\Filelist.txt -append 
  $file = gci -path $path | select name 
  $file.name | Out-File d:\filelist.txt -append 
  Write-Output "------- Next Folder --------------" | Out-File d:\Filelist.txt -append 
}
Alex_P
  • 2,580
  • 3
  • 22
  • 37
SmIqbal
  • 99
  • 1
  • 13
  • Please read [how to ask](https://stackoverflow.com/help/how-to-ask) and edit the question accordingly. SO is not a code writing service, but a QA site. Questions are expected to show research and include code or algorithm outlined so far. That being said, start by getting a list of dirs with `Get-ChildItem`. `Test-Path` will tell if an item exists on given a path. – vonPryz May 22 '20 at 06:51
  • Someone came up with a similar question and was rescued with the above code. I have altered it to suit my needs but it lacks only one aspect that it is showing even the 'Educational Documents' subfolders which have content in them whereas I need only the empty ones. – SmIqbal May 22 '20 at 06:59
  • That's exactly what I meant about research. This is a piece of good code that can be altered to suit for the needs. Please add that into the question body, so anyone reading the question will find it easily enough. – vonPryz May 22 '20 at 07:05

2 Answers2

2

If I understand correctly, you want a file listing of all empty folders called 'Educational Documents'.

To do that, you could make use of the GetFileSystemInfos() method of the DirectoryInfo objects returned by Get-ChildItem like this:

$Search = Get-ChildItem -Path "D:\Employees" -Filter "Educational Documents" -Recurse -Directory |
          Where-Object { $_.GetFileSystemInfos().Count -eq 0 } | Select-Object -ExpandProperty FullName

# add '-PassThru' to also output this list on screen 
$Search | Set-Content -Path 'D:\Empty_EducationalDocuments_Folders.txt'

Hope that helps


As per your comment, you would like to list both empty folders and folders that do not have a file with the word Graduation in their name, you can edit the above to become
$Search = Get-ChildItem -Path "D:\Employees" -Filter "Educational Documents" -Recurse -Directory |
          Where-Object { $_.GetFileSystemInfos().Count -eq 0 -or 
                         $_.GetFiles("*Graduation*", "TopDirectoryOnly").Count -eq 0 } | 
          Select-Object -ExpandProperty FullName

# add '-PassThru' to also output this list on screen 
$Search | Set-Content -Path 'D:\EducationalDocuments_Folders_without_Graduation_File.txt'
Theo
  • 57,719
  • 8
  • 24
  • 41
  • Theo, thanks a million. This is exactly what I was looking for. – SmIqbal May 23 '20 at 09:08
  • Hi Theo, while these commands do a wonderful job, I am just curious to know if we could limit it to a specific file to be looked into each Education Documents folder. Suppose if you want it to find any missing files which could not be found which have word 'Graduation' being a part of filename. Do you think this additional filter can be piped in? – SmIqbal May 24 '20 at 10:40
  • @SmIqbal I have added extra code to do that for you. – Theo May 24 '20 at 13:19
  • Theo, you are truly a genius. Thank you s much. – SmIqbal May 25 '20 at 17:27
  • @Smiqbal Thanks for the nice feedback! – Theo May 25 '20 at 17:47
1

You can try this code:

$Search = Get-ChildItem -Recurse -Path "D:\Employees" -Directory 
foreach ($path in $Search.fullname) 
{ 
  $directoryInfo = Get-ChildItem -Path $path | Measure-Object
  if($directoryInfo.count -eq 0)
  { 
    $path | Out-File "D:\Filelist.txt" -append 
    Write-Output "------- Next Folder --------------" | Out-File "D:\Filelist.txt" -append 
  }
}

I used some code from this question: Test if folder is empty

Alex_P
  • 2,580
  • 3
  • 22
  • 37
  • Alex Thanks for your solution, however, I want to specifically search for empty 'Educational Documents' subfolders only within each employee's folder. Again, all those 200+ employees' subfolders reside in the root directory of 'D:\Employees" and each employee's folder has this 'Educational Documents" subfolder that needs to be checked for its emptiness. Doing this check manually is quite painstaking. I hope you get my point. – SmIqbal May 22 '20 at 07:41
  • The folders are called 'Educational Documents'? And only the empty folders with this name you want? – Alex_P May 22 '20 at 08:45
  • Thanks a lot Alex for all your help. Yes, your understanding is correct. Theo's response handles this requirement. – SmIqbal May 23 '20 at 09:10