How to check file name in Windows?

Is there a Windows API function with which I can pass a string value that will return a value indicating whether the file name is valid or not?

I need to verify that the file name is valid, and I'm looking for an easy way to do this without reinventing the wheel. I work in direct C, but focus on the Win32 API.

If there is no such built-in function, how would I start writing my own? Is there a general algorithm or pattern that Windows follows to determine the validity of a file name?

+7
source share
3 answers

The problem is not so simple because it depends on what you consider to be a "valid file name".

The Windows APIs used with UNC paths will allow you to happily create many names that are considered invalid inside regular paths, since with the \\?\ you tell the Windows API to simply deliver the path to the file system driver without executing any inspections; the file systems themselves often do not care about what they used as the file name, as soon as they know that some line is just the file name (i.e. the path / name separation has already been done), they usually treat it as an opaque sequence characters.

On the other hand, if you want to play safely, you must perform the check in accordance with the rules specified in the document

But, in my opinion, if you need to create a file immediately, the best thing to check is what you can do, try to create / open the file, letting the OS do the job for you and be ready to gracefully refuse ( GetLastError should return ERROR_BAD_PATHNAME ) This will check for any other restriction you have on creating such a file, for example. that your application has the appropriate permissions, that the path is not in a read-only environment, ...

If for some reason this is not possible, you may like the shell function: provided that the name of the requested file and the directory in the file system where it should be created, this function will delete all invalid characters (I'm not sure about the reserved DOS names, they are not listed in its documentation), which makes the path "probably valid" and notifies you if any modification has been made (so you can use it also only for verification).

Please note that this feature is labeled as “modifiable or removable in any future version of Windows,” although Microsoft’s policy is usually that “everything that made it a way for a public title will remain open forever.”

+5
source

If you check if the file name is valid in the sense of "can this file be named like this?"

No, there is no way to directly verify this. You will need to write your own function.

But if you know what is a valid file name (a valid file name now contains any of the following: \ / : * ? " < > | ), Which should not be such a problem.

Perhaps you can help yourself with some of these functions from ctype.h (with them you can check if a particular character belongs to certain specific character classes):

http://www.cplusplus.com/reference/clibrary/cctype/

0
source

This function gives you a list of invalid characters for the file name. It is up to you to verify that your file name does not contain:

 public static char[] Path.GetInvalidFileNameChars() 

The docs are here .

Note that if you want to check the directory name, you must use GetInvalidPathChars() .

EDIT: Oooo! Sorry, I thought you were on .NET. Using Reflector, here these functions come down to the following:

 '"', '<', '>', '|', '\0', '\x0001', '\x0002', '\x0003', '\x0004', '\x0005', '\x0006', '\a', '\b', '\t', '\n', '\v', '\f', '\r', '\x000e', '\x000f', '\x0010', '\x0011', '\x0012', '\x0013', '\x0014', '\x0015', '\x0016', '\x0017', '\x0018', '\x0019', '\x001a', '\x001b', '\x001c', '\x001d', '\x001e', '\x001f', ':', '*', '?', '\\', '/' 

Please note: in addition, there are reserved names such as prn, con, com1, com2,... , lpt1, lpt2,...

-one
source

All Articles