Recent

Author Topic: ExpandFileNameCase - correct the filepath too?  (Read 924 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 1961
    • UVviewsoft
ExpandFileNameCase - correct the filepath too?
« on: October 21, 2022, 12:21:58 am »
This example tries to normalize casing of filename. Result is OK for filename part, but path-part is not normalized.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$h+}
  2.  
  3. program project1;
  4. uses
  5.   SysUtils,
  6.   StrUtils;
  7.  
  8. var
  9.   s: string;
  10.   m: TFilenameCaseMatch;
  11. begin
  12.   s:= 'c:\WORK\up_ALL.BAT';
  13.   writeln(ExpandFileNameCase(S, m));
  14. end.
  15.  

It shows 'C:\WORK\up_all.bat'; I want the casing of path 'C:\Work' ('dir' in console shows 'Work').

KodeZwerg

  • Hero Member
  • *****
  • Posts: 568
  • Fifty shades of code.
    • Delphi & FreePascal
Re: ExpandFileNameCase - correct the filepath too?
« Reply #1 on: October 21, 2022, 01:29:39 am »
Not nice but it does give back everything like it is on your system:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. {.$R *.res}
  9.  
  10. function ReturnFilename(const AFilepath: UnicodeString): UnicodeString;
  11. var
  12.   SR: TSearchRec;
  13.   drive: UnicodeString;
  14.   path: UnicodeString;
  15.   name: UnicodeString;
  16.   tmp: UnicodeString;
  17. begin
  18.   Result := AFilepath;
  19.   if (not FileExists(AFilepath)) then
  20.     Exit;
  21.   drive := IncludeTrailingBackslash(ExtractFileDrive(ExpandFileName(AFilepath)));
  22.   path := ExcludeTrailingBackslash(ExtractFilePath(AFilePath));
  23.   name := AFilepath;
  24.   tmp := '';
  25.   if FindFirst(path, faDirectory, SR) = 0 then
  26.     begin
  27.       tmp := drive + SR.Name;
  28.       FindClose(SR);
  29.     end
  30.     else
  31.       Exit;
  32.   if FindFirst(name, faAnyFile, SR) = 0 then
  33.     begin
  34.       tmp := IncludeTrailingBackslash(tmp) + SR.Name;
  35.       FindClose(SR);
  36.     end
  37.     else
  38.       Exit;
  39.   Result := tmp;
  40. end;
  41.  
  42. var
  43.   s, ss: UnicodeString;
  44. begin
  45.   s := 'c:\program files (x86)\desktop.INI';
  46.   ss := ReturnFilename(s);
  47.   WriteLn(s);
  48.   WriteLn(ss);
  49.   ReadLn;
  50. end.
« Last Edit: October 21, 2022, 01:38:27 am by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Jorg3000

  • New Member
  • *
  • Posts: 41
Re: ExpandFileNameCase - correct the filepath too?
« Reply #2 on: October 21, 2022, 07:19:31 am »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 568
  • Fifty shades of code.
    • Delphi & FreePascal
Re: ExpandFileNameCase - correct the filepath too?
« Reply #3 on: October 21, 2022, 11:52:38 am »
updated for any amount of sub-folders
Code: Pascal  [Select][+][-]
  1. function ReturnFilename(const AFilepath: string): string;
  2. var
  3.   SR: TSearchRec;
  4.   tmp: string;
  5.   splits: TStringArray;
  6.   final: TStringArray;
  7.   i: Integer;
  8. begin
  9.   Result := AFilepath;
  10.   if (not FileExists(AFilepath)) then
  11.     Exit;
  12.  
  13.   splits := string(AFilepath).Split(Pathdelim);
  14.   SetLength(final, Length(splits));
  15.   tmp := '';
  16.   for i := Low(splits) to High(splits) do
  17.     begin
  18.       if i < High(splits) then
  19.         tmp := tmp + IncludeTrailingBackslash(splits[i])
  20.         else
  21.         tmp := tmp + splits[i];
  22.       final[i] := tmp;
  23.     end;
  24.  
  25.   for i := Low(final) to High(final) do
  26.     if i > 0 then
  27.        if FindFirst(ExcludeTrailingBackslash(final[i]), faAnyfile or faDirectory, SR) = 0 then
  28.               begin
  29.                 splits[i] := SR.Name;
  30.                 FindClose(SR);
  31.               end;
  32.  
  33.   tmp := '';
  34.   for i := Low(splits) to High(splits) do
  35.     begin
  36.       if i < High(splits) then
  37.         tmp := tmp + IncludeTrailingBackslash(splits[i])
  38.         else
  39.         tmp := tmp + splits[i];
  40.       final[i] := tmp;
  41.     end;
  42.   tmp := final[High(final)];
  43.   Result := tmp;
  44. end;
Still looks ugly but it works :-P
« Last Edit: October 21, 2022, 12:01:13 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 568
  • Fifty shades of code.
    • Delphi & FreePascal
Re: ExpandFileNameCase - correct the filepath too?
« Reply #4 on: October 21, 2022, 12:25:47 pm »
updated to accept files or folders as input.
Code: Pascal  [Select][+][-]
  1. function ReturnFilename(const AFilepath: string): string;
  2. var
  3.   SR: TSearchRec;
  4.   tmp: string;
  5.   splits: TStringArray;
  6.   final: TStringArray;
  7.   i: Integer;
  8. begin
  9.   Result := AFilepath;
  10.  
  11.   if ((not FileExists(AFilepath)) and (not DirectoryExists(AFilepath))) then
  12.     Exit;
  13.  
  14.   final := nil;
  15.   splits := string(ExcludeTrailingBackslash(AFilepath)).Split(Pathdelim);
  16.   SetLength(final, Length(splits));
  17.   tmp := '';
  18.   for i := Low(splits) to High(splits) do
  19.     begin
  20.       if i < High(splits) then
  21.         tmp := tmp + IncludeTrailingBackslash(splits[i])
  22.         else
  23.         tmp := tmp + splits[i];
  24.       final[i] := tmp;
  25.     end;
  26.  
  27.   for i := Low(final) to High(final) do
  28.     if i > 0 then
  29.       if FindFirst(ExcludeTrailingBackslash(final[i]), faAnyfile or faDirectory, SR) = 0 then
  30.         begin
  31.           splits[i] := SR.Name;
  32.           FindClose(SR);
  33.         end;
  34.  
  35.   tmp := '';
  36.   for i := Low(splits) to High(splits) do
  37.     begin
  38.       if i < High(splits) then
  39.         tmp := tmp + IncludeTrailingBackslash(splits[i])
  40.         else
  41.         tmp := tmp + splits[i];
  42.       final[i] := tmp;
  43.     end;
  44.   tmp := final[High(final)];
  45.   Result := tmp;
  46. end;
still ugly but working hahaha
« Last Edit: October 21, 2022, 12:39:42 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

AlexTP

  • Hero Member
  • *****
  • Posts: 1961
    • UVviewsoft
Re: ExpandFileNameCase - correct the filepath too?
« Reply #5 on: October 21, 2022, 07:40:23 pm »
@KodeZwerg
I just used this function in CudaText (git repo). Thanks...

KodeZwerg

  • Hero Member
  • *****
  • Posts: 568
  • Fifty shades of code.
    • Delphi & FreePascal
Re: ExpandFileNameCase - correct the filepath too?
« Reply #6 on: October 23, 2022, 05:59:45 pm »
@KodeZwerg
I just used this function in CudaText (git repo). Thanks...
I have no idea what CudaText is but i am happy to help  O:-)
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Avinash

  • Jr. Member
  • **
  • Posts: 83
Re: ExpandFileNameCase - correct the filepath too?
« Reply #7 on: November 10, 2022, 02:58:51 am »
DOS.GetLongName

PascalDragon

  • Hero Member
  • *****
  • Posts: 4765
  • Compiler Developer
Re: ExpandFileNameCase - correct the filepath too?
« Reply #8 on: November 10, 2022, 07:15:58 am »
DOS.GetLongName

The DOS unit is not useful on plaftorms that support and use the SysUtils unit.

Avinash

  • Jr. Member
  • **
  • Posts: 83
Re: ExpandFileNameCase - correct the filepath too?
« Reply #9 on: November 10, 2022, 10:31:47 am »
Looks like GetLongPathNameA / W is missing in the Windows unit?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10400
  • FPC developer.
Re: ExpandFileNameCase - correct the filepath too?
« Reply #10 on: November 10, 2022, 10:42:42 am »
Looks like GetLongPathNameA / W is missing in the Windows unit?

Looks like it magically appeared  :)

 

TinyPortal © 2005-2018