ExpandFileNameCase - correct the filepath too? lazarus / 67-Windows ---------------------------------------------------------------------- AlexTP · 2022-10-20 22:21 This example tries to normalize casing of filename. Result is OK for filename part, but path-part is not normalized. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$h+} program project1;uses  SysUtils,  StrUtils; var  s: string;  m: TFilenameCaseMatch;begin  s:= 'c:\WORK\up_ALL.BAT';  writeln(ExpandFileNameCase(S, m));end.  It shows 'C:\WORK\up_all.bat'; I want the casing of path 'C:\Work' ('dir' in console shows 'Work'). ---------------------------------------------------------------------- KodeZwerg · 2022-10-20 23:29 Not nice but it does give back everything like it is on your system: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$APPTYPE CONSOLE} uses  SysUtils; {.$R *.res} function ReturnFilename(const AFilepath: UnicodeString): UnicodeString;var  SR: TSearchRec;  drive: UnicodeString;  path: UnicodeString;  name: UnicodeString;  tmp: UnicodeString;begin  Result := AFilepath;  if (not FileExists(AFilepath)) then    Exit;  drive := IncludeTrailingBac kslash(ExtractFileDrive(ExpandFileName(AFilepath)));  path := ExcludeTrailingBackslash(ExtractFilePath(AFilePath));  name := AFilepath;  tmp := '';  if FindFirst(path, faDirectory, SR) = 0 then  begin      tmp := drive + SR.Name;      FindClose(SR);    end    else      Exit;  if FindFirst(name, faAnyFile, SR) = 0 then    begin    tmp := IncludeTrailingBackslash(tmp) + SR.Name;      FindClose(SR);    end    else      Exit;  Result := tmp;end; var  s, ss: UnicodeString;begin  s := 'c:\program files (x86)\desktop.INI';  ss := ReturnFilename(s);  WriteLn(s);  WriteLn(ss);  ReadLn;end. ---------------------------------------------------------------------- Jorg3000 · 2022-10-21 05:19 Hi! Windows has a function for this: GetFinalPathNameByHandle() Delphi example: https://codereview.stackexchange.com/questions/268676/delphi-function- to-canonicalize-the-capitalization-in-a-file-path ---------------------------------------------------------------------- KodeZwerg · 2022-10-21 09:52 updated for any amount of sub-folders Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function ReturnFilename(const AFilepath: string): string;var  SR: TSearchRec;  tmp: string;  splits: TStringArray;  final: TStringArray;  i: Integer;begin  Result := AFilepath;  if (not FileExists(AFilepath)) then    Exit;   splits := string(AFilepath).Split(Pathdelim);  SetLength(final, Length(splits));  tmp := '';  for i := Low(splits) to High(splits) do    begin      if i < High(splits) then        tmp := tmp + IncludeTrailingBackslash(splits[i])        else        tmp := tmp + splits[i];      final[i] := tmp;    end;   for i := Low(final) to High(final) do    if i > 0 then       if FindFirst(ExcludeTrailingBackslash(final[i]), faAnyfile or faDirectory, SR) = 0 then              begin                splits[i] := SR.Name;                FindClose(SR);              end;   tmp := '';  for i := Low(splits) to High(splits) do    begin      if i < High(splits) then        tmp := tmp + IncludeTrailingBackslash(splits[i])        else        tmp := tmp + splits[i];      final[i] := tmp;    end;  tmp := final[High(final)];  Result := tmp;end;Still looks ugly but it works :-P ---------------------------------------------------------------------- KodeZwerg · 2022-10-21 10:25 updated to accept files or folders as input. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function ReturnFilename(const AFilepath: string): string;var  SR: TSearchRec;  tmp: string;  splits: TStringArray;  final: TStringArray;  i: Integer;begin  Result := AFilepath;   if ((not FileExists(AFilepath)) and (not DirectoryExists(AFilepath))) then    Exit;   final := nil;  splits := string(ExcludeTrailingBackslash(AFilepath)).Split(Pathdelim);  SetLength(final, Length(splits));  tmp := '';  for i := Low(splits) to High(splits) do    begin      if i < High(splits) then        tmp := tmp + IncludeTrailingBackslash(splits[i])        else        tmp := tmp + splits[i];      final[i] := tmp;    end;   for i := Low(final) to High(final) do    if i > 0 then      if FindFirst(ExcludeTrailingBackslash(final[i]), faAnyfile or faDirectory, SR) = 0 then        begin          splits[i] := SR.Name;          FindClose(SR);        end;   tmp := '';  for i := Low(splits) to High(splits) do    begin      if i < High(splits) then        tmp := tmp + IncludeTrailingBackslash(splits[i])        else        tmp := tmp + splits[i];      final[i] := tmp;    end;  tmp := final[High(final)];  Result := tmp;end;still ugly but working hahaha ---------------------------------------------------------------------- AlexTP · 2022-10-21 17:40 @KodeZwerg I just used this function in CudaText (git repo). Thanks... ---------------------------------------------------------------------- KodeZwerg · 2022-10-23 15:59 Quote from: AlexTP on October 21, 2022, 07:40:23 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 ---------------------------------------------------------------------- Avinash · 2022-11-10 01:58 DOS.GetLongName ---------------------------------------------------------------------- PascalDragon · 2022-11-10 06:15 Quote from: Avinash on November 10, 2022, 02:58:51 amDOS.GetLongName The DOS unit is not useful on plaftorms that support and use the SysUtils unit. ---------------------------------------------------------------------- Avinash · 2022-11-10 09:31 Looks like GetLongPathNameA / W is missing in the Windows unit? ---------------------------------------------------------------------- marcov · 2022-11-10 09:42 Quote from: Avinash on November 10, 2022, 10:31:47 amLooks like GetLongPathNameA / W is missing in the Windows unit? Looks like it magically appeared