Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check file name is valid windows name

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

like image 825
user152508 Avatar asked Jun 21 '10 13:06

user152508


People also ask

What are valid file names in Windows?

Supported characters for a file name are letters, numbers, spaces, and ( ) _ - , . *Please note file names should be limited to 100 characters.

How do I make a file name valid?

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.

How do I fix not a valid file name?

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.

Why is it saying my file name is not valid?

'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.


2 Answers

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.

like image 147
M. Williams Avatar answered Sep 21 '22 08:09

M. Williams


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.

like image 33
codymanix Avatar answered Sep 21 '22 08:09

codymanix