Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

up vote 2 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
   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.

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

Your Answer

 
discard

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.