Hopefully a simple question (searching MSDN came up with some clear answers but only for Windows Forms and the .NET class framework, neither of which I'm using, just the plain C Win32 API here)
How do I find out if a string is a valid filename? (i.e. it doesn't contain bad characters and has a proper length)
Thanks in advance!
The length is fairly easy. Looking for invalid characters is much harder, as many characters are valid in certain places, but not in other places. Of course, the OS will check that the filename IS valid before it opens the file, so opening a file for reading will automatically check if it is valid. The value in GetLastError() or errno will be different depending on the cause of failure, so you can tell if the file didn't exist but has a valid name, or if the filename itself is invalid (e.g contains invalid characters, is too long). It may be harder to tell the difference between an invalid path and a file that doesn't exist - do you need to do this?
The path isn't an issue as I'm only saving files in the program's working folder.
Problem is I want to be able to check whether it's a valid filename long before the file is actually created.
Yes, so try to open the file for reading - if that succeeds, close it, if it doesn't succeed, then check the "error code" to see if it failed due to the file missing (ENOENT), the the file isn't there (but implied that it's a valid name). If it's some other error, then it's not there.
Originally Posted by Boksha
You could have a look at PathCleanupSpec, but you can only be sure you can create a file by creating it (you may not have permission, a network drive may be offline, etc).