Search for all windows folders containing a single file (as opposed to 2+ files)?

2

Kind of an odd parameter, but it would be useful if I could run a search that returned only folders that contain a single file. Folders with 2 or more files don't get returned. Can this be done in windows? I could also live with a search that returned the files themselves.

CreeDorofl

Posted 2011-12-28T15:55:06.723

Reputation: 2 053

Answers

3

ONE way this can be done is by using the built-in windows scripting:

Two plain text files need to be created in a single folder:

search.vbs with contents (basically picked up from code samples, modified slightly by me):

strDir = "i:\"
ignoreFolderCount = false

'*********************

set FSO = createobject("Scripting.FileSystemObject")

Set objDir = FSO.GetFolder(strDir)
getInfo objDir, ignoreFolderCount


'**********************
Sub getInfo(pCurrentDir, ignoreFolderCount)

   For Each aItem In pCurrentDir.SubFolders
      getInfo aItem, ignoreFolderCount
   Next

   if pCurrentDir.Files.Count <= 1 then
      if pCurrentDir.Subfolders.Count = 0 or ignoreFolderCount then 
         wscript.echo pCurrentDir
      end if
   end if
End Sub

search_launch.bat with contents:

echo off
echo "Running process..."
cscript.exe search.vbs > search_results.txt
echo "Process complete"
pause

The first two lines of the search script take a folder path (change to suit), and a decision to ignore folders with one file which ALSO have a subfolder. The rest just recursively searches folders and checks the file count.

The search_launch batch file will launch the script using the console (rather than the windows gui) and will redirect output (the folder paths) to a text file called search_results.txt. This will be empty or it will have the names of any folders which match your criteria.

I got an "access denied error" when using against c:\ (probably need to run the batch file as admin), and this probably does not detect hidden files.

horatio

Posted 2011-12-28T15:55:06.723

Reputation: 3 345

running the batach didn't work for me (windows7), a file "search_results.txtecho" is created but it only contains "offecho "Running process..."cscript.exe search.vbs ". But running the .vbs did work (I didn't have a lot of folder to go through so it was ok for me to have a dozens of modal dialogs) – MagTun – 2015-11-16T06:45:10.640

Cheers horatio. That worked beautifully. Worked on a mapped network drive and also returned folders with 0 files (which in my case was helpful, so no complaint). Thanks very much. If anyone else is gonna use this, be sure to run the .bat and not the .vbs file, unless you feel like OKing a million dialogue boxes. – CreeDorofl – 2011-12-28T20:13:52.253

GG, and yeah, I think I forgot to explicitly mention that you want to run the batch file because the echo statements throw up a modal dialog otherwise. – horatio – 2011-12-28T21:18:00.870