Results 1 to 10 of 10

Thread: Search for folders on a drive then open that folder if it is found.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    6

    Search for folders on a drive then open that folder if it is found.

    Is it possible to create a file system that could search for a record using wild card characters then open the file if it is found. For example my DB form is something in the effect of http://allenbrowne.com/ser-62.html I want the user to be able to click on one of the records shown then I want my application to search a predetermined drive, say the I drive, and if the ID is found, say the name of the Folder is cutomer1 so I guess it would have to have some kind of wild card expression, then I want it to open that folder and display the results to the user. Is this possible? There may not be a folder for every customer, and the naming conventions used for the folder may be different so it would have to be a wild card. Of course My Id field is a little more specific so im not so worried about it finding multiple folders that share common numbers.

  2. #2
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Search for folders on a drive then open that folder if it is found.

    The Dir() command supports wildcards when finding either files or folders.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    6

    Re: Search for folders on a drive then open that folder if it is found.

    Private Sub Text30_Click()
    Dim sUserFile As String
    sUserFile = Dir$("L:\Test Results\TR'S\TR" & Text30.Text, vbDirectory)

    On Error GoTo err
    Dim bb
    bb = Shell("Explorer.exe L:\Test Results\TR'S\" & sUserFile, 1)
    Exit Sub
    err:
    MsgBox err.Description



    End Sub

    This is my code so far. I cant get it to search in subdirectories and I would be albe to leave out the TR if I could get some kind of wild card expression after TR's. like L:\Test Results\TR'S\*.text30 but that expression wont work. I Havent used VB for years and even then I didnt have to much experience somebody help plz. I Dont really care to much about the files as long as I can search an verify if a folder is there and if it is use the shell or some other command to open the folder. Does anybody have any idea's? Ive seen many samples that included a recrusive search for files but none suit my purpose for displaying folders.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Search for folders on a drive then open that folder if it is found.

    Welcome to the forums.

    Try
    Code:
    If Dir$("L:\Test Results\TR'S\TR" & Text30.Text, vbDirectory) <> vbNullString Then
       MsgBox "directory exists"
    Else
       MsgBox "directory does not exist"
    End If

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    6

    Re: Search for folders on a drive then open that folder if it is found.

    Quote Originally Posted by Hack View Post
    Welcome to the forums.

    Try
    Code:
    If Dir$("L:\Test Results\TR'S\TR" & Text30.Text, vbDirectory) <> vbNullString Then
       MsgBox "directory exists"
    Else
       MsgBox "directory does not exist"
    End If
    I need it to be able search sub folders as well. So it will have to be recrusive some how but I really dont know where to start when searching for just folders.

  6. #6
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Search for folders on a drive then open that folder if it is found.

    Several ways...

    As mentioned above the Dir Function, then there is the slowest, the FSO (File System Object, the fasted would be the API's FindFirstFile and FindNextFile, or you could actually use the DirListBox.

    Your process would be something like...

    psudo code

    Begin sub/function(PathToSearch as String)
    Start Loop
    Result = MethodUsedToSearchDir
    If Result Is Directory Then
    Add Item To Collection/Dictionary/Array
    End If
    Loop until no more directories are found

    From beginning to end of collection/dictionary/array
    call this sub/function code with directory path
    loop until each item in collection/dictionary/array has been used to call search routine

    end sub/function



    Good Luck
    Option Explicit should not be an Option!

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    6

    Re: Search for folders on a drive then open that folder if it is found.

    Ok guys this is the code I have so far:

    Code:
    Option Compare Database
    Option Explicit
    Dim strnow As String
    Dim subFol As New Collection
    Dim f As FileSystemObject
    Dim folder As Variant
    
    Private Sub cmdFilter_Click()
        
                         'The criteria string.
        Dim lngLen As Long                      'Length of the criteria string to append to.
        Const conJetDate = "\#mm\/dd\/yyyy\#"
        Dim strWhere As String
        
        
        If Not IsNull(Me.textFilterTR_) Then
            strWhere = strWhere & "([TR#] Like ""*" & Me.textFilterTR_ & "*"") AND "
        End If
        
        If Not IsNull(Me.textFilterOriginator) Then
            strWhere = strWhere & "([Originator] Like ""*" & Me.textFilterOriginator & "*"") AND "
        End If
        
        If Not IsNull(Me.textFilterPart_) Then
            strWhere = strWhere & "([Part #] Like ""*" & Me.textFilterPart_ & "*"") AND "
        End If
        
        If Not IsNull(Me.textFilterCell_or_battery) Then
            strWhere = strWhere & "([Cell or Battery] Like ""*" & Me.textFilterCell_or_battery & "*"") AND "
        End If
        
        If Not IsNull(Me.textFilter_Type) Then
            strWhere = strWhere & "([Type] Like ""*" & Me.textFilter_Type & "*"") AND "
        End If
        
        If Not IsNull(Me.textFilterTestlabstatus) Then
            strWhere = strWhere & "([TestLabStatus] Like ""*" & Me.textFilterTestlabstatus & "*"") AND "
        End If
        
        If Not IsNull(Me.txtStartDate) Then
            strWhere = strWhere & "([Date] >= " & Format(Me.txtStartDate, conJetDate) & ") AND "
        End If
        
        If Not IsNull(Me.txtEndDate) Then   'Less than the next day.
            strWhere = strWhere & "([Date] < " & Format(Me.txtEndDate + 1, conJetDate) & ") AND "
        End If
        
        lngLen = Len(strWhere) - 5
        If lngLen <= 0 Then     'Nah: there was nothing in the string.
            MsgBox "No criteria", vbInformation, "Nothing to do."
        Else                    'Yep: there is something there, so remove the " AND " at the end.
            strWhere = Left$(strWhere, lngLen)
            'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
            'Debug.Print strWhere
            
            Me.Filter = strWhere
            Me.FilterOn = True
            strnow = strWhere
    
        
            
        End If
    End Sub
    Private Sub cmdReset_Click()
    
    'Purpose:   Clear all the search boxes in the Form Header, and show all records again.
        Dim ctl As Control
        
        'Clear all the controls in the Form Header section.
        For Each ctl In Me.Section(acHeader).Controls
            Select Case ctl.ControlType
            Case acTextBox, acComboBox
                ctl.Value = Null
            Case acCheckBox
                ctl.Value = False
            End Select
        Next
        
        'Remove the form's filter.
        Me.FilterOn = False
    End Sub
    
    Private Sub Command35_Click()
    DoCmd.OpenReport "Report1", acViewPreview, , strnow
    
    End Sub
    
    Private Sub Command50_Click()
    
    For Each folder In subFol
    lstFileList.AddItem folder
    
    Next
    
    End Sub
    
    Private Sub Form_BeforeInsert(Cancel As Integer)
        'To avoid problems if the filter returns no records, we did not set its AllowAdditions to No.
        'We prevent new records by cancelling the form's BeforeInsert event instead.
        Cancel = True
        MsgBox "You cannot add new information to the search form.", vbInformation, "Permission denied."
    End Sub
    
    Private Sub Form_Load()
     Set f = CreateObject("Scripting.FileSystemObject")
     Call findFolders
     Call Command50_Click
     
    End Sub
    
    Private Sub Form_Open(Cancel As Integer)
        
        Me.Filter = "(False)"
        Me.FilterOn = True
    End Sub
    
    
    
    Private Sub Text30_Click()
    
    Call findFolders
    
    End Sub
    
    Private Sub textFilterOriginator_Change()
    
    'Call cmdFilter_Click
    
    End Sub
    
    
     Private Function findFolders()
     Dim suserFile As String
     Dim folderFoundBoolean As Boolean
     Dim subfolder As String
     Dim fol As Object
    
     
    
     suserFile = "L:\Test Results\TR'S"
     
     If f.FolderExists(suserFile) = True Then
            If Right(suserFile, 1) <> "\" Then suserFile = suserFile & "\"
            Set fol = f.GetFolder(suserFile)
            subFol.Add fol.Path, fol.Path
            MsgBox "Itemfound"
            For Each folder In fol.SubFolders
                subFol.Add folder.Path & "\", folder.Path
                RecurseSubFolders (subFol.Item(folder.Path))
              
            Next
            Else
            MsgBox "Folder does not exist."
        End If
    
    
    
    
    
    
    
    
    
    
    End Function
    
    Private Sub RecurseSubFolders(rPath As String)
        Dim fol As Object
        
        Set fol = f.GetFolder(rPath)
        For Each folder In fol.SubFolders
            subFol.Add folder.Path & "\", folder.Path
            
        Next
    End Sub
    Ok I got ot to display all folders and subfolders into a list box which I plan to make invisible. Does anyone have any idea's how I can use a wild card charachter based on a field number to search the list then have shell open the file it finds if any. I guess I could make it simple by saving the list somehow so it doesnt take forever to load every time I open the database?

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    6

    Re: Search for folders on a drive then open that folder if it is found.

    Is there any way to save the contents of a list box in microsoft access to a file, the streamwriter command wont work in access vb, I cant find the reference for the IO.namespace file? Also the listbox.selectedindex is not reconginized by access vb so whats the alternative method for getting listbox selections in VB?

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Search for folders on a drive then open that folder if it is found.

    just loop through the listbox and assign the values to a field, or a textfile, whereever you want to save to

    it looks to me like this is in access and should be moved to office development forum
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Search for folders on a drive then open that folder if it is found.

    It certainly seems that way... Thread moved to Office Development/VBA forum (note that the "VB Editor" in Office programs is actually VBA rather than VB, so the VB6 forum is not really apt)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width