-
1
- #1
Mike Lewis
Programmer
Some years ago, I wrote a little function that strips invalid characters from filenames. Why would you need to do that? You might need the user to specify the name of an output file. Or you might want to generate a filename from some other string, such as a customer or product name. In both those cases, you have to be sure that the name doesn't contain question marks, colons, backslashes, or other characters that aren't allowed in filenames.
So here is my function. As you can see, it simply replaces each instance of an invalid character with an underscore.
Most of the time, this works well enough. But it's not perfect. One issue is that it doesn't distinguish plain filenames (stem plus extension) from full path designations (including drive and/or directories). It would be easy to modify it to retain colons and backslashes, but it would be harder to deal with those characters in the "wrong" place (e.g. more than one consecutive colon or backslash). There are a few other similar minor issues.
Then I discovered what looks like a much easier solution: a FoxTools function named CleanPath() which appears to do just what its name suggests.
To use it, you must first open the FoxTools library, like this:
[tt]
SET LIBRARY TO (HOME(1) + "foxtools")[/tt]
after which you can call CleanPath() just like any other function. You pass it the "raw" input string, and it returns the cleaned-up version. In this case, the invalid characters are completely removed (in my function, they are replaced by undescores).
However, this too is not perfect. Its biggest problem is that it doesn't recognise embedded spaces in filenames (probably because it was written in MS_DOS days). If a filename contains spaces, it simply removes them. On the other hand, it does seem to handle drives and paths correctly - including, for example, removing duplicate backslashes - at least in most cases.
I hope the above information will be useful for anyone who has this requirement. To help you decide between two methods, here are the results of a quick comparison test of the two functions:
I'd welcome your comments or suggestions re the above.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads
So here is my function. As you can see, it simply replaces each instance of an invalid character with an underscore.
Code:
FUNCTION StripInvalidChars
* Removes all invalid characters from a filename (excluding extension)
* and replaces them with underscores.
LPARAMETERS tcIn
LOCAL lcBadChars
lcBadChars = [<>:"/\|?*]
RETURN CHRTRAN(tcIn, lcBadChars, REPLICATE("_", LEN(lcBadChars)))
Most of the time, this works well enough. But it's not perfect. One issue is that it doesn't distinguish plain filenames (stem plus extension) from full path designations (including drive and/or directories). It would be easy to modify it to retain colons and backslashes, but it would be harder to deal with those characters in the "wrong" place (e.g. more than one consecutive colon or backslash). There are a few other similar minor issues.
Then I discovered what looks like a much easier solution: a FoxTools function named CleanPath() which appears to do just what its name suggests.
To use it, you must first open the FoxTools library, like this:
[tt]
SET LIBRARY TO (HOME(1) + "foxtools")[/tt]
after which you can call CleanPath() just like any other function. You pass it the "raw" input string, and it returns the cleaned-up version. In this case, the invalid characters are completely removed (in my function, they are replaced by undescores).
However, this too is not perfect. Its biggest problem is that it doesn't recognise embedded spaces in filenames (probably because it was written in MS_DOS days). If a filename contains spaces, it simply removes them. On the other hand, it does seem to handle drives and paths correctly - including, for example, removing duplicate backslashes - at least in most cases.
I hope the above information will be useful for anyone who has this requirement. To help you decide between two methods, here are the results of a quick comparison test of the two functions:
Code:
Input Own function CleanPath() Comment (CP = CleanPath)
abc.dbf abc.dbf ABC.DBF As expected
abc def.dbf abc def.dbf ABCDEF.DBF CP removes space
abc?def.dbf abc_def.dbf ABCDEF.DBF Both correctly remove ?
c:\abc.dbf c__abc.dbf C:\ABC.DBF CP handles full path OK
c:\data\abc.dbf c__data_abc.dbf C:\DATA\ABC.DBF ditto
c:\data\\abc.dbf c__data__abc.dbf C:\DATA\ABC.DBF CP removes double backslash
c::\\abc.dbf c____abc.dbf ABC.DBF CP loses (invalid) drive
(empty string) (empty string) (empty string) As expected
?//*| _____ (5 underscrs)(empty string)
I'd welcome your comments or suggestions re the above.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads