Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 3 down vote accepted

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

   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"

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.

share|improve this answer
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 Dec 28 '11 at 20:13
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 Dec 28 '11 at 21:18
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) – Enora Nedelec Nov 16 '15 at 6:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.