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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search multiple folders for filename using wildcard 4

Status
Not open for further replies.

cwsstins

MIS
Aug 10, 2004
412
0
0
US
I've been able to string together the basic code for what I want by searching TekTips and other sites. The code below will search a network share for a particular file and write the folder|filename to a text file. What I need is for it to search multiple directories that have a similar structure:

n:\prod\00572\From-Other\YYYYMMDD-Download
n:\prod\00573\From-Other\YYYYMMDD-Download
n:\prod\00574\From-Other\YYYYMMDD-Download
...

...and I need for it to search not for a specific file but for any file that contains "OP." prior to the extension (the extension is incremental numeric). So my output text file should look something like this:

n:\prod\00572\From-Other\YYYYMMDD-Download|CR13OP.001
n:\prod\00572\From-Other\YYYYMMDD-Download|CR13OP.002
n:\prod\00572\From-Other\YYYYMMDD-Download|CITNOP.001
n:\prod\00573\From-Other\YYYYMMDD-Download|CR13OP.001
n:\prod\00574\From-Other\YYYYMMDD-Download|YUITOP.001

Help is appreciated!

Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolderName = "n:\prod\00572\From-Other\" & Year(Date) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & "-Download"
'strFileName = strFolderName & "\" & "CR13OP.001"
strFileName = strFolderName & "\" & objFSO.GetExtensionName()
If objFSO.FileExists(strFileName) Then

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile("C:\prod\Test.txt")
objTextFile.Writeline objFSO.GetFolder(strFolderName) & "|" & objFSO.GetFileName(strFileName)
 
Wow...was my question that boring? I figured out how to get around the wildcard limitation, still just trying to figure out how I can search from root folder into subfolder >> subfolder >> subfolder for multiple directories. Any suggestions?
 
You want a recursive search.
This is a good start: faq329-5515

If you search the forum on the terms "recursive search" or "search subfolders" you'll get dozens of more hits some with good example code.
 
The closest thing I can find is a script that will recursively search every subfolder in a main folder. That is a problem for me because I need it to search only a specific subfolder within a subfolder within a subfolder with a folder (in my example above, 00572 has other subfolders, but I'm only interested in "From-Other". This is what I'm trying right now, but it's not working (getting Type Mismatch on first "FolderExists")

Set oFolder = fso.GetFolder(MainPath)
For each oSubFolder in oFolder.SubFolders
If FolderExists("From-Other") Then
If FolderExists(strDate & "-Download") Then
For Each oFile in oFolder(strDate & "-Download")
If Mid(oFile.Name,7,2) = "OP" Then
Set objTextFile = fso_OpenTextFile(LogPath & "\Search_Saig_Folders_" & Year(Date) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & ".log", ForAppending, True)
objTextFile.Writeline fso.GetFolder(MainPath) & "\From-Other\" & strDate & "-Download" & "|" & fso.GetFileName(oFile.Name)
Set objTextFile = nothing
End If
Next
End If
End If
next
 
[blue]Methods cannot be called without an obect. Make sure you include them.[/blue][red] Also, the method is relative to the fso object and therefore requires the full path of the folder/file name to function expectedly.[/red]

Code:
If [blue]fso.[/blue]FolderExists("[red]\\server\share\[/red]From-Other") Then
If [blue]fso.[/blue]FolderExists("[red]c:\path\to\folder\" & [/red]strDate & "-Download[/red]") Then

-Geates



"I do not offer answers, only considerations."
- Geates's Disclaimer

 
Thanks Geates. But my problem is that I'm trying to avoid having to indicate the [red]\\server\share [/red]piece. If I have to enter the full path, then I'll need to hard-code over 100 paths.

So instead of this:
n:\prod\00572\From-Other\YYYYMMDD-Download
n:\prod\00573\From-Other\YYYYMMDD-Download
n:\prod\00574\From-Other\YYYYMMDD-Download
...

I'm trying to get to:
n:\prod\recursvively search prod\From-Other\YYYYMMDD-Download
 
not at all, just the beginning path.

is a folder "YYYYMMDD-Download" always located inside "From-Other"? If so, there is no point in seaching for "From-Other" when you can search for folders that have "-Download" in them. [red]Additionally, you can check to see if the parent folder is "From-Other"[/red]

Code:
set objFSO = CreateObject("Scripting.FileSystemObject")
function findOPs(strDir)
	set objFolder = objFSO.GetFolder(strDir)
	
	for each objSubFolder in objFolder.SubFolders
		findOPs objSubFolder.Path
	next

	if (inStr(objSubFolder.Name = "-Default") then
		[red]if (objFSO.GetParentFolder(objSubFolder.Path) = "From-Other") then[/red]
			for each objFile in objFolder.Files
				if (inStr(objFile.Name, "OP.")) then
					[gray]//do something[/gray]
				end if
			next
		[red]end if[/red]
	end if
end function

-Geates

"I do not offer answers, only considerations."
- Geates's Disclaimer

 
I think I'm almost there...the script will complete now, but it doesn't actually write to the file like I'm telling it to...which I assume means I'm not truly finding the right subfolder that contains my files?

Code:
MainPath = "c:\prod\saig"
Set oFolder = fso.GetFolder(MainPath)
  For each oSubFolder in oFolder.SubFolders
    if (inStr(oSubFolder.Name,"-Download")) then
	  set oFolder = oSubFolder.Name
	  for each oFile in oFolder.Files
		if (inStr(oFile.Name, "OP.")) then
          Set objTextFile = fso.OpenTextFile(LogPath & "\Search_Saig_Folders_" & Year(Date) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & ".log", ForAppending, True)
		  objTextFile.Writeline fso.GetFolder(MainPath) & "\From-Other\" & strDate & "-Download" & "|" & fso.GetFileName(oFile.Name)
		  Set objTextFile = nothing	   
		End If	   
Next    
    End If
  Next
 
cwsttins:
In the code you posted, only files contained in folders with this format will be looked at: n:\prod\saig\?????-Download

This is different from what you stated you wanted originally. Geates code probably gets you closer to a solution, if not all the way there.
 
Maybe I should have prefaced this post by saying that I don't really know what I'm doing...although that is probably fairly obvious from my responses. I had been trying to avoid using the function because I already have this code as part of a sub where I gather input from user (HTA wrapper) and wasn't sure how to get the function to work within a sub. To try to get a better handle on this, I ran just Geates code (modified a bit and looking at C drive), but when I run it, nothing happens. There is no log file created.

Code:
strDir = "c:\prod\saig"
LogPath = "C:\Search_SAIG"
set objFSO = CreateObject("Scripting.FileSystemObject")
function findOPs(strDir)
	set objFolder = objFSO.GetFolder(strDir)
	
	for each objSubFolder in objFolder.SubFolders
		findOPs objSubFolder.Path
	next

	if (inStr(objSubFolder.Name = "-Download")) then
		if (objFSO.GetParentFolder(objSubFolder.Path) = "From-Other") then
			for each objFile in objFolder.Files
				if (inStr(objFile.Name, "OP.")) then
					Set objTextFile = fso.OpenTextFile(LogPath & "\Search_Saig_Folders_" & Year(Date) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & ".log", ForAppending, True)
					objTextFile.Writeline fso.GetFolder(strDir) & "\From-Other\" & strDate & "-Download" & "|" & fso.GetFileName(oFile.Name)
					Set objTextFile = nothing
				end if
			next
		end if
	end if
	
end function
 
- [blue]You have to call the function to run it.[/blue]
- [red]The only thing that separators a function from a sub is that a function is if it returns a value. I use function by default. In this case, however, a declaring it a sub is fine.[/red]
- [green]No need to open and close the log file more than once.[/green]

Code:
[red]sub[/red] findOPs(strDir)
	set objFolder = objFSO.GetFolder(strDir)
	
	for each objSubFolder in objFolder.SubFolders
		findOPs objSubFolder.Path
	next

	if (inStr(objSubFolder.Name = "-Download")) then
		if (objFSO.GetParentFolder(objSubFolder.Path) = "From-Other") then
			for each objFile in objFolder.Files
				if (inStr(objFile.Name, "OP.")) then
					objTextFile.Writeline fso.GetFolder(strDir) & "\From-Other\" & strDate & "-Download" & "|" & fso.GetFileName(oFile.Name)
				end if
			next
		end if
	end if
	
[red]end sub[/red]

'*** Main ***
strDir = "c:\prod\saig"
LogPath = "C:\Search_SAIG"
set objFSO = CreateObject("Scripting.FileSystemObject")

[green]Set objTextFile = fso.OpenTextFile(LogPath & "\Search_Saig_Folders_" & Year(Date) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & ".log", ForAppending, True)[/green]
[blue]findOps(strPath)[/blue]
[green]objTextFile.close
Set objTextFile = nothing[/green]

-Geates

"I do not offer answers, only considerations."
- Geates's Disclaimer

 
but when I run it, nothing happens. There is no log file created.
When that happens, it can be helpful to log what is happening at certain key points. As an example, I added a few lines to Geates' code that should indicate to you what paths are being checked. This could be used to figure out which folders are (or aren't) being analyzed and explain why you are getting no results.

Code:
sub findOPs(strDir)
	set objFolder = objFSO.GetFolder(strDir)
	
	for each objSubFolder in objFolder.SubFolders
		findOPs objSubFolder.Path
	next

	if (inStr(objSubFolder.Name = "-Download")) then
		[highlight #FCE94F]objTextFile.Writeline "Found download folder: " & objSubFolder.Path[/highlight]
		if (objFSO.GetParentFolder(objSubFolder.Path) = "From-Other") then
			[highlight #FCE94F]objTextFile.Writeline "... and Download folder also has 'from-other' parent folder: " & objSubFolder.Path[/highlight]
			for each objFile in objFolder.Files
				if (inStr(objFile.Name, "OP.")) then
					objTextFile.Writeline fso.GetFolder(strDir) & "\From-Other\" & strDate & "-Download" & "|" & fso.GetFileName(oFile.Name)
				end if
			next
		else
			[highlight #FCE94F]objTextFile.Writeline "... but Download folder does not have 'from-other' parent folder: " & objSubFolder.Path[/highlight]
		end if
	else
		[highlight #FCE94F]objTextFile.Writeline "Skipping folder: " & objSubFolder.Path[/highlight]
	end if
	
end sub
 
Sorry guys, but I'm a bit confused. So for this code, if I run it on its own, I'm still not getting any results at all...I mean, I run the vbs file and...nothing. I'm sure it's something I'm doing wrong...this is what I'm running:

Code:
strDir = "c:\prod\saig"
LogPath = "C:\Search_SAIG"
set objFSO = CreateObject("Scripting.FileSystemObject")

sub findOPs(strDir)
	set objFolder = objFSO.GetFolder(strDir)
	
	for each objSubFolder in objFolder.SubFolders
		findOPs objSubFolder.Path
	next

	if (inStr(objSubFolder.Name = "-Download")) then
		objTextFile.Writeline "Found download folder: " & objSubFolder.Path
		if (objFSO.GetParentFolder(objSubFolder.Path) = "From-Other") then
			objTextFile.Writeline "... and Download folder also has 'from-other' parent folder: " & objSubFolder.Path
			for each objFile in objFolder.Files
				if (inStr(objFile.Name, "OP.")) then
					objTextFile.Writeline fso.GetFolder(strDir) & "\From-Other\" & strDate & "-Download" & "|" & fso.GetFileName(oFile.Name)
				end if
			next
		else
			objTextFile.Writeline "... but Download folder does not have 'from-other' parent folder: " & objSubFolder.Path
		end if
	else
		objTextFile.Writeline "Skipping folder: " & objSubFolder.Path
	end if
	
end sub
 
Look at Geates' last post... you missed the code at the end, in green and blue
 
I thought the part in green was indicating that I didn't need it because it was redundant...when I run this, I get "invalid procedure call or argument" on the Set objTextFile:

Code:
strDir = "c:\prod\saig"
LogPath = "C:\Search_SAIG"
set objFSO = CreateObject("Scripting.FileSystemObject")

sub findOPs(strDir)
	set objFolder = objFSO.GetFolder(strDir)
	
	for each objSubFolder in objFolder.SubFolders
		findOPs objSubFolder.Path
	next

	if (inStr(objSubFolder.Name = "-Download")) then
		objTextFile.Writeline "Found download folder: " & objSubFolder.Path
		if (objFSO.GetParentFolder(objSubFolder.Path) = "From-Other") then
			objTextFile.Writeline "... and Download folder also has 'from-other' parent folder: " & objSubFolder.Path
			for each objFile in objFolder.Files
				if (inStr(objFile.Name, "OP.")) then
					objTextFile.Writeline fso.GetFolder(strDir) & "\From-Other\" & strDate & "-Download" & "|" & fso.GetFileName(oFile.Name)
				end if
			next
		else
			objTextFile.Writeline "... but Download folder does not have 'from-other' parent folder: " & objSubFolder.Path
		end if
	else
		objTextFile.Writeline "Skipping folder: " & objSubFolder.Path
	end if
	
end sub  

Set objTextFile = objFSO.OpenTextFile(LogPath & "\Search_Saig_Folders_" & Year(Date) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & ".log", ForAppending, True)
findOps(strPath)
objTextFile.close
Set objTextFile = nothing
 
Add this at the top of your script:
Const ForAppending = 8

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV. I just added that as the first line and now I'm getting the same msg "invalid procedure call or argument" but now getting it on the [red]set objFolder = objFSO.GetFolder(strDir)[/red] line (line 6 in the code above).
 
Replace this:
strDir = "c:\prod\saig"
with this:
strPath = "c:\prod\saig"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks for your help guys, I think I'm almost there. I've gotten it to work locally when I have the path set to c:\prod\saig. However, when I change it to a network share, nothing happens. There is no error, it just doesn't write to the log. Also, my CompleteMsg sub runs for each file found, I am trying to get it to only run once at the end, to let user know it's finished.

Code:
' If no value is entered, prompt the user to enter a date
Sub SearchFolders
  If SearchDate.Value = "" Then
    MsgBox "You must enter a date in format YYYYMMDD"
  Exit Sub	
  End If

  'Capture value from "Search on Date" button click
	strDate = SearchDate.Value

'Call the sub to capture and log the path
ShowSubfolders FSO.GetFolder("n:\dev\saig")  
	
End Sub

Set FSO = CreateObject("Scripting.FileSystemObject") 
Const ForReading = 1, ForWriting = 2, ForAppending = 8

' Path to which to save the log file
LogPath = "C:\Search_SAIG\"

Sub ShowSubFolders(Folder)     

For Each Subfolder in Folder.SubFolders         

' If log file path doesn't exist, create the folder  
  If Not fso.FolderExists(LogPath) Then
    fso.CreateFolder(LogPath)
  End If  

' If OP file exists in the Download folder for the date selected (strDate), log the full path and filename
If (inStr(Folder.Name, strDate & "-Download")) Then
  For Each File in Folder.Files
         If (inStr(File.Name, "OP.")) Then
         Set objTextFile = FSO.OpenTextFile(LogPath & "Search_Saig_Folders_" & Year(Date) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & ".log", 8, True)
		 objTextFile.Writeline FSO.GetFolder(Folder) & "\From-Other\" & strDate & "-Download" & "|" & FSO.GetFileName(File.Name)
		 Set objTextFile = nothing	   
	     End If	   

Next

		 End If
ShowSubFolders Subfolder     

Next 

CompleteMsg ()

End Sub

Sub CompleteMsg
  MsgBox "Search completed, please see results in log file at the following location: " & LogPath
End Sub
 
If it matters, my network share is already mapped as a drive.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top