[英]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.我有 VBA 代码来将文件名捕获到 MS Access 数据库中的表中。
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.每个文件名都有 4 个_
下划线,文件名的长度各不相同。
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.在本例中,我将文件名的第 5 部分移到前面,并添加文件扩展名。
For example, WC1603992365_Michael_Cert_03-19-2019_858680723.csv
would be saved as 858680723.csv
in the folder.例如, WC1603992365_Michael_Cert_03-19-2019_858680723.csv
将在文件夹中另存为858680723.csv
。
Is there a way that I can use RegEx to capture 3rd section of the file name, and save the value in a field?有没有办法可以使用 RegEx 捕获文件名的第三部分,并将值保存在字段中?
I tried VBA code, and searched SQL examples, but I did not find any.我尝试了 VBA 代码,并搜索了 SQL 示例,但没有找到。
Because the file name length is not fixed, I cannot use LEFT
or RIGHT
...因为文件名长度不固定,所以不能使用LEFT
或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.一种可能的解决方案是使用 VBA Split
function 将字符串拆分为使用下划线作为分隔符的字符串数组,然后返回该数组中索引 2 处的项目。
For example, you could define a VBA function such as the following, residing in a public module:例如,您可以定义一个 VBA function,如下所示,驻留在公共模块中:
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.在这里,我将参数strStr
定义为 Variant,以便您可以将Null
值传递给它而不会出错。
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.如果提供了Null
值,或者如果提供的索引超出了使用下划线拆分字符串返回的数组的边界,则 function 将返回一个空字符串。
You can then call the above function from a SQL statement:然后,您可以从 SQL 语句中调用上述 function :
select StringElement(t.Filename, 2) from Filenames t
Here I have assumed that your table is called Filenames
- change this to suit.在这里,我假设您的表名为Filenames
- 将其更改为适合。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.