0

下面有我用来将“..”更改为“.”的代码。例如,我有一个文件名,如“file..pdf”,我想要“file.pdf”,但它会删除所有点。我不知道如何改变它:

Function strLegalFileName2(ByVal FName) As String
Dim i As Integer

Const strIllegals = "*&..&*"
strLegalFileName2 = FName
For i = 1 To Len(strIllegals)
    strLegalFileName2 = Replace(strLegalFileName2, Mid$(strIllegals, i, 1), ".")
Next i
End Function

Sub LoopThroughFiles2()
Dim FName As Variant
Dim strNew As String
Dim strDir As String

strDir = "path"
FName = Dir(strDir & "*..*")
Do While Len(FName) > 0
    strNew = strLegalFileName2(FName)
        If StrComp(strNew, FName) <> 0 Then Name (strDir & FName) As (strDir & strNew)
    FName = Dir
Loop
End Sub
4

2 回答 2

0

因为您遍历了文件名中的所有字符:

For i = 1 To Len(strIllegals)

因此它将删除所有“。” 在您的文件名中。

只是简单的使用:

Function strLegalFileName2(ByVal FName) As String
Dim i As Integer

Const strIllegals = "*&..&*"
strLegalFileName2 = FName

strLegalFileName2 = Replace(strLegalFileName2, "..", ".")

End Function
于 2015-05-19T09:23:30.013 回答
0

如果您想将一些特殊字符更改为其他一些字符,我建议您使用此功能:

Function strLegalFileName2(ByVal FName) As String
Dim strIllegal() As String
Dim i As Integer

Const strIllegals = "..@=>@."

strIllegal = Split(strIllegals, "@|@")

For i = LBound(strIllegal) To UBound(strIllegal)
    FName = Replace(FName, Mid(strIllegal(i), 1, InStr(1, strIllegal(i), "@=>@") - 1), Mid(strIllegal(i), InStr(1, strIllegal(i), "@=>@") + 4))
Next i

strLegalFileName2 = FName
End Function

对于更非法的,您可以更改strIllegals = "..@=>@."为类似strIllegals = "..@=>@.@|@&@=>@ AND "将更&改为AND.

于 2015-05-19T10:46:16.090 回答