Marth8880
Marth8880

Reputation: 53

Delete specific part of string

I'm using Pascal scripting to return a string based on a registry entry but I need to delete a specific part of that string.

Here's the current code:

function GetDirName(Value: string): string;
var          
  InstallPath: string;
begin
  // initialize default path, which will be returned when the following registry
  // key queries fail due to missing keys or for some different reason
  Result := '{pf}\LucasArts\Star Wars Battlefront II\RegistryFailed';
  // query the first registry value; if this succeeds, return the obtained value
  if RegQueryStringValue(HKLM, 'SOFTWARE\LucasArts\Star Wars Battlefront II\1.0', 'ExePath', InstallPath) then
    Result := InstallPath
  // otherwise the first registry key query failed, so...
  else
  // query the second registry value; if it succeeds, return the obtained value
  if RegQueryStringValue(HKLM, 'SOFTWARE\Wow6432Node\LucasArts\Star Wars Battlefront II\1.0', 'ExePath', InstallPath) then
    Result := InstallPath
end;

The part that I need to remove from the returned string is \BattlefrontII.exe and it is always located at the very end of the string.

Example of what the returned string currently is: C:\Program Files (x86)\LucasArts\Star Wars Battlefront II\GameData\BattlefrontII.exe

Example of what I need the returned string to look more-or-less like: C:\Program Files (x86)\LucasArts\Star Wars Battlefront II\GameData

Any help is appreciated.

P.S. I am entirely new to Pascal; I'm only using it to compile an installer using Inno Setup Compiler

Upvotes: 2

Views: 1413

Answers (1)

Blorgbeard
Blorgbeard

Reputation: 103525

Looks like what you want is ExtractFilePath:

function ExtractFilePath(const FileName: string): String;

Description: Extracts the drive and directory parts of the given file name. The resulting string is the leftmost characters of FileName, up to and including the colon or backslash that separates the path information from the name and extension. The resulting string is empty if FileName contains no drive and directory parts.

See the list of functions for more useful stuff.

In your case, you could put this:

Result := ExtractFilePath(Result);

At the end of your function.

Edit: to be extra explicit:

function GetDirName(const Value: string): string;
var          
  InstallPath: string;
begin
  // initialize default path, which will be returned when the following registry
  // key queries fail due to missing keys or for some different reason
  Result := ExpandConstant('{pf}\LucasArts\Star Wars Battlefront II\RegistryFailed');
  // query the first registry value; if this succeeds, return the obtained value
  if RegQueryStringValue(HKLM, 'SOFTWARE\LucasArts\Star Wars Battlefront II\1.0', 'ExePath', InstallPath) then
    Result := InstallPath  
  // otherwise the first registry key query failed, so...
  else
  // query the second registry value; if it succeeds, return the obtained value
  if RegQueryStringValue(HKLM, 'SOFTWARE\Wow6432Node\LucasArts\Star Wars Battlefront II\1.0', 'ExePath', InstallPath) then
    Result := InstallPath;

  // return only the path
  Result := ExtractFilePath(Result);
end;

Upvotes: 3

Related Questions