Recent

Author Topic: Library routine for returning the exact name for a case-insensitive filename?  (Read 7185 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
 In a case-insensitive file system like Windows, is there a routine for returning a filename in its original case, eg if a file is saved as 'aBcDe' and the user entered 'abcde' which locates the file, is there a routine that can check the file system and and return 'aBcDe' as the original spelling?
Lazarus 3.0/FPC 3.2.2

molly

  • Hero Member
  • *****
  • Posts: 2330
In a case-insensitive file system like Windows, is there a routine for returning a filename in its original case, eg if a file is saved as 'aBcDe' and the user entered 'abcde' which locates the file, is there a routine that can check the file system and and return 'aBcDe' as the original spelling?
All file related routines for FPC/Lazarus preserve filename case as it is a filesystem/OS related matter.

rvk

  • Hero Member
  • *****
  • Posts: 6111
In a case-insensitive file system like Windows, is there a routine for returning a filename in its original case, eg if a file is saved as 'aBcDe' and the user entered 'abcde' which locates the file, is there a routine that can check the file system and and return 'aBcDe' as the original spelling?
Not sure if there is a ready made function.
But you could use something like this:
Code: Pascal  [Select][+][-]
  1. function GiveCorrectCaseFileName(Filename: String): String;
  2. var
  3.   Sr: TSearchRec;
  4. begin
  5.   Result := '';
  6.   if FindFirst(Filename, faAnyFile and faDirectory, Sr) = 0 then
  7.     Result := ExtractFilePath(Filename) + Sr.Name;
  8.   FindClose(Sr);
  9. end;
  10.  
  11. procedure TForm1.Button1Click(Sender: TObject);
  12. begin
  13.  Showmessage(GiveCorrectCaseFileName('c:\temp\TeSt.tXt'));
  14. end;

(Note that the filepath is extracted and not corrected. If you don't need the pathname you can omit the ExtractFilePath() from the function.)

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
In a case-insensitive file system like Windows, is there a routine for returning a filename in its original case, eg if a file is saved as 'aBcDe' and the user entered 'abcde' which locates the file, is there a routine that can check the file system and and return 'aBcDe' as the original spelling?
Not sure if there is a ready made function.
But you could use something like this:
Code: Pascal  [Select][+][-]
  1. function GiveCorrectCaseFileName(Filename: String): String;
  2. var
  3.   Sr: TSearchRec;
  4. begin
  5.   Result := '';
  6.   if FindFirst(Filename, faAnyFile and faDirectory, Sr) = 0 then
  7.     Result := ExtractFilePath(Filename) + Sr.Name;
  8.   FindClose(Sr);
  9. end;
  10.  
  11. procedure TForm1.Button1Click(Sender: TObject);
  12. begin
  13.  Showmessage(GiveCorrectCaseFileName('c:\temp\TeSt.tXt'));
  14. end;

(Note that the filepath is extracted and not corrected. If you don't need the pathname you can omit the ExtractFilePath() from the function.)

I was considering something along this line. Does Windows have a value such as the inode of Linux which can be used to obtain the exact filename and other details about a file?
Lazarus 3.0/FPC 3.2.2

derek.john.evans

  • Guest
FileUtil

function FindDiskFilename(const Filename: string): string;
// Searches for the filename case on disk.
// The file must exist.
// For example:
//   If Filename='file' and there is only a 'File' then 'File' will be returned.   

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
FileUtil

function FindDiskFilename(const Filename: string): string;


Thanks
Lazarus 3.0/FPC 3.2.2

Kaller

  • Jr. Member
  • **
  • Posts: 73
ExpandFileNameCase function will return the full path as on disk case

type TFilenameCaseMatch = ( 
mkNone, No file was found
mkExactMatch, The filename can be used to refer to a file on the system (findfirst will find it).
mkSingleMatch, Exactly one match was found, but case didn't match.
mkAmbiguous More than one file will match the filename in a case-insensitive way

:)
« Last Edit: October 18, 2021, 08:51:38 am by Kaller »

 

TinyPortal © 2005-2018