Author: William Gerbert
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.
1 const
2 { for short 8.3 file names }
3 ShortForbiddenChars: set of Char = [';', '=', '+', '<', '>', '|',
4 '"', '[', ']', '\', ''''];
5 { for long file names }
6 LongForbiddenChars: set of Char = ['<', '>', '|', '"', '\'];
7
8 function TestFilename(Filename: string; islong: Boolean): Boolean;
9 var
10 I: integer;
11 begin
12 Result := Filename <> '';
13 if islong then
14 begin
15 for I := 1 to Length(Filename) do
16 Result := Result and not (Filename[I] in LongForbiddenChars);
17 end
18 else
19 begin
20 for I := 1 to Length(Filename) do
21 Result := Result and not (Filename[I] in ShortForbiddenChars);
22 end;
23 end;
|