2008. április 13., vasárnap

Test if a string is a valid file name


Problem/Question/Abstract:

Test if a string is a valid file name

Answer:

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.


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;

Nincsenek megjegyzések:

Megjegyzés küldése