Does anyone know how to search through thousands of subdirectories for all directories that contain only 1 file and not more than 1 file?
Any suggestion on what tool to use or an easy code snippet?
|
Does anyone know how to search through thousands of subdirectories for all directories that contain only 1 file and not more than 1 file? Any suggestion on what tool to use or an easy code snippet?
| ||||
|
feedback
|
|
In PowerShell, this is one way you could do it:
The | |||||||
feedback
|
|
Here is a Perl solution (tested on Windows):
Output: C:\Temp> f . C:\Temp\1 C:\Temp\chrome_9999 C:\Temp\CR_3E.tmp | |||
|
feedback
|
|
If this was on Linux I would be tempted to use a command like this.
The find command will print out the directory name of all the files. Which we then run through sort, and then use the -c option of uniq to give us the number of files per directory. Once you have the count per directory it should be easy enough to just grep out the directories with a value of 1. | |||||||||
feedback
|
|
I think you could craft something in a few lines using
Something like this.
EDIT: I really like Zoredache's find method better, but you did tag this as perl. | ||||
feedback
|
|
The solution with:
Needlessly reads each directory to count items within it, then reads it again when actually descending it (as part of the A simpler solution is just to descend, charging the presence of each file to the directory that contains it:
| |||
|
feedback
|