Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursion with a twist 1

Status
Not open for further replies.

ckugo

IS-IT--Management
Jan 6, 2004
165
0
0
US
Hello all!

I am trying to develop some code that will do a kind of backwards recursive search.

I have a directory (D:\), that contains a folder (archive) that has over 2000 subfolders, and some of those folders have subfolders. I need a script to search through each folder and if there are no *.jpg's in a folder to return that folders name. The name pop-up can just be an echo, it does not have to get complicated as far as writing the folder name to another file or anything like that. Of course, if that is possible without a lot of extra effort, it would be nice. Most of my folders should contain jpg's, maybe only 5-10 will not. That is my reason for the search.

I have done my research on recursive searches, but none of them address this type of issue (that I could find). Where basically it checks at the end of each folder and then returns a value if necessary and then continues to the next folder.

I really appreciate any help on this.

Thanks!!

Chris
 
I think you should post your code up to date because your recursion issue is precisely what all recursion issues are.

In recursion like this you have a search algorithm and a payload function.

In your case the algorithm is to search for directories and if found, recurse the search to that new directory.

Your payload function, to be applied on all new directories is to search for files with *.jpg as their name and if none found, output the directory name.

I estimate 20 lines of code for this and I'll post it for you once I write it. Not because I think you want someone to do your work for you, but because it might help someone else as well one day :)
 
I just noticed that I am replying in the VBScript forum which is nto where I thought I was. Here is some VBA code in any case. I'll quicly learn VBScript and post some more code soon (I hope) hehe.

Code:
Option Explicit

Public Sub RecurseFolder(sPath As String)
  Dim colFolders As New Collection
  ' have to store the folders and check them after because the Dir
  ' function will get confused between CheckFolder and RecurseFolder
  Dim sFolder As String
  Dim c As Long
  
  CheckFolder (sPath)
  
  If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
  
  ' careful of directories that you have no read rights on.
  On Error Resume Next
  sFolder = Dir(sPath, vbDirectory)
  On Error GoTo 0
  
  While sFolder <> ""
    ' calling dir with no path will return all files including . and ..
    If sFolder <> "." And sFolder <> ".." And _
      ((GetAttr(sPath & sFolder) And vbDirectory) = vbDirectory) Then _
        colFolders.Add (sFolder)
    sFolder = Dir(, vbDirectory)
  Wend
  
  For c = 1 To colFolders.Count
    RecurseFolder (sPath & colFolders(c))
  Next
  
End Sub

Private Function CheckFolder(sPath As String)
  If Dir(sPath & "*.jpg", vbNormal) <> "" Then Debug.Print sPath
End Function
 
Not sure how good this is yet as it's my first VBScript. I named it test.vbs and ran from a command window with CScript test.vbs.

I'm not too sure about the error trapping method so a few lines wasted due to that. It seemed to work for me anyhow...

Code:
Function RecurseFolder(sPath)
   Dim fso, f, sf, f1
   if (right(sPath,1)<>"\") then sPath = sPath & "\"
   CheckFolder (sPath)

   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(sPath)
   Set sf = f.SubFolders
   On Error resume next
   if Err.Number = 0 then
     For Each f1 in sf
       RecurseFolder (sPath & f1.name)
     Next
   End if
End Function

Function CheckFolder(sPath)
   dim fso, f, f1, sf
   Set fso = CreateObject("Scripting.FileSystemObject")
   set sf = fso.GetFolder(spath)
   on Error resume next
   set f = sf.Files
   if Err.Number = 0 then
     for each f1 in f
       If (fso.GetExtensionName(f1.name) = "jpg") then 
         WScript.stdout.Writeline sPath  
         Exit function
       end if
     Next
   end if
End Function

RecurseFolder("d:\")
 
PCLewis,

Wow, what an amazing script. Very well done. The code that you wrote was going to give me the names of the folders that had jpg's in them. I needed to know the ones that did not have any jpg's. Also, if a folder contained no files at all, that code would not check it. Please do not feel like I am degrading your code, as it is amazing, but I just wanted to let you know. Here is what I pieced together. I came up with a function that counts the files in a folder and if it returns 0, it displays that folder as well. It now displays the folders that do not contain any jpg's or does not contain any files at all. Again, thank you so much for your help. Here is the final product:


Function Count(sPath,x)
x = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService. _
ExecQuery("Select * from CIM_DataFile where Path = 'sPath'")
For Each objFile in colFiles
x = x + 1
Next
end Function

Function RecurseFolder(sPath)
Dim fso, f, sf, f1
if (right(sPath,1)<>"\") then sPath = sPath & "\"
CheckFolder sPath,x

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(sPath)
Set sf = f.SubFolders
On Error resume next
if Err.Number = 0 then
For Each f1 in sf
RecurseFolder (sPath & f1.name)
Next
End if
End Function

Function CheckFolder(sPath,x)
dim fso, f, f1, sf
Set fso = CreateObject("Scripting.FileSystemObject")
set sf = fso.GetFolder(spath)
on Error resume next
set f = sf.Files
Count sPath,x
if Err.Number = 0 then
for each f1 in f
If (fso.GetExtensionName(f1.name) = "jpg") or (x <> 0) then
exit function
end if
Next
wscript.echo sPath
end if

End Function

RecurseFolder("c:\dump")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top