Click here to Skip to main content
15,879,326 members
Articles / Desktop Programming / MFC
Article

Validating file names

Rate me:
Please Sign up or sign in to vote.
4.74/5 (13 votes)
9 Oct 20023 min read 147.1K   3.5K   23   21
Function to determine if a file name is valid.

Introduction

The Windows file system has documented set of rules for creating file and directory names. IsValidFileName() provides functions to check a filename against this set. They also return error codes enabling the developer to display more specific codes for the user. (These functions do NOT do a comprehensive search of the global name space to ensure there are no other failure modes with the file name in a subsequent file and/or directory creation. For example, the directory could already exist.)

The File Naming Rules

The file system has the following rules for creating names for files or directories:

  • All characters greater than ASCII 31 to be used except for the following: "*/:<>?\|
  • The name may not be only dots
  • The following device names cannot be used for a file name nor may they be used for the first segment of a file name (that part which precedes the first dot):
    CLOCK$, AUX, CON, NUL, PRN, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9,
    		LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9
  • Device names are case insensitive. (i.e. aux, AUX, Aux, etc. are identical.)
  • Names cannot be longer than 255 characters unless UNICODE paths are used

(There are additional rules for naming file paths, but they are of no concern here; refer to the WIN32 SDK documentation under "File Name Conventions" for additional information.)

The Prototypes

int IsValidFileName(const char *pFileName);
int IsValidFileName(const wchar_t *pFileName);

const TCHAR *pInvalidFileNameErrStr[];
const TCHAR *GetIsValidFileNameErrStr(int err);

IsValidFileName()

IsValidFileName() will validate a file name against the above rules. It is implemented as two overloaded functions, one which accepts a const char * and one which accepts a const wchar_t *. The function will return zero on success and non-zero on failure. A non-zero return value indicates what the error was. The possible values are listed in the following table:

Return Value Description
0 Success
>0 An illegal character was encountered. The return value is the illegal character. If the return value is a dot ('.', 46) the file name was nothing but dots.
-1 A NULL or zero length file name was passed.
<-1 A device name was used. The value corresponds to the INVALID_FILENAME_... series of enumerations. You can pass this value to GetIsValidFileNameErrStr() to obtain a pointer to the name of this device.

GetIsValidFileNameErrStr()

GetIsValidFileNameErrStr() is provided to facilitate reporting errors. This function accepts a negative value in the range of the INVALID_FILENAME_... series of enumerations and returns the device name associated with that value.

The array of strings used for this function is exposed as const TCHAR *pInvalidFileNameErrStr[]"

Notes

The source code is commented and self explanatory.

The .cpp does not include a precompiled header. If you are using this in a project with precompiled headers, just add the proper name or include the .cpp file in one of the project's cpp files.

The source may look inefficient and confusing, but it is highly optimized. It is more than 11.5 times faster than if a scanning method using _strnicmp was used.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
Joe is one of those software engineers with a film degree. His first paid programming job (you think film is a good way to make a living?) was writing games for Apple II's using 6502 assembly. He soon moved to 80x86 assembly, C, C++ (for a long time), C# and then back to C++ with occasional dabbling in C#, Python and other vile languages.

He first wrote software for Windows 3.0 in 1990. Save for some work in Linux, DOS and a mercifully brief foray into OS/2, he has concentrated on designing and writing software for all versions and types of Windows except RT.

Comments and Discussions

 
QuestionCompile with Express Edition? Pin
JustinGiovanetti5-May-09 7:06
JustinGiovanetti5-May-09 7:06 
AnswerRe: Compile with Express Edition? Pin
JustinGiovanetti5-May-09 14:59
JustinGiovanetti5-May-09 14:59 
GeneralRe: Compile with Express Edition? Pin
Joe Woodbury5-May-09 15:46
professionalJoe Woodbury5-May-09 15:46 
GeneralRe: Compile with Express Edition? Pin
JustinGiovanetti5-May-09 17:10
JustinGiovanetti5-May-09 17:10 
GeneralRe: Compile with Express Edition? Pin
Joe Woodbury5-May-09 17:32
professionalJoe Woodbury5-May-09 17:32 
GeneralMore Invalid Filenames Pin
Millard Filmore7-Sep-07 2:53
Millard Filmore7-Sep-07 2:53 
General'_' Pin
ynial24-Jul-06 23:34
ynial24-Jul-06 23:34 
GeneralRe: '_' Pin
Joe Woodbury25-Jul-06 1:10
professionalJoe Woodbury25-Jul-06 1:10 
QuestionHow about this function: PathIsDirectory()? Pin
flyingxu13-May-06 3:50
flyingxu13-May-06 3:50 
AnswerRe: How about this function: PathIsDirectory()? Pin
Joe Woodbury13-May-06 10:16
professionalJoe Woodbury13-May-06 10:16 
GeneralGood Stuff Pin
Larry Antram4-Nov-03 18:57
Larry Antram4-Nov-03 18:57 
GeneralRe: Good Stuff Pin
flyingxu15-Nov-05 14:49
flyingxu15-Nov-05 14:49 
GeneralThink again... Pin
Andreas Saurwein7-Oct-02 1:18
Andreas Saurwein7-Oct-02 1:18 
GeneralRe: Think again... Pin
Joe Woodbury7-Oct-02 4:40
professionalJoe Woodbury7-Oct-02 4:40 
GeneralRe: Think again... Pin
Andreas Saurwein7-Oct-02 6:24
Andreas Saurwein7-Oct-02 6:24 
GeneralRe: Think again... Pin
Joe Woodbury7-Oct-02 7:23
professionalJoe Woodbury7-Oct-02 7:23 
GeneralRe: Think again... Pin
Gabriel 210-Oct-02 3:48
Gabriel 210-Oct-02 3:48 
GeneralRe: Think again... Pin
Andreas Saurwein10-Oct-02 4:04
Andreas Saurwein10-Oct-02 4:04 
GeneralRe: Think again... Pin
Joe Woodbury10-Oct-02 5:32
professionalJoe Woodbury10-Oct-02 5:32 
GeneralRe: Think again... Pin
Philippe Lhoste15-Oct-02 3:06
Philippe Lhoste15-Oct-02 3:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.