下面的代码我曾经将“..”更改为“。”。例如,我有一个文件的名称,如“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
答案 0 :(得分:0)
因为您浏览了文件名中的所有字符:
For i = 1 To Len(strIllegals)
因此它将删除所有&#34;。&#34;在你的文件名中。
只需简单使用:
Function strLegalFileName2(ByVal FName) As String
Dim i As Integer
Const strIllegals = "*&..&*"
strLegalFileName2 = FName
strLegalFileName2 = Replace(strLegalFileName2, "..", ".")
End Function
答案 1 :(得分: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
。