如果存在,删除文件名末尾的特定符号和数字

时间:2015-09-24 12:11:09

标签: vb.net

我的应用程序正在从网络下载许多不同的文件。某些文件可能包含括号内的附加数字,如下所示:

 report78-12-34-34_ex                                  'nothing to be removed 
 blabla3424dm_d334(7)                                  '(7) - to be removed
 erer3r3r3_2015_03_03-1945-user-_d334(31).xml          '(31) - to be removed 
 group78-12-34-34_ex.html                              'nothing to be removed
 somereport5_6456                                       'nothing to be removed

如果您看到(数字)是否出现在文件名中,则必须将其删除。你有一些很好的安全方法可以完成这项工作吗?

我从rakesh获得了一些代码,但是当字符串不包含(数字)时它不起作用:

string test="something(3)";
test=Regex.Replace(test, @"\d", "").Replace("()","");

例如:

时无效

如果我放置这样的文件:UIPArt3MilaGroupUIAPO34mev1-mihe-2015_9_23-21_30_5_580.csv,则会显示:UIPArtMilaGroupUIAPOmev-mihe--_.csv

我宁愿不使用正则表达式。

3 个答案:

答案 0 :(得分:1)

避免使用正则表达式并检查括号内的字符串,只有在包含的字符串是数字时才删除子字符串。

Private Function NewFileName(ByVal FileName As String) As String
    If FileName Like "*(*)*" Then
        Try
            Dim SubStrings() As String = Split(FileName, "(", 2)
            NewFileName = SubStrings(0)
            SubStrings = Split(SubStrings(1), ")", 2)
            SubStrings(0) = NewFileName(SubStrings(0))
            SubStrings(1) = NewFileName(SubStrings(1))
            If IsNumeric(SubStrings(0)) Then
                NewFileName &= SubStrings(1)
            Else
                Return FileName
            End If
        Catch
            Return FileName
        End Try
    Else
        Return FileName
    End If
End Sub

答案 1 :(得分:1)

我会做这样的事情:

Public Function GetFileName(ByVal fileName As String) As String

    Dim lastOpenBracketPos As Integer = fileName.LastIndexOf("(")
    Dim lastCloseBracketPos As Integer = fileName.LastIndexOf(")")
    If lastOpenBracketPos <> -1 AndAlso lastCloseBracketPos <> -1 AndAlso lastCloseBracketPos > lastOpenBracketPos Then
        Dim bracketsText As String = fileName.Substring(lastOpenBracketPos, lastCloseBracketPos-lastOpenBracketPos+1)
        If IsNumeric(bracketsText.Trim("(",")")) Then
            Return fileName.Replace(bracketsText,"")
        End If
    End If
    Return fileName

End Function

答案 2 :(得分:0)

在这里的所有代码中我都制作了自己的代码,因为必须确保在每次播放文件名之前必须先检查文件名中有多少括号 - 只有1表示打开而1表示关闭括号才有去检查。您认为有什么问题我不知道或者可以调整的东西是什么?

Private Function DeleteBrackets(ByVal fn As String) As String

        Dim countOpenBracket As Integer = fn.Split("(").Length - 1
        Dim countCloseBracket As Integer = fn.Split(")").Length - 1

        '-- If only one occurence of ( and one occurence of )
        If countOpenBracket = 1 And countCloseBracket = 1 Then

            Dim filextension = IO.Path.GetExtension(fn)
            Dim filewithoutExtension As String = IO.Path.GetFileNameWithoutExtension(fn)

            'Debug.Print("Oryginal file name = " & fn)
            'Debug.Print("File name without extension = " & filewithoutExtension)
            'Debug.Print("Extension = " & IO.Path.GetExtension(fn))

            If filewithoutExtension.EndsWith(")") Then
                fn = filewithoutExtension.Remove(filewithoutExtension.LastIndexOf("("))
                'Debug.Print("After removing last index of ( = " & fn)
                'Debug.Print("Adding again extension = " & fn & filextension)
            End If

            'Debug.Print(fn)
        End If

        Return fn

    End Function