Search code examples
stringinno-setuppascalscript

Cut specific part of path


Here is the code from Inno Setup

function GetDirName(const Value: string): string;
    var 
    InstallPath: string;
    begin

    RegQueryStringValue(HKLM, 'HKEY_LOCAL_MACHINE\SOFTWARE\Classes\IconExtractor\DefaultIcon', '', InstallPath)

    Result := InstallPath;
    end;

The result is as below:

Result = C:\ProgramFiles\Solutions\Extractor\tools\v3\iconextractor.exe,1

But i only want to remove the last 3 path elements

C:\Program Files\Solutions\Extractor

How i can do it?


Solution

  • Try little bit of code

    function GetDirName(const Value: string): string;
    var 
     InstallPath : string;
     I,bscount   : Integer;
    
    begin
     RegQueryStringValue(HKLM,'HKEY_LOCAL_MACHINE\SOFTWARE\Classes\IconExtractor\DefaultIcon',
                            '', InstallPath);
     bscount := 0;
    
     for I := Length(InstallPath) downto 1 do begin
       if InstallPath[I] = '\' then Inc(bscount);
       if bscount = 3 then begin
          InstallPath := Copy(InstallPath,1,I-1);
          break;
       end;
    end;// for
    
    Result := InstallPath;
    end;