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:

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.
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 *)
Even shorter! :
function IsValidFileName( const FileName: string ): boolean;
begin
Result:=(FileName”) and (LastDelimiter(‘\/:*?”|’,FileName )=0)
end;
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
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
@ 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
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.