Remove invalid characters from path

G

Guest

I am working on a macro that saves messages to a folder if the subject
contains certain text. This becomes a problem with RE:, FW:, and any special
characters like /. What could I add to my code that would check for invalid
characters and replace them with "-"?
Here is a small portion of my code:

Dim Item As Object
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
For Each Item In Inbox.Items
If InStr(Item.subject, "test") > 0 Then
Item.SaveAs ("c:\mailtest\" & Item.subject) & ".msg", 3
End If
 
M

Michael Bauer

Am Mon, 9 Jan 2006 14:32:03 -0800 schrieb Daniel Sommerfeld:

You can use this function:

Private Sub ReplaceCharsForFileName(sText As String, sReplace As String)
sText = Replace(sText, "/", sReplace)
sText = Replace(sText, "\", sReplace)
sText = Replace(sText, ":", sReplace)
sText = Replace(sText, "?", sReplace)
sText = Replace(sText, Chr(34), sReplace)
sText = Replace(sText, "<", sReplace)
sText = Replace(sText, ">", sReplace)
sText = Replace(sText, "|", sReplace)
End Sub
 
G

Guest

I also found this after a few more days or looking, although it would need
some touch-up work:

Function ValidateFilename(strFilename)
Dim strValidChars
'# Enter all valid characters here.
strValidChars = "^&'@{}[],$=!-#()%.+~_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim strInvalid
Dim i,char
For i = 1 To Len(strFilename)
char = UCase(Mid(strFilename, i, 1))
If InStr(strValidChars, char) = 0 Then
strInvalid = strInvalid & ", " & char
End If
Next

If strInvalid <> "" Then
MsgBox "The following characters were invalid in your filename:" &
vbCrlf & vbCrLf & strInvalid
ValidateFilename = False
Else
ValidateFilename = True
End If

End Function
 
M

Michael Bauer

Am Wed, 11 Jan 2006 14:29:03 -0800 schrieb Daniel Sommerfeld:

Not important for file names, which are very short - but a nice excercise:
The reversed search should be faster:

Sub SearchInvalidChars(YourText As String)
Dim aInvalid As Variant: aInvalid = Array("/", "\") ' etc.
Dim ubnd&: ubnd = UBound(aInvalid)
Dim msg$: msg = String((ubnd + 1) * 3, " ")
Dim char$
Dim i&
Dim pos&: pos = 1
For i = 0 To UBound(aInvalid)
char = aInvalid(i)
If InStr(1, YourText, char, vbTextCompare) Then
Mid(msg, pos, 3) = char & ", "
pos = pos + 3
End If
Next
If pos = 1 Then msg = vbNullString Else msg = Mid(msg, 1, pos - 3)
End Sub


--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook


I also found this after a few more days or looking, although it would need
some touch-up work:

Function ValidateFilename(strFilename)
Dim strValidChars
'# Enter all valid characters here.
strValidChars = "^&'@{}[],$=!-#()%.+~_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim strInvalid
Dim i,char
For i = 1 To Len(strFilename)
char = UCase(Mid(strFilename, i, 1))
If InStr(strValidChars, char) = 0 Then
strInvalid = strInvalid & ", " & char
End If
Next

If strInvalid <> "" Then
MsgBox "The following characters were invalid in your filename:" &
vbCrlf & vbCrLf & strInvalid
ValidateFilename = False
Else
ValidateFilename = True
End If

End Function

Daniel Sommerfeld said:
I am working on a macro that saves messages to a folder if the subject
contains certain text. This becomes a problem with RE:, FW:, and any special
characters like /. What could I add to my code that would check for invalid
characters and replace them with "-"?
Here is a small portion of my code:

Dim Item As Object
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
For Each Item In Inbox.Items
If InStr(Item.subject, "test") > 0 Then
Item.SaveAs ("c:\mailtest\" & Item.subject) & ".msg", 3
End If
 
G

Guest

Function Sanitized(text As String, illegal As String) As String
Dim Ch As String ' individual illegal character to be removed

For i = 1 To Len(illegal)
Ch = Mid(illegal, i, 1)
' remove illegal characters such as :/\,?<>*| and .
While InStr(text, Ch) > 0
text = Left(text, InStr(text, Ch) - 1) & Right(text, Len(text) -
InStr(text, Ch))
Wend
Next
Sanitized = text
End Function

Michael Bauer said:
Am Wed, 11 Jan 2006 14:29:03 -0800 schrieb Daniel Sommerfeld:

Not important for file names, which are very short - but a nice excercise:
The reversed search should be faster:

Sub SearchInvalidChars(YourText As String)
Dim aInvalid As Variant: aInvalid = Array("/", "\") ' etc.
Dim ubnd&: ubnd = UBound(aInvalid)
Dim msg$: msg = String((ubnd + 1) * 3, " ")
Dim char$
Dim i&
Dim pos&: pos = 1
For i = 0 To UBound(aInvalid)
char = aInvalid(i)
If InStr(1, YourText, char, vbTextCompare) Then
Mid(msg, pos, 3) = char & ", "
pos = pos + 3
End If
Next
If pos = 1 Then msg = vbNullString Else msg = Mid(msg, 1, pos - 3)
End Sub


--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook


I also found this after a few more days or looking, although it would need
some touch-up work:

Function ValidateFilename(strFilename)
Dim strValidChars
'# Enter all valid characters here.
strValidChars = "^&'@{}[],$=!-#()%.+~_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim strInvalid
Dim i,char
For i = 1 To Len(strFilename)
char = UCase(Mid(strFilename, i, 1))
If InStr(strValidChars, char) = 0 Then
strInvalid = strInvalid & ", " & char
End If
Next

If strInvalid <> "" Then
MsgBox "The following characters were invalid in your filename:" &
vbCrlf & vbCrLf & strInvalid
ValidateFilename = False
Else
ValidateFilename = True
End If

End Function

Daniel Sommerfeld said:
I am working on a macro that saves messages to a folder if the subject
contains certain text. This becomes a problem with RE:, FW:, and any special
characters like /. What could I add to my code that would check for invalid
characters and replace them with "-"?
Here is a small portion of my code:

Dim Item As Object
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
For Each Item In Inbox.Items
If InStr(Item.subject, "test") > 0 Then
Item.SaveAs ("c:\mailtest\" & Item.subject) & ".msg", 3
End If
 
G

Guest

David C said:
Function Sanitized(text As String, illegal As String, replacement as string) As String
Dim Ch As String ' individual illegal character to be removed

For i = 1 To Len(illegal)
Ch = Mid(illegal, i, 1)
' remove illegal characters such as :/\,?<>*| and .
While InStr(text, Ch) > 0
text = Left(text, InStr(text, Ch) - 1) & replacement & Right(text, Len(text) -
InStr(text, Ch))
Wend
Next
Sanitized = text
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top