Solved

Find folder with two unknown parameters

Posted on 2013-12-30
8
216 Views
Last Modified: 2014-01-03
Hi

Need to find a folder in a path like

\\servername\knownfoldername\unknownfoldername\partiallyknownfoldername

I need the ouput to be the unknownfoldername. I know that there is exactly one possible value.

For the partiallyknownfoldername there may be multiple values.

Everlas
0
Comment
Question by:Everlas
  • 4
  • 3
8 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39746692
This should get you started:
Option Explicit

Const strPath = "\\servername\knownfoldername" ' fill in the base path here

Dim arrSearch, objFSO, objFolder, objSubFolder, objSubSubFolder, strPartialName

arrSearch = Array("partiallyknownfoldername", "xyz") ' find one of these values in sub-subfolder names

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)
For Each objSubFolder In objFolder.SubFolders
	For Each objSubSubFolder In objSubFolder.SubFolders
		For Each strPartialName In arrSearch
			If InStr( LCase(objSubSubFolder.Name), LCase(strPartialName) ) > 0 Then
				MsgBox "Found '" & objSubSubFolder.Name & "' in '" & objSubFolder.Name & "'."
			End If
		Next
	Next
Next
Set objFolder = Nothing
Set objFSO = Nothing

Open in new window

0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39750032
Did that get the job done, or did you need something different?

~bp
0
 

Author Comment

by:Everlas
ID: 39754164
Hi
It looked quite simple, but I get the error "invalid outside procedure" when I get to "partialfoldername", so I can't get it to run.

Everlas
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39754234
this was meant to be run from a .vbs file (as you posted in vbscript), if you're running as VBA, you need to stick the code in a Sub and run that:
Option Explicit

Const strPath = "\\servername\knownfoldername" ' fill in the base path here

Sub runcode()

Dim arrSearch, objFSO, objFolder, objSubFolder, objSubSubFolder, strPartialName

arrSearch = Array("partiallyknownfoldername", "xyz") ' find one of these values in sub-subfolder names

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)
For Each objSubFolder In objFolder.SubFolders
    For Each objSubSubFolder In objSubFolder.SubFolders
        For Each strPartialName In arrSearch
            If InStr(LCase(objSubSubFolder.Name), LCase(strPartialName)) > 0 Then
                MsgBox "Found '" & objSubSubFolder.Name & "' in '" & objSubFolder.Name & "'."
            End If
        Next
    Next
Next
Set objFolder = Nothing
Set objFSO = Nothing

End Sub

Open in new window

0
 

Author Closing Comment

by:Everlas
ID: 39754573
Sorry, should have seen the problem myself. Works perfectly. Thanks.
However, I wonder why I get hourglass after the msgbox. Should the End if not kill the sub instantly? Is it just the closing down which is slow?

Do you know if the same result could have been accomplished with the Dir command?

Everlas
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39754627
While the msgbox is open you mean? That would be logical as the msgbox blocks the program. Or after you press OK? It will keep checking the rest of the folders but it shouldn't take very long unless you've got 1000s of folders.

"Dir" could be possible but I've done very little with that, I think it has a lot fewer options than you get with the FileSystemObject.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39754774
So, this may or may not still interest you, but I felt challenged to find a solution with VBA's Dir command and here it is, maybe not the most elegant but works for me ;-)
Option Explicit

Const strPath = "\\servername\knownfoldername" ' fill in the base path here

Sub runcode2()
    Dim dn As String
    Dim arrdn() As String
    Dim cntdn As Integer
    cntdn = -1
    dn = Dir(strPath & "\", vbDirectory) ' note: returns all entries, files as well
    While dn <> ""
        If dn <> "." And dn <> ".." And (GetAttr(strPath & "\" & dn) And vbDirectory) = vbDirectory Then
            cntdn = cntdn + 1
            ReDim Preserve arrdn(cntdn)
            arrdn(cntdn) = dn
        End If
        dn = Dir
    Wend
    Dim arrSearch
    arrSearch = Array("partiallyknownfoldername", "xyz") ' find one of these values in sub-subfolder names
    Dim i As Integer
    For i = 0 To cntdn
        Dim pn
        For Each pn In arrSearch
            'Debug.Print "checking: " & arrdn(i) & " for '*" & pn & "*'"
            dn = Dir(strPath & "\" & arrdn(i) & "\*" & pn & "*", vbDirectory)
            While dn <> ""
                If (GetAttr(strPath & "\" & arrdn(i) & "\" & dn) And vbDirectory) = vbDirectory Then
                    Debug.Print "found: " & dn & " under " & arrdn(i)
                    i = cntdn ' exit outer For as well
                    Exit For
                End If
                dn = Dir
            Wend
        Next
    Next
End Sub

Open in new window

0
 

Author Comment

by:Everlas
ID: 39754862
Yes, still interested. And I'm going to check it out. In the end the task is to create a script that doesn't break the server or the computer and is more or less independent of any speciel configuration on the actual computer. And to an extent backward compatible.

I guess that if the the filesystem is very large an complex, one might create a small database as an alternative to searching the filesystem. Someday classic filesystems as such stop to exist and then we do it in another way :-)

Regards
Everlas
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question