I\'ve got a routine that converts a file into a different format and saves it. The original datafiles were numbered, but my routine gives the output a filename based on an
Regarding the question whether there is any API function to sanitize a file a name (or even check for its validity) - there seems to be none. Quoting from the comment on the PathSearchAndQualify() function:
There does not appear to be any Windows API that will validate a path entered by the user; this is left as an an ad hoc exercise for each application.
So you can only consult the rules for file name validity from File Names, Paths, and Namespaces (Windows):
Use almost any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:
Do not use the following reserved device names for the name of a file: CON, PRN, AUX, NUL, COM1..COM9, LPT1..LPT9.
Also avoid these names followed immediately by an extension; for example, NUL.txt is not recommended.
If you know that your program will only ever write to NTFS file systems you can probably be sure that there are no other characters that the file system does not allow, so you would only have to check that the file name is not too long (use the MAX_PATH constant) after all invalid chars have been removed (or replaced by underscores, for example).
A program should also make sure that the file name sanitizing has not lead to file name conflicts and it silently overwrites other files which ended up with the same name.