0

There is code below that I used to change ".." to ".". For example I have a name of file like "file..pdf" and I would like to have "file.pdf", but It removes all dots. I don't know how to change it:

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
shA.t
  • 16,580
  • 5
  • 54
  • 111
user3114375
  • 97
  • 3
  • 12

2 Answers2

0

Because you go through all character in your file name:

For i = 1 To Len(strIllegals)

Therefore it will remove all "." in your file name.

Just simple use:

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

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

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

End Function
dtk142
  • 51
  • 2
  • 10
0

If you want to change some special characters to some other characters I suggest you to use this function:

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

For more illegal you can change strIllegals = "..@=>@." to something like strIllegals = "..@=>@.@|@&@=>@ AND " that will change & to AND.

shA.t
  • 16,580
  • 5
  • 54
  • 111