Tom, Nikolay:
That code doesn't work, at least not in VS2005. What happens is that
when you replace with VBNullChar, it basically chops off the string
from that point onwards. So Sna?*|fu" would become Sna instead of
Snafu. If you use two double quotes, then it works as you expect.
Here is corrected code:
strNewFileName = InputBox("Enter the new base file name: ")
' Check for invalid filename characters.
For Each invalidChar As Char In
System.IO.Path. GetInvalidFileN ameChars
strNewFileName = strNewFileName .Replace(invali dChar,
"")
Next
Note that there is also a System.IO.Path. GetInvalidPathC hars function.
Not really sure what the difference is but I guess there must be some
characters that are allowed in the filename but not the folder name, or
vice-versa. Also, System.IO.Path. InvalidPathChar s has been replaced
by the two functions I mentioned above - you get an error if you try to
use System.IO.Path. InvalidPathChar s in VS2005.
=============== =============== =============== =============== =======
From: Tom Shelton
Date: Thurs, Feb 10 2005 2:39 am
Email: Tom Shelton <tshel...@YOUKN OWTHEDRILLcomca st.net>
Groups: microsoft.publi c.dotnet.langua ges.vb
On 2005-02-09, Nikolay Petrov <johntup2_nosp. ..@mail.bg> wrote:
[color=blue]
> I need a way to strip chars from a string. The chars are all chars that are
> not allowed in file path.[/color]
[color=blue]
> TIA[/color]
What ever way you do it - I would make sure that you get the list of
illegal chars from the System.IO.Path class's InvalidPathChar s
property.
One way, would be to build a regular expression (as Herfried
suggested).
Or you could do it something like:
For Each invalidChar As Char In System.IO.Path. InvalidPathChar s
thePathString = thePathString.R eplace (invalidChar, vbNullChar)
Next
HTH
--
Tom Shelton [MVP]
That code doesn't work, at least not in VS2005. What happens is that
when you replace with VBNullChar, it basically chops off the string
from that point onwards. So Sna?*|fu" would become Sna instead of
Snafu. If you use two double quotes, then it works as you expect.
Here is corrected code:
strNewFileName = InputBox("Enter the new base file name: ")
' Check for invalid filename characters.
For Each invalidChar As Char In
System.IO.Path. GetInvalidFileN ameChars
strNewFileName = strNewFileName .Replace(invali dChar,
"")
Next
Note that there is also a System.IO.Path. GetInvalidPathC hars function.
Not really sure what the difference is but I guess there must be some
characters that are allowed in the filename but not the folder name, or
vice-versa. Also, System.IO.Path. InvalidPathChar s has been replaced
by the two functions I mentioned above - you get an error if you try to
use System.IO.Path. InvalidPathChar s in VS2005.
=============== =============== =============== =============== =======
From: Tom Shelton
Date: Thurs, Feb 10 2005 2:39 am
Email: Tom Shelton <tshel...@YOUKN OWTHEDRILLcomca st.net>
Groups: microsoft.publi c.dotnet.langua ges.vb
On 2005-02-09, Nikolay Petrov <johntup2_nosp. ..@mail.bg> wrote:
[color=blue]
> I need a way to strip chars from a string. The chars are all chars that are
> not allowed in file path.[/color]
[color=blue]
> TIA[/color]
What ever way you do it - I would make sure that you get the list of
illegal chars from the System.IO.Path class's InvalidPathChar s
property.
One way, would be to build a regular expression (as Herfried
suggested).
Or you could do it something like:
For Each invalidChar As Char In System.IO.Path. InvalidPathChar s
thePathString = thePathString.R eplace (invalidChar, vbNullChar)
Next
HTH
--
Tom Shelton [MVP]
Comment