1. Computing
in Delphi TIPS :: If you have an application that operates on the file system and one of the tasks of the application is creating or manipulating files you might need to check if a given file name is valid - will Windows allow your code to save a file using the specified file name.

Read the full article to learn how to Check if a given File Name is Valid using Delphi

The code does what Windows Explorer warns about if you try to name a file using invalid characters: A file name cannot contain any of the following characters: \ / : * ? " < > |

Related:

Comments
April 7, 2009 at 7:30 pm
(1) Brad says:

In Delphi 7 this function does not compile.

Specifically this line below
while c in fileName do

I changed it to
while pos(c,fileName) > 0 do

This seems to work but there may be better methods.

April 8, 2009 at 3:51 am
(2) Lasse says:

Here is my code for Delphi 7:
//
// Test if a “FileName” is a valid Windows file name
//
FUNCTION IsValidFileName(CONST FileName: String): boolean;
CONST
InvalidCharacters: SET OF Char = ['\', '/', ':', '*', '?', '"', '', '|'];

VAR
I, Lng: Integer;

BEGIN
//
Result:= FileName ”;
//
IF Result THEN BEGIN
Lng:= Length(FileName);
FOR I:= 1 TO Lng DO BEGIN
IF FileName[I] IN InvalidCharacters THEN BEGIN
Result:= FALSE; Break
END;
END;
END;
END; (* IsValidFileName *)

April 8, 2009 at 9:27 am
(3) Teun van Immerzeel says:

Even shorter! :

function IsValidFileName( const FileName: string ): boolean;
begin
Result:=(FileName”) and (LastDelimiter(‘\/:*?”|’,FileName )=0)
end;

April 12, 2009 at 12:30 pm
(4) TridenT says:

Hi,

You have missed one test about the filename.
here is a COMPLEMENT to ignore Windows KEYWORD for filename (CON, COMx, LPTx, NUL, PRN, AUX)

[CODE]

{*——————————————————————————
Verify the filename compatibility (with Windows OS) and fix it if nedded
Reserved word are :
CON, COMx (COM1, COM2, …), LPTx (LPT1, LPT2, …), NUL, PRN, AUX,
@param fileInput Input filename to verify
@return Fixed filename (or same if valid)
——————————————————————————}
function ConvertToValidFilename(const fileInput : TFilename) : TFilename;
const
FSReservedName : array [1..6] of string = (‘CON’,'COM’,'LPT’,'NUL’,'PRN’,'AUX’);
var
IndexWord : integer;
FilenameLength : integer;
begin
// Set result to same value
Result := fileInput;
// Limit test to string with no more than 4 characters
FilenameLength := Length(fileInput);
if (FilenameLength > 4) or (FilenameLength

April 12, 2009 at 12:31 pm
(5) TridenT says:

function ConvertToValidFilename(const fileInput : TFilename) : TFilename;
const
FSReservedName : array [1..6] of string = (‘CON’,'COM’,'LPT’,'NUL’,'PRN’,'AUX’);
var
IndexWord : integer;
FilenameLength : integer;
begin
// Set result to same value
Result := fileInput;
// Limit test to string with no more than 4 characters
FilenameLength := Length(fileInput);
if (FilenameLength > 4) or (FilenameLength

May 8, 2009 at 2:42 pm
(6) Zarko Gajic says:

@ Teun:
A real gem! Nice work. Clean and simple.

@TridenT : please send this to my email .. I’ll publish it if you agree.

@All: the best approach (at least for me) would be to use RegEx :

http://delphi.about.com/od/toppicks/tp/delphi-regular-expressions.htm

December 1, 2010 at 6:43 am
(7) Lazac says:

Some special characters having ASCII codes below 32 are also not accepted in file names (for example, backspace, or linefeed), so the check criteria should be expanded accordingly.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2014 About.com. All rights reserved.