Checking a string for filename compliance

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

In my NNTP program, i'm using the Message-ID's as the filename (to eliminate
duplicate messages coming in from different groups). My program has been
working fine for months until I received some messages with a forward slash
"/" in the Message-ID.

I specifically used a .Replace function to change the "/" to a "-". But is
there a simple function to replace any non-filename-compliant character with
a dash or other legal character?
 
Here is sample code:
Imports System.IO
Module EnsureValidFileNameModule

Function EnsureValidFileName(ByVal strFullPathName As String) As String
Dim b As Boolean
EnsureValidFileName = strFullPathName
Try
b = Path.IsPathRooted(strFullPathName)
Catch ex As ArgumentException
EnsureValidFileName = ReplaceBadFileNameCharacters(strFullPathName)
End Try
End Function

Function ReplaceBadFileNameCharacters(ByVal strFullPathName As String) As
String
Dim c As Char
Dim s As String = strFullPathName
For Each c In Path.InvalidPathChars
' Replace any invalid character with the letter "X"
s = s.Replace(c, "X")
Next c
ReplaceBadFileNameCharacters = s
End Function

Sub main()
Dim tstGoodFileName As String = "c:\aaa\bbb\ccc\ddd\test.txt"
Dim tstBadFileName As String = "c:\aaa\bbb\c<>cc\ddd\test<>.txt"
MsgBox(EnsureValidFileName(tstGoodFileName) & vbNewLine &
EnsureValidFileName(tstBadFileName))
End Sub

End Module
 
Thanks! That did it. I kind of thought there might be a built-in function
for this.
 
Terry Olsen said:
Thanks! That did it. I kind of thought there might be a built-in function
for this.

Note that

is not complete. So the function is not able to guarantee that the path is
valid and/or that it can exist.
 
Here is a better version:

Imports System.IO
Module EnsureValidFileNameModule

Function ReplaceBadFileNameCharacters(ByVal strFullPathName As String) As
String
'
'Replaces bad characters with "X"
'
Dim c As Char
Dim s As String = strFullPathName
Dim strFileName As String
Dim strPathName As String
Dim strPathRoot As String
Const FILENAMEBADCHARACTERS As String = "*?:/\"
Const PATHNAMEBADCHARACTERS As String = "*?"
'
' Change Path.InvalidPathChars
' " < > |
'
For Each c In Path.InvalidPathChars
s = s.Replace(c, "X")
Next c
'
strFileName = Path.GetFileName(s)
strPathName = Path.GetDirectoryName(s)
s = Nothing
'
' Change characters that are invalid in filenames
'
For Each c In FILENAMEBADCHARACTERS
strFileName = strFileName.Replace(c, "X")
Next
'
' Change : character (except in column 2)
'
If strPathName.Length > 0 Then
If strPathName.Length >= 2 AndAlso strPathName.Substring(0, 2) = "\\"
Then
strPathName = strPathName.Replace(":", "X")
ElseIf strPathName.Length >= 2 AndAlso strPathName.Substring(1, 1) = ":"
Then
strPathName = strPathName.Substring(0, 2) & _
strPathName.Substring(2).Replace(":", "X")
End If
End If
'
' Change characters that are invalid in paths
'
For Each c In PATHNAMEBADCHARACTERS
strPathName = strPathName.Replace(c, "X")
Next
ReplaceBadFileNameCharacters = String.Concat(strPathName, _
DirectCast(IIf(strPathName.Length > 0 AndAlso Not
strPathName.EndsWith("\"), "\", ""), String), _
strFileName)
End Function

Sub Main()
Dim tstBadFileName1 As String = "C:\?/\/\/\/\?test.txt"
Dim tstBadFileName2 As String = "C:\xxx\x:x\xxx\:test.txt"
Dim tstBadFileName3 As String = "\\x/x\x:x\x*x\:test.txt" 'UNC format
MsgBox(ReplaceBadFileNameCharacters(tstBadFileName1) & vbNewLine & _
ReplaceBadFileNameCharacters(tstBadFileName2) & vbNewLine & _
ReplaceBadFileNameCharacters(tstBadFileName3))
End Sub

End Module
 

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

Back
Top