Forum > Windows
ExpandFileNameCase - correct the filepath too?
AlexTP:
This example tries to normalize casing of filename. Result is OK for filename part, but path-part is not normalized.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$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:
Not nice but it does give back everything like it is on your system:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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 := IncludeTrailingBackslash(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:
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:
updated for any amount of sub-folders
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
updated to accept files or folders as input.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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
Navigation
[0] Message Index
[#] Next page