We help IT Professionals succeed at work.
Troubleshooting Question

How do I find a heavily nested folder having 263 files - Windows 2k12 R2

71 Views
Last Modified: 2020-09-30
Aloha -

I have a folder that I need to tweak permissions on. I know the drive and I know the number of files within that folder. I need a way to pinpoint the folder.

Mahalo,
           Bill and Bruce
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
With PowerShell, obviously ;)
Get-ChildItem -Path "D:\Folder" -Directory -Recurse | Where-Object {($_ | Get-ChildItem -File | Measure-Object).Count -eq 263} | Select-Object -ExpandProperty FullName

Open in new window

Bill CourtneyThe stupid vmware guy

Author

Commented:
First off - we do not know ps... When running the command I get this error -

Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
Hi Bill,

Here's an AutoHotkey script that does it:

; begin variables to change
RootFolder:="c:\StartHere\" ; root folder to start recursing from
MatchNum:=263 ; number of files to match
ResultsFile:=A_Temp . "\NumFiles.txt" ; full path to results file containing matches
; end variables to change

FileDelete,%ResultsFile% ; delete results file from previous runs
FileAppend,Time of run=%A_Now%`nNumber of files to match=%MatchNum%`n,%ResultsFile% ; put date-time stamp (YYYYMMDDhhmmss) and number of files to match into results file
Loop,Files,%RootFolder%*.*,DR ; recurse to all folders starting with root folder (D=directories, R=recurse)
{
  NumFiles:=ComObjCreate("Scripting.FileSystemObject").GetFolder(A_LoopFilePath).Files.Count ; number of files in folder
  If (NumFiles=MatchNum) ; if number of files is a match, put it in results file, otherwise continue loop
    FileAppend,%A_LoopFilePath%`n,%ResultsFile% ; put folder name in results file
}
Run,%ResultsFile% ; open results file with program that owns its file type
MsgBox,4096,Finished,Results file is now open
ExitApp ; all done - exit

Open in new window

If you're not familiar with AutoHotkey, my EE article will get you going on it:
AutoHotkey - Getting Started

My hope is that the well-chosen variable names and extensive comments will act as sufficient documentation, but if you have any questions, please post them and I'll try to help. I'm sure we can get it working for you, as I've tested it thoroughly here and it works perfectly. Note that I built some variables into the script for more general use in the future. Regards, Joe
Bill CourtneyThe stupid vmware guy

Author

Commented:
Looking at it Joe.. Please sit tight.
Bill CourtneyThe stupid vmware guy

Author

Commented:
When running the AHK script I immediately get this -

Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
This is incorrect:

ResultsFile:=A_Temp . "c:\NumFiles.txt"

It should be this:

ResultsFile:=A_Temp . "\NumFiles.txt"

or this (but only if you have permission to write into the root of the C drive...using A_Temp is better):

ResultsFile:="c:\NumFiles.txt"

or, if you want to hard-code a temp folder (and c:\temp must exist):

ResultsFile:="c:\temp\NumFiles.txt"
Bill CourtneyThe stupid vmware guy

Author

Commented:
still a no-go-jo(e)

Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
And your assignment statement for ResultsFile is still wrong. Please copy/paste this exactly as is:

ResultsFile:=A_Temp . "\NumFiles.txt"

Open in new window

Bill CourtneyThe stupid vmware guy

Author

Commented:
If I wasn't crazy before, I'm getting closer - 
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
Hi Bill,
I see that you're running this on WS2012R2. I've been testing it on W`10, where it works perfectly. Give me some time to try it on a WS2012R2 system and I'll get back to you. Regards, Joe
Bill CourtneyThe stupid vmware guy

Author

Commented:
Mahalo so much. I appreciate your help.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
A'ole pilikia. Still working on it...accessing a remote WS2012R2 system.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
Hi Bill,
Three things:

(1) Here's an improved version of the script with a Try-Catch pair in the event that the ComObjCreate call fails (and it has your variables in it).

#NoEnv
#SingleInstance force
SetBatchLines,-1

RootFolder:="f:\Shared\" ; root folder to start recursing from
MatchNum:=263 ; number of files to match
ResultsFile:=A_Temp . "\NumFiles.txt" ; full path to results file containing matches

FileDelete,%ResultsFile% ; delete results file from previous runs
FileAppend,Time of run=%A_Now%`nNumber of files to match=%MatchNum%`n,%ResultsFile% ; put date-time stamp (YYYYMMDDhhmmss) in results file
Loop,Files,%RootFolder%*.*,DR ; recurse to all folders starting with root folder (D=directories, R=recurse)
{
  Try NumFiles:=ComObjCreate("Scripting.FileSystemObject").GetFolder(A_LoopFilePath).Files.Count ; number of files in folder
  Catch ; in case ComObjCreate fails
  {
    FileAppend,Failed on %A_LoopFilePath%`n,%ResultsFile% ; put failed folder name in results file
    Continue
  }
  If (NumFiles=MatchNum) ; if number of files is a match, put it in results file, otherwise continue loop
    FileAppend,%A_LoopFilePath%`n,%ResultsFile% ; put folder name in results file
}
Run,%ResultsFile% ; open results file with program that owns its file type
MsgBox,4096,Finished,Results file is now open
ExitApp ; all done - exit

Open in new window


(2) Make sure you are running the latest version of AutoHotkey, which is 1.1.33.02. If not, download and install it (instructions in my AutoHotkey - Getting Started article).

(3) It failed on my WS2012R2 system in the same way that yours failed, but it was using an old version of AutoHotkey, which is why I wrote (2) above. After upgrading to the latest AutoHotkey, it worked fine. Btw, this one line script will display the AutoHotkey version:

MsgBox % A_AhkVersion

Open in new window

Aloha, Joe
Bill CourtneyThe stupid vmware guy

Author

Commented:
I'm current. Downloaded AHK after your reply. I'll try new script now.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
Hi Bill,
If it doesn't work, run this script:

#NoEnv
#SingleInstance force
SetBatchLines,-1
RootFolder:="f:\Shared\"
ResultsFile:=A_Temp . "\LoopFiles.txt"
FileDelete,%ResultsFile%
FileAppend,Time of run=%A_Now%`n,%ResultsFile%
Loop,Files,%RootFolder%*.*,DR
  FileAppend,%A_LoopFilePath%`n,%ResultsFile% ; put folder name in results file
Run,%ResultsFile%
MsgBox,4096,Finished,Results file is now open
ExitApp

Open in new window

That should create a text file with the name of each subfolder on a separate line. It works perfectly here on WS2012R2...does it work for you? Regards, Joe
Bill CourtneyThe stupid vmware guy

Author

Commented:
The 24 line script kinda but not completely worked. It seemed to work one folder level below the starting
point but not below that.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
Does the 12-line script show all the subfolders recursed to an unlimited depth? It should...and does here.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
That error happened because you didn't copy the complete command.
There's still a "Property FullName" following in the script I posted above that somehow didn't make its way into your PS shell. Use the "Select All" link below the code box to select the complete contents.
Here it is again with some line breaks:
Get-ChildItem -Path "F:\Shared" -Directory -Recurse |
	Where-Object {($_ | Get-ChildItem -File | Measure-Object).Count -eq 263} |
	Select-Object -ExpandProperty FullName

Open in new window

Here's the complete one-liner again:
Get-ChildItem -Path "F:\Shared" -Directory -Recurse | Where-Object {($_ | Get-ChildItem -File | Measure-Object).Count -eq 263} | Select-Object -ExpandProperty FullName

Open in new window

Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
Hi Bill,
A quick note to let you know that I just ran the 24-line script on a top level network share (X:\root) that has 339,650 files in 26,177 folders. I set the number of files to match at 15 (because I figured there would be many more of them than those with 263 files). The script worked perfectly, finding 351 folders with 15 files in them. This was on a W10 system that is connected to the X: share, but I tested the script on WS2012R2, where it worked fine. Regards, Joe
Bill CourtneyThe stupid vmware guy

Author

Commented:
I'll try the 12 liner now. I took time yesterday to watch the CNN Superstar Train Wreck Show
Bill CourtneyThe stupid vmware guy

Author

Commented:
I ran the 12 liner and it appeared to step thru all the levels. If you add code to point out folders with x number of files, I'd think I'd be good to go and indebted to you.
Developer
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Bill CourtneyThe stupid vmware guy

Author

Commented:
That scratched our collective itch. Mahalo for all your help.

Bill
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
A'ole pilikia, Bill, and mahalo back at you for the Testimonial...much appreciated!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.