Consulting

Results 1 to 9 of 9

Thread: Search for a folder and return full path.

  1. #1
    VBAX Regular
    Joined
    Oct 2011
    Posts
    11
    Location

    Search for a folder and return full path.

    I know the folder name that I am looking for and I know the parent directory name. The folder I am looking for is several sub folders deep in an array of sub folders beneath the parent directory. With VBA code I would like to be able to enter the parent directory, enter the sub directory name and recieve the resultant full path to that folder location.

    Thanks in advance for your help.

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    like this?

    [vba]
    Sub FullPathName()
    'http://vbaexpress.com/forum/showthread.php?t=39864

    Dim NumFolder As Integer
    Dim FullPath As String

    On Error Resume Next
    NumFolder = InputBox(Prompt:="Total Number of Parent & Subfolders, Start With Drive Letter", _
    Title:="SUB FOLDER LEVEL")

    If NumFolder = False Then Exit Sub 'Cancelled
    On Error GoTo 0

    For i = 1 To NumFolder
    FullPath = FullPath & InputBox(Prompt:="Drive (add ':' after drv letter) or Folder Name", _
    Title:="FULL PATH") & "\"
    Next

    Range("A1").Value = FullPath 'to cell
    'OR
    MsgBox FullPath 'to msgbox
    'OR
    Debug.Print FullPath 'to immediate window in VBE

    End Sub
    [/vba]
    Last edited by mancubus; 11-18-2011 at 09:31 AM.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Regular
    Joined
    Oct 2011
    Posts
    11
    Location

    Close - on the right track

    I have a folder under the J: drive called "jobs". Within the "jobs" directory are all the folders to projects in numerical order by job number. The are ordered numerically in groups first in folders by 1000 jobs and then in subfolders by 100 jobs. So the tree to job number "012021" is:

    J:\Jobs\12000-12999\12000-12099\012021

    I know that all jobs are uner the J:\Jobs and I know the folder title "012021". I am struggling with how to navigate to that specific folder within the tree. I need to access a specific file in the job folder within the code.

    Thanks again for your help.

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i'm not sure i'm understanding your requirement.

    1- you know the full path to file which is "J:\Jobs\12000-12999\12000-12099\012021"

    [vba]
    strPath = "J:\Jobs\12000-12999\12000-12099\012021"
    strFile = "specificFile.xlsx"

    MsgBox strPath & "\" & strFile
    'Workbooks.Open strPath & "\" & strFile
    [/vba]

    2- you know the part of the full path to file which is "J:\Jobs\.....-.....\.....-.....\012021" and look for a way that returns values for dots.
    to do so, you need a rule.

    perhaps:
    [vba]
    Dim strPath As String, strFile As String, strParPath As String
    Dim strUltPath As String, strMid As String, strMidPath As String

    strParPath = "J:\Jobs\"
    strUltPath = "012021" '6 dgt num as text
    strFile = "specificFile.xlsx"

    strMid = Mid(strUltPath, 2, 2) 'return "12" from "012021"
    strMidPath = strMid & "000-" & strMid & "999\" & strMid & "000-" & strMid & "099\"

    strPath = strParPath & strMidPath & strUltPath

    MsgBox strPath & "\" & strFile
    'Workbooks.Open strPath & "\" & strFile
    [/vba]


    3- you want something else...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    VBAX Regular
    Joined
    Oct 2011
    Posts
    11
    Location

    You stated it better than I did.

    "2- you know the part of the full path to file which is "J:\Jobs\.....-.....\.....-.....\012021" and look for a way that returns values for dots.
    to do so, you need a rule. "

    It did produce the path for the example I gave you. Which is perfect. I think you are the exact right path. The next job number, "010777" should return a path of "J:\Jobs\10000-10999\10700-10799\010777" but returned
    "J:\Jobs\10000-10999\10000-10099\010777"

    I will play with your code and see if I can't get to this result. Thanks so much for your help.

    Jeff

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    ok.

    you may use strMid2 = Mid(strUltPath, 2, 2) to return 10
    and strMid3 = Mid(strUltPath, 2, 3) to return 107




    ... not a good coding... for sure...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    [VBA]
    strMid2 = Mid(strUltPath, 2, 2) 'return "10" from "010777"
    strMid3 = Mid(strUltPath, 2, 3) 'return "107" from "010777"
    strMidPath = strMid2 & "000-" & strMid2 & "999\" & strMid3 & "00-" & strMid3 & "99\"
    [/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  8. #8
    VBAX Regular
    Joined
    Oct 2011
    Posts
    11
    Location
    Got it! Works great. Thanks a ton.

    Jeff

  9. #9
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you're wellcome.
    pls mark the thread as solved from thread tools dropdown which is above the first message.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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