I wanna check if my string is valid windows file path. I was searching around and it seems that there is no reliable method to do that. Also I checked boost filesystem library , and no obvious function exist to do this check , maybe something like is_valid_windows_name
Supported characters for a file name are letters, numbers, spaces, and ( ) _ - , . *Please note file names should be limited to 100 characters.
Don't start or end your filename with a space, period, hyphen, or underline. Keep your filenames to a reasonable length and be sure they are under 31 characters. Most operating systems are case sensitive; always use lowercase. Avoid using spaces and underscores; use a hyphen instead.
If you have tried to use an unusual character in the file name (as listed in the Cause section of this article), remove it from the file name and then try saving the file. If the file you are trying to open has an invalid character, rename the file and then try to open it again.
'FileName is not valid': while creating and saving, or opening an Excel file such error may mean one of the following reason: Path of file including file name may exceed 218 number of characters limit. The file name may be incorrect. The path may not be appropriate.
You could use _splitpath()
function and parse the output (based on it, you could easily say if your path is valid or not).
See MSDN for additional information.
Note that this function is windows-specific.
I do not believe there is a standard c++ api for that.
Note that the Windows API allows more filenames than the Windows Shell (The filenames the user is allowed to use in windws explorer).
You should have a look at the windows shell api.
Another possibility is to use trial and error, this way you are truly independend of the current filesystem.
The easiest way is to disallow
\ / < > | " : ? *
and you should be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With