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)
 
First, network mapped drives should work exactly the same way local drives work, assuming you have permissions correct.

Second, your subroutines will never execute unless they are called. They must be called from a line of code outside a subroutine. Currently, the only lines that are outside a subroutine are the following lines:

[tt]Set FSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1, ForWriting = 2, ForAppending = 8
' Path to which to save the log file
LogPath = "C:\Search_SAIG\"[/tt]

Every other line of code is inside either the "SearchFolders" sub, the "ShowSubFolders" sub, or the "CompleteMsg" sub. And as you can see, none of those subs are ever called, therefore your script runs those four lines alone and quits.

 
Then I'm confused as to why it works when i run it with the search path set to local drive. For the first sub (SearchFolders), I'm calling that when the user clicks a button. How can I subsequently call the ShowSubFolders sub after the SearchFolders sub runs? I would have thought the call to the second sub would have to be included in the first sub. As for the CompleteMsg sub, I don't think I really need it as its own sub, but again just need to call that message to run once at the end.
 
>I would have thought the call to the second sub would have to be included in the first sub

Er ... it is, as far as I can see

>As for the CompleteMsg sub, I don't think I really need it as its own sub ...

Possibly not, but it is often better to break code down into seperate routines

>just need to call that message to run once at the end

The call to CompleteMSg is in the wrong routine. Move it to the end of SearchFolders.
 
I believe it should be:

Code:
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([highlight #FCE94F]Subfolder[/highlight].Name, strDate & "-Download")) Then
         For Each File in [highlight #FCE94F]Subfolder[/highlight].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 

   [s]CompleteMsg ()[/s]

End Sub

And as strongm said, move CompleteMSg to the end of SearchFolders.
 
Those two items did the trick! Thanks to all for your input and help, much appreciated.
 
Glad it worked out... remember the trick of logging certain actions of your script, either by writing lines to a log file, or echoing lines to the screen (wscript.echo) .... that has helped me immensely, both in the short term (making this script work) and in the long term (making that NEXT script work)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top