Author: William Gerbert
Long file names - short file names
Answer:
Here's a way to convert between short (8.3 DOS file names) and long file names:
1 {$APPTYPE console}
2
3 program LongShrt;
4
5 uses
6 Windows, SysUtils;
7
8 function GetShortName(sLongName: string): string;
9 var
10 sShortName: string;
11 nShortNameLen: integer;
12 begin
13 SetLength(sShortName, MAX_PATH);
14
15 nShortNameLen := GetShortPathName(PChar(sLongName),
16 PChar(sShortName), MAX_PATH - 1);
17
18 if nShortNameLen = 0 then
19 begin
20 { handle errors... }
21 end;
22
23 SetLength(sShortName, nShortNameLen);
24
25 Result := sShortName;
26 end;
27
28 function GetLongName(sShortName: string; var bError: boolean): string;
29 var
30 bAddSlash: boolean;
31 SearchRec: TSearchRec;
32 nStrLen: integer;
33 begin
34 bError := False;
35 Result := sShortName;
36 nStrLen := Length(sShortName);
37 bAddSlash := False;
38
39 if sShortName[nStrLen] = '\' then
40 begin
41 bAddSlash := True;
42 SetLength(sShortName, nStrLen - 1);
43 dec(nStrLen);
44 end;
45
46 if ((nStrLen - Length(ExtractFileDrive(sShortName))) > 0) then
47 begin
48 if FindFirst(sShortName, faAnyFile, SearchRec) = 0 then
49 begin
50 Result := ExtractFilePath(sShortName) + SearchRec.name;
51 if bAddSlash then
52 begin
53 Result := Result + '\';
54 end;
55 end
56 else
57 begin
58 // handle errors... bError := True;
59 end;
60 FindClose(SearchRec);
61 end;
62 end;
63
64 function GetLongName(sShortName: string): string;
65 var
66 s: string;
67 p: integer;
68 bError: boolean;
69 begin
70 Result := sShortName;
71
72 s := '';
73 p := Pos('\', sShortName);
74 while (p > 0) do
75 begin
76 s := GetLongName(s + Copy(sShortName, 1, p), bError);
77 Delete(sShortName, 1, p);
78 p := Pos('\', sShortName);
79
80 if (bError) then
81 Exit;
82 end;
83 if sShortName <> '' then
84 begin
85 s := GetLongName(s + sShortName, bError);
86 if bError then
87 Exit;
88 end;
89 Result := s;
90 end;
91
92 const
93 csTest = 'C:\program Files';
94
95 var
96 sShort,
97 sLong: string;
98
99 begin
100 sShort := GetShortName(csTest);
101 WriteLn('Short name for "' + csTest +
102 '" is "' + sShort + '"');
103
104 WriteLn;
105
106 sLong := GetLongName(sShort);
107 WriteLn('Long name for "' + sShort + '" is "' + sLong + '"');
108 end.
|