Search code examples
excelvba

Opening/Retrieving File Via Name in VBA With Variable Folder Names


I am a semi-green build and deploy specialist, in which I am responsible for deploying various different code bases to different applications across a variety of environments on a regular basis. I am trying to improve my work flow by creating a VBA script that grabs the current Build/Version Number of an application.

As code is deployed to a specific server/environment, there is a .txt file that contains something along the lines of:

<Version Number>V5.65.3</Version Number>

I am trying to build a script that accesses this file and grabs the version number and inputs it into the desired cell.

My problem is that the file path contains folder names that are variable.. These are folders in a temp directory that will change each time a new version is deployed to the application. See an example file path below:

\\servername\constantFolder\applicationName\temp\constantFolder\temp\**RANDOMSTRING**\**RANDOMSTRING2**\constantFolder\constantFolder\FileIWant.txt

The issue in essence is that each time a new build is deployed, the two "RandomString" folders are created with names that are not consistent. The rest of the file path is consistent, so how can I retrieve the "FileIWant.txt" with the two folders that may not have a consistent name?

I tried using DIR() function in the following way, but I don't believe that what I am trying to do can be accomplished with DIR() unless I am misunderstanding.

Dir("\\server\constantFolder\applicationName\temp\*\constantFolder\constantFolder\FileIWant.txt")

I have also tried to concatenate within the DIR function, but I receive a Run-Time Error "52": Bad File Name or Number.

Is there a way to retrieve a file in VBA that contains variable folder names? I have searched other questions but found that they are trying to retrieve a file with a wildcard on the file name. Here, I know the file name but will not always know the name of two intermediary folders.


Solution

  • I was able to complete what I wanted by using DIR() directly to find the name of the variable folders' names. Both folder names contained a word (temp & content) prior to the random string of characters which allowed me to concatenate the entire path together.

    See below:

    sub get_version() 
    
    Dim tempFolder As String
    Dim ContentFolder As String
    Dim FullPath As String
    
    tempFolder = Dir("\\servername\staticFolder1\staticFolder2\applicationFolder\staticFolder3\temp*", vbDirectory)
    
    tempContentFolder = Dir("\servername\staticFolder1\staticFolder2\applicationFolder\staticFolder3\" & tempFolder & "\content*", vbDirectory)
    
    FullPath = "\\servername\staticFolder1\staticFolder2\applicationFolder\staticFolder3\" & tempFolder & "\" & tempContentFolder & "\staticFolder4\staticFolder5\FileIWant.txt"
    
    End Sub
    

    Since there is never another folder in that exact directory with the same starting string, it works for my solution. Thank you all for who answered my question!