简体   繁体   English

如何格式化文件名中第二个和第四个下划线之间的值?

[英]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 ...因为文件名长度不固定,所以不能使用LEFTRIGHT ...

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.

相关问题 我需要 oracle 正则表达式将第 2 到第 4 个 position 个字符替换为符号 - I need oracle regex to replace 2nd to 4th position characters to symbol REGEXP_EXTRACT 值从左边第 4 到第 5 个下划线之间 - REGEXP_EXTRACT value from left between 4th and 5th underscore 如何在 SQL Server 中获取第 2 个和第 4 个星期六的日期列表? - How to get list of 2nd and 4th Saturday dates in SQL Server? 如何在Oracle表的varchar列中的第2个和第4个char之后插入“/” - How to Insert '/' after 2nd and 4th char in varchar column of an Oracle table 在一行中查找第 2、3、4、5 个非 NULL 值(SQL) - finding 2nd, 3rd, 4th, 5th not NULL values in a row (SQL) 使用ROW_NUMBER()将第1个ARRAY与第2个,第3个,第4个等进行比较 - Using ROW_NUMBER () to Compare 1st ARRAY to 2nd, 3rd, 4th, etc 根据特定材料,从同一列中选择第二,第四行等 - Select every 2nd, 4th row and so on from same column based on specific material 每月第 2 和第 4 个星期四运行的重复 SQL 作业 - Reoccurring SQL job to run on the 2nd and 4th Thursday of every month 如何从第一列中删除 Null 值但保留第二列和第三列的值 - How can I remove Null value from first column but keep the value of the 2nd and thirds columns 如何使用嵌套 CASE 语句检查 3 列中的值以设置 Microsoft SQL 中第 4 列中的值? - How can I use a nested CASE statement to check the values in 3 columns to set the value in a 4th column in Microsoft SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM