Lazarus

Using the Lazarus IDE => Editor => Topic started by: vfclists on July 06, 2016, 07:09:58 pm

Title: Library routine for returning the exact name for a case-insensitive filename?
Post by: vfclists on July 06, 2016, 07:09:58 pm
 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?
Title: Re: Library routine for returning the exact name for a case-insensitive filename?
Post by: molly on July 06, 2016, 07:39:06 pm
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.
Title: Re: Library routine for returning the exact name for a case-insensitive filename?
Post by: rvk on July 06, 2016, 07:53:56 pm
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.)
Title: Re: Library routine for returning the exact name for a case-insensitive filename?
Post by: vfclists on July 06, 2016, 08:52:38 pm
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?
Title: Re: Library routine for returning the exact name for a case-insensitive filename?
Post by: derek.john.evans on July 06, 2016, 10:13:37 pm
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.   
Title: Re: Library routine for returning the exact name for a case-insensitive filename?
Post by: vfclists on July 12, 2016, 06:42:11 am
FileUtil

function FindDiskFilename(const Filename: string): string;


Thanks
Title: Re: Library routine for returning the exact name for a case-insensitive filename?
Post by: Kaller on October 18, 2021, 08:36:33 am
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

:)
TinyPortal © 2005-2018