DelphiFAQ Home Search:

Test if a string is a valid file name

 

comments11 comments. Current rating: 4 stars (4 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
2010-05-11, 10:28:11
anonymous from United States  
CRSABLH-809429
2010-05-11, 10:28:11
anonymous from United States  
CRSABLH-809429
2011-10-10, 09:36:51   (updated: 2011-10-10, 09:37:34)
anonymous from United States  
f
2012-04-14, 09:32:46
anonymous from Ukraine  
rating
2012-11-13, 00:13:24
anonymous from China  
http://www.giantste..mfans.com/ Nike Giants Jersey
http://www.giantste..ing-jersey Eli Manning Jersey
http://www.giantste..ruz-jersey Victor Cruz Jersey
http://www.giantste..aul-jersey Jason Pierre-Paul Jersey
http://www.giantste..cks-jersey Hakeem Nicks Jersey
http://www.giantste..uck-jersey Justin Tuck Jersey
http://www.giantste..haw-jersey Ahmad Bradshaw Jersey
http://www.giantste..lle-jersey Antrel Rolle Jersey
http://www.giantste..den-jersey Antwaun Molden Jersey
http://www.giantste..nks-jersey Carl Banks Jersey
http://www.giantste..nty-jersey Chris Canty Jersey
http://www.giantste..nee-jersey Chris Snee Jersey
http://www.giantste..ter-jersey Corey Webster Jersey
http://www.giantste..are-jersey D.J. Ware Jersey
http://www.giantste..hes-jersey Dante Hughes Jersey
http://www.giantste..ehl-jersey David Diehl Jersey
http://www.giantste..son-jersey David Wilson Jersey
http://www.giantste..nes-jersey Greg Jones Jersey
http://www.giantste..son-jersey Harry Carson Jersey
http://www.giantste..ams-jersey Jacquian Williams Jersey
http://www.giantste..ley-jersey Julian Talley Jersey
http://www.giantste..ips-jersey Kenny Phillips Jersey
http://www.giantste..lor-jersey Lawrence Taylor Jersey
http://www.giantste..nes-jersey Lawrence Tynes Jersey
http://www.giantste..eph-jersey Linval Joseph Jersey
http://www.giantste..aro-jersey Mark Bavaro Jersey
http://www.giantste..ett-jersey Martellus Bennett Jersey
http://www.giantste..uka-jersey Mathias Kiwanuka Jersey
http://www.giantste..ley-jersey Michael Boley Jersey
http://www.giantste..ora-jersey Osi Umenyiora Jersey
http://www.giantste..mms-jersey Phil Simms Jersey
http://www.giantste..ara-jersey Prince Amukamara Jersey
http://www.giantste..ard-jersey Rocky Bernard Jersey
http://www.giantste..dle-jersey Rueben Randle Jersey
http://www.giantste..ord-jersey Steve Weatherford Jersey

 

 

NEW: Optional: Register   Login
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, or post under a registered account.
 

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:

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.

photo Add a picture: