DelphiFAQ Home Search:

Test if a string is a valid file name

 

comments6 comments. Current rating: 4 stars (3 votes). Leave comments and/ or rate it.

The following code tests a given string for forbidden characters.
The forbidden characters are dependent on whether it is a 8.3 (short) or a long file name.

Thanks to a comment to A. Pottjeweid, who pointed out that the forward slash is a problem character as well.

const
  { for short 8.3 file names }
  ShortForbiddenChars : set of Char = [';', '=', '+', '<', '>', '|',
                                       '"', '[', ']', '\', '/', ''''];
  { for long file names }
  LongForbiddenChars  : set of Char = ['<', '>', '|', '"', '\', '/', ':', '*', '?'];

function TestFilename(Filename: String; islong: Boolean) : Boolean;
var
  I: integer;
begin
  Result := Filename <> '';
  if islong then
  begin
    for I := 1 to Length(Filename) do
      Result := Result and not (Filename[I] in LongForbiddenChars);
  end
  else
  begin
    for I := 1 to Length(Filename) do
      Result := Result and not (Filename[I] in ShortForbiddenChars);
  end;
end;


Comments:

2005-11-06, 20:14:19
hungpvtn@gmail.com from Australia  
LongForbiddenChars : set of Char = ['<', '>', '|', ''', '', '/',':','*','?'];
2007-02-04, 22:04:49
anonymous from Madrid in Madrid, Spain  
Here is good website!!! I will introduce it to my friends...
2007-02-05, 00:38:45
anonymous from Sønderborg in Sonderjylland, Denmark  
rating
2007-06-11, 04:45:14
Vahid from Iran, Iran  
rating
usefull :) TNX
2007-07-19, 03:19:34
anonymous from Ukraine  
rating
thanks for code
2007-09-25, 21:35:39
anonymous from Australia  
Actual list is: ?[]/\=+<>:;',*
http://msdn.microso..a_file.asp

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.