• Find folder name (2003 SP3)

    Author
    Topic
    #455217

    Hello All,
    I have some code that is looking to see if a folder exists on a network drive. It works as long as the folder name matches exactly the string I am looking for. I would like to have it find anything starting with the value that I am searching for. For instance, the folder could be named “123456” or “123456 Customer Name”. The code:

    strHLAddress = “usabox12ServiceDataCustomer filesSystem Data” & Me.Sales_order & “”

    is looking for the sales order folder but I would like to use a wildcard, similar to dos commands, like “*”, after the sales order number. Is this something I can do?

    Thanks,
    Mark

    Viewing 0 reply threads
    Author
    Replies
    • #1132119

      You can test like this:

      strHLAddress = "usabox12ServiceDataCustomer filesSystem Data" & Me.Sales_order & "*"
      If Dir(strHLAddress, vbDirectory) = "" Then
      ' There is no such folder
      Else
      ' There is at least one such folder
      End If

      • #1132348

        Hi Hans,
        What I am trying to do is open the folder location after it figures out if the folder is there. Unfortunately, the name of the folder may be different than what is located in the form control. The folder name will always begin with 6 numbers (i.e. 123456) but there may be something that comes after it, like a customer name. I want to search for the folder that starts with the 6 numbers and open that folder. Now, how can I do that?

        Thanks,
        Mark

        • #1132360

          The expression Dir(strHLAddress, vbDirectory) from my previous reply will return the name of the first folder that matches the pattern, or an empty string if there is no matching folder.

          • #1132364

            Hi Hans,
            I tried as you stated before with the “*” after the first 6 numbers and it doesn’t return that folder name. The DIR command is not coming back with anything but the folder is there. Got any other ideas?

            Thanks,
            Mark

            • #1132369

              Check carefully whether there’s a typo somewhere. The code does work, I’ve tested it (again).

            • #1132372

              Hi Hans,
              Here is what I have:

              If IsNull(Sales_order) Or Sales_order = “” Then
              strHLAddress = “usabox12ServiceDataCustomer filesSystem Data”
              Else: strHLAddress = “usabox12ServiceDataCustomer filesSystem Data” & Me.Sales_order & “*”
              End If

              strHLAddress1 = strHLAddress

              If Dir(strHLAddress1, vbDirectory) = “” Then
              MsgBox “Sales Order folder not found. Please find the correct folder and update name.”, vbOKOnly
              strHLAddress = “usabox12ServiceDataCustomer filesSystem Data”
              End If

              Me.Command32.HyperlinkAddress = strHLAddress

              In this case, the folder name is “usabox12ServiceDataCustomer filesSystem Data250076, customer name”. The code is looking for 250076* and the dir command doesn’t find it.

              How are you testing it? If I create a folder with the 6 numbers alone, it opens it every time without fail.

              Thanks,
              Mark

            • #1132375

              Perhaps Sales_order doesn’t equal 250076?

              I created a subfolder named 123456, Jones in a folder named C:Data.
              I used the following test procedure:

              Sub Test()
              Dim strParent As String
              Dim strName As String
              Dim strFolder As String
              strParent = “C:Data”
              strName = “123456”
              strFolder = Dir(strParent & “” & strName & “*”, vbDirectory)
              If strFolder = “” Then
              MsgBox “Not found”, vbExclamation, “Folder Test”
              Else
              MsgBox strFolder, vbInformation, “Folder Test”
              End If
              End Sub

              Result is shown below.

            • #1132376

              Hi Hans,
              OK, it is working now. What I think happened was that the string was being changed back to the value with the * in it and therefore couldn’t open the directory. Now I am using this and it works as advertised:

              Private Sub Command32_Click()
              On Error Resume Next

              Dim strParent As String
              Dim strName As String
              Dim strFolder As String, strFolder1 As String
              strParent = “usabox12ServiceDataCustomer filesSystem Data”
              strName = Me.Sales_order
              strFolder = Dir(strParent & “” & strName & “*”, vbDirectory)
              If strFolder = “” Then
              MsgBox “SO folder not found”, vbExclamation, “Folder Test”
              Else
              ‘MsgBox strFolder, vbInformation, “Folder Test”
              strFolder1 = strParent & strFolder
              End If

              Shell (strFolder1), vbNormalFocus
              ‘Me.Command32.HyperlinkAddress = strFolder1

              End Sub

              Thanks for the help!
              Mark

            • #1132374

              Hi again Hans,
              Testing some more shows that the DIR command is returning something. However, the hyperlink command or shell results in the following error (see attachment)

              Thanks,
              Mark

      • #1132353

        Edited by HansV to provide link to post – see Help 19

        I have looked through post 715155 and this is the type pf path I am looking for.

        Similar issue I have but the text following the initial string will be different. Is there a way to step through a main folder(fixed location) looking for a specific subfolder starting with a known string? I am not looking to store that info in any way, just to open the folder it finds. The code I have now looks for the folder name and when it doesn’t find it exactly, it opens the main folder for browsing.

        Thanks,
        Mark

    Viewing 0 reply threads
    Reply To: Find folder name (2003 SP3)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: