Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Short File Names

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,312
US
I was reminded of a project I mean to go back to soon and thought of a problem I had. One of the things I was wanting to do was get a short file name off of a long file name. Is that a possible thing under Windows XP? (I knew it was under 95/98/ME) I tried accessing cAlternateFileName of TWin32FindData off of FindFirst/FindNext, and wasn't able to get a short file name.

So is there a function in Delphi/Windows that will return a short file name given a long one under any conditions? I was looking and found KB142982. In the case of Windows XP, would it be required to write something compliant to this KB article to get this done? Or is there something within Rewrite that would allow the creation of the short name file given the long file name?

(If you're curious the project involved writing a data file to be read by an app that only accepts 8.3 file names. An alternative, maybe my own proprietary name scheme?)
 
This (Delphi 7) code works for me in Windows XP Pro:
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  tsr: TSearchRec;
  x: TWin32FindData;
begin
  if Findfirst( 'c:\photos\*.jpg', 0, tsr ) = 0 then begin
    repeat
      x := tsr.FindData;
      ShowMessage( x.cAlternateFileName );
    until FindNext( tsr ) <> 0;
    FindClose( tsr );
  end;
end;


Andrew
Hampshire, UK
 
Glenn

Was my code any help or is this still an outstanding problem?

Andrew
Hampshire, UK
 
That's the same basic thing I tried. I just pulled the code back out and it worked for Windows ME. I have no idea if there's some limitation in XP (NTFS) that would make it not store or return a FAT32 style short-file name. Perhaps that is a difference and why it didn't work for me? I don't know.
 
Creation of short filenames _can_ be turned off, so XP can't retrieve them. This requires registry 'hacking' though (google).
There is a GetShortFileName() function (or in that name-trend) available, that fetches the name for you, if it's available, and that is probably used to fill the record for you already.

HTH
TonHu
 
Creation of short filenames _can_ be turned off, so XP can't retrieve them. This requires registry 'hacking' though (google).

When I get back to the exact code in question (and not the stub I tested), I can investigate that. If that's the case, I'll need to come up with some kind of file name mangling scheme to get 8x3 from a LFN.
 
The long to short filename 'mangling' algorithm can be found somewhere in msdn, but it's too tricky and volatile to be implemented in any of my applications.

I'd advise to seek other options in the matter at hand, like upgrading the external program to support long filenames (or use double-quotes around the name on the commandline?)

In extreme cases you could create a Logical Link (XP and later only, just like Unix/Linux can) with a 8.3 filename, use it to run the external program, and if no longer needed, remove the L.Link.

HTH
TonHu
 
Edit1.Text := GetShortFileName(OpenDialog1.FileName);

That's a basic code...
Probably won't help you but still :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top