简体   繁体   中英

How can I format value between 2nd and 4th underscore in the file name?

I have VBA code to capture filenames to a table in an MS Access Database.

The values look like this:

FileName
----------------------------------------------------    
WC1603992365_Michael_Cert_03-19-2019_858680723.csv
WC1603992365_John_Non-Cert_03-19-2019_858680722.csv
WC1703611403_Paul_Cert_03-27-2019_858679288.csv

Each filename has 4 _ underscores and the length of the filename varies.

I want to capture the value between the 2nd and the 3rd underscore, eg:

Cert
Non-Cert
Cert

I have another file downloading program, and it has "renaming" feature with a regular expression. And I set up the following:

Source file Name: (.*)\_(.*)\_(.*)\_(.*)\_\-(.*)\.(.*)
New File Name: \5.\6

In this example, I move the 5th section of the file name to the front, and add the file extension.

For example, WC1603992365_Michael_Cert_03-19-2019_858680723.csv would be saved as 858680723.csv in the folder.

Is there a way that I can use RegEx to capture 3rd section of the file name, and save the value in a field?

I tried VBA code, and searched SQL examples, but I did not find any.

Because the file name length is not fixed, I cannot use LEFT or RIGHT ...

Thank you in advance.

One possible solution is to use the VBA Split function to split the string into an array of strings using the underscore as a delimiter, and then return the item at index 2 in this array.

For example, you could define a VBA function such as the following, residing in a public module:

Function StringElement(strStr, intIdx As Integer) As String
    Dim strArr() As String
    strArr = Split(Nz(strStr, ""), "_")
    If intIdx <= UBound(strArr) Then StringElement = strArr(intIdx)
End Function

Here, I've defined the argument strStr as a Variant so that you may pass it Null values without error.

If supplied with a Null value or if the supplied index exceeds the bounds of the array returned by splitting the string using an underscore, the function will return an empty string.

You can then call the above function from a SQL statement:

select StringElement(t.Filename, 2) from Filenames t

Here I have assumed that your table is called Filenames - change this to suit.

This is the working code that I completed. Thank you for sharing your answers.

Public Function getSourceFiles()
Dim rs As Recordset
Dim strFile As String
Dim strPath As String
Dim newFileName As String
Dim FirstFileName As String
Dim newPathFileName As String
Dim RecSeq1 As Integer
Dim RecSeq2 As Integer
Dim FileName2 As String
Dim WrdArrat() As String

RecSeq1 = 0


Set rs = CurrentDb.OpenRecordset("tcsvFileNames", dbOpenDynaset) 'open a recordset

strPath = "c:\in\RegEx\"

strFile = Dir(strPath, vbNormal)


Do 'Loop through the balance of files

    RecSeq1 = RecSeq1 + 1

    If strFile = "" Then 'If no file, exit function
        GoTo ExitHere
    End If


    FirstFileName = strPath & strFile
    newFileName = strFile
    newPathFileName = strPath & newFileName
    FileName2 = strFile

Dim SubStrings() As String
SubStrings = Split(FileName2, "_")
Debug.Print SubStrings(2)

    rs.AddNew
    rs!FileName = strFile
    rs!FileName68 = newFileName 'assign new files name max 68 characters
    rs!Decision = SubStrings(2) 'extract the value after the 3rd underscore, and add it to Decision Field
    rs.Update
    Name FirstFileName As newPathFileName

    strFile = Dir()


Loop

ExitHere:
Set rs = Nothing
MsgBox ("Directory list is complete.")

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM