VB.Net - 使用正则表达式过滤文件名
我认为我把简单的事情变得过于复杂,但我正在寻找 VB.Net 代码来“过滤”文件名。
设想: 我的公司在服务器上有一个文件夹,其中包含超过 65,000 个文件。读取这些文件的新计算机区分大小写,并且仅接受“*.S4”文件扩展名。
因此,我需要将所有文件名转换为“*.S4”,但我希望可以选择用我指定的模式替换每个文件。
例如:
查找> test.s4
替换> test_1.S4
使用模式:
查找> *.s4
替换> *_1.S4
这是我到目前为止的代码(不起作用):
'Inputs:
Dim Filename As String = "ThisIsAnExample.s4"
Dim Find As String = "*.s4"
Dim Replace As String = "*.S4"
Find = Find.Replace("*", "(.*)")
Replace = Replace.Replace("*", "(.*)")
Dim rgxExp As New System.Text.RegularExpressions.Regex(Find)
MsgBox(rgxExp.Replace(Filename, Replace))
我知道它是可能的,我曾经用 Javascript 编写过一个类似的脚本。
I think im overcomplicating something simple but im looking for a VB.Net code to "filter" file names.
Scenario:
My company has a folder on the server with over 65,000 files on it. The new machine that reads those files is case sensitive and only accepts "*.S4" file extensions.
So, I need to convert all the file names to "*.S4" but I'd like the option to replace each file with a pattern I Specify.
For example:
Find > test.s4
Replace > test_1.S4
Using Patterns:
Find > *.s4
Replace > *_1.S4
Heres the code I have so far (doesnt work):
'Inputs:
Dim Filename As String = "ThisIsAnExample.s4"
Dim Find As String = "*.s4"
Dim Replace As String = "*.S4"
Find = Find.Replace("*", "(.*)")
Replace = Replace.Replace("*", "(.*)")
Dim rgxExp As New System.Text.RegularExpressions.Regex(Find)
MsgBox(rgxExp.Replace(Filename, Replace))
I know its possible, I wrote a similar script in Javascript once.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能这样做吗?
Can't you do this?
如果您想使用不带正则表达式的 C# 来执行此操作,您可以使用以下内容。
如果您想限制文件类型,您可以在方法内添加一个检查来执行此操作,或者您可以限制传递给它的文件路径。
If you wanted to do this using c# without regex you could use the following
If you wanted to limit the types of file you could add a check to do this inside the method or you could limit the file paths being passed to it.