VB.Net - 使用正则表达式过滤文件名

发布于 2024-12-20 05:21:08 字数 727 浏览 3 评论 0原文

我认为我把简单的事情变得过于复杂,但我正在寻找 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生来就爱笑 2024-12-27 05:21:08

你不能这样做吗?

  Dim input As String = "ThisIsAnExample.s4"
  Dim pattern As String = "\.s4$"
  Dim replacement As String = ".S4"
  Dim rgx As New Regex(pattern)
  Dim result As String = rgx.Replace(input, replacement)

  Console.WriteLine("Original String: {0}", input)
  Console.WriteLine("Replacement String: {0}", result)    

Can't you do this?

  Dim input As String = "ThisIsAnExample.s4"
  Dim pattern As String = "\.s4$"
  Dim replacement As String = ".S4"
  Dim rgx As New Regex(pattern)
  Dim result As String = rgx.Replace(input, replacement)

  Console.WriteLine("Original String: {0}", input)
  Console.WriteLine("Replacement String: {0}", result)    
谈情不如逗狗 2024-12-27 05:21:08

如果您想使用不带正则表达式的 C# 来执行此操作,您可以使用以下内容。

    private void UppercaseExtension(string filepath)
    {
        //ensure the source file exists
        if (!File.Exists(filepath))
            return;

        //get the parts of the filepath
        string filename = Path.GetFileNameWithoutExtension(filepath);
        string extension = Path.GetExtension(filepath).ToUpperInvariant();
        string folderPath = Path.GetDirectoryName(filepath);
        try
        {
            //rename the file using the uppercase extension
            File.Move(filepath, Path.Combine(folderPath, filename + extension));
        }
        catch (Exception ex)
        {
            //failed to rename
        }
    }

如果您想限制文件类型,您可以在方法内添加一个检查来执行此操作,或者您可以限制传递给它的文件路径。

If you wanted to do this using c# without regex you could use the following

    private void UppercaseExtension(string filepath)
    {
        //ensure the source file exists
        if (!File.Exists(filepath))
            return;

        //get the parts of the filepath
        string filename = Path.GetFileNameWithoutExtension(filepath);
        string extension = Path.GetExtension(filepath).ToUpperInvariant();
        string folderPath = Path.GetDirectoryName(filepath);
        try
        {
            //rename the file using the uppercase extension
            File.Move(filepath, Path.Combine(folderPath, filename + extension));
        }
        catch (Exception ex)
        {
            //failed to rename
        }
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文