mirror of
https://github.com/fosrl/olm.git
synced 2026-02-28 15:56:43 +00:00
Refactor PATH removal to use TStringList for more robust parsing
Co-authored-by: oschwartz10612 <4999704+oschwartz10612@users.noreply.github.com>
Former-commit-id: 91f0230d21
This commit is contained in:
71
olm.iss
71
olm.iss
@@ -91,9 +91,8 @@ procedure RemovePathEntry(PathToRemove: string);
|
|||||||
var
|
var
|
||||||
OrigPath: string;
|
OrigPath: string;
|
||||||
NewPath: string;
|
NewPath: string;
|
||||||
P: Integer;
|
PathList: TStringList;
|
||||||
UpperOrigPath: string;
|
I: Integer;
|
||||||
UpperPathToRemove: string;
|
|
||||||
begin
|
begin
|
||||||
if not RegQueryStringValue(HKEY_LOCAL_MACHINE,
|
if not RegQueryStringValue(HKEY_LOCAL_MACHINE,
|
||||||
'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
|
'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
|
||||||
@@ -103,44 +102,38 @@ begin
|
|||||||
exit;
|
exit;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Prepare for case-insensitive search
|
// Create a string list to parse the PATH entries
|
||||||
UpperOrigPath := ';' + UpperCase(OrigPath) + ';';
|
PathList := TStringList.Create;
|
||||||
UpperPathToRemove := ';' + UpperCase(PathToRemove) + ';';
|
try
|
||||||
|
// Split the PATH by semicolons
|
||||||
|
PathList.Delimiter := ';';
|
||||||
|
PathList.StrictDelimiter := True;
|
||||||
|
PathList.DelimitedText := OrigPath;
|
||||||
|
|
||||||
// Check if the path exists in PATH
|
// Find and remove the matching entry (case-insensitive)
|
||||||
P := Pos(UpperPathToRemove, UpperOrigPath);
|
for I := PathList.Count - 1 downto 0 do
|
||||||
if P = 0 then
|
begin
|
||||||
begin
|
if CompareText(Trim(PathList[I]), Trim(PathToRemove)) = 0 then
|
||||||
// Path not found, nothing to remove
|
begin
|
||||||
exit;
|
Log('Found and removing PATH entry: ' + PathList[I]);
|
||||||
|
PathList.Delete(I);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Reconstruct the PATH
|
||||||
|
NewPath := PathList.DelimitedText;
|
||||||
|
|
||||||
|
// Write the new PATH back to the registry
|
||||||
|
if RegWriteExpandStringValue(HKEY_LOCAL_MACHINE,
|
||||||
|
'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
|
||||||
|
'Path', NewPath)
|
||||||
|
then
|
||||||
|
Log('Successfully removed path entry: ' + PathToRemove)
|
||||||
|
else
|
||||||
|
Log('Failed to write modified PATH to registry');
|
||||||
|
finally
|
||||||
|
PathList.Free;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Remove the path entry from OrigPath
|
|
||||||
// We need to handle the actual string with proper casing
|
|
||||||
NewPath := ';' + OrigPath + ';';
|
|
||||||
|
|
||||||
// Find and remove the entry (case-insensitive search but preserve original casing in other entries)
|
|
||||||
// We search for the pattern in the upper-case version but remove from the original
|
|
||||||
Delete(NewPath, P, Length(PathToRemove) + 1); // +1 for the semicolon
|
|
||||||
|
|
||||||
// Clean up: remove leading and trailing semicolons, and reduce multiple semicolons to one
|
|
||||||
while (Length(NewPath) > 0) and (NewPath[1] = ';') do
|
|
||||||
Delete(NewPath, 1, 1);
|
|
||||||
while (Length(NewPath) > 0) and (NewPath[Length(NewPath)] = ';') do
|
|
||||||
Delete(NewPath, Length(NewPath), 1);
|
|
||||||
|
|
||||||
// Replace multiple semicolons with single semicolon
|
|
||||||
while Pos(';;', NewPath) > 0 do
|
|
||||||
StringChangeEx(NewPath, ';;', ';', True);
|
|
||||||
|
|
||||||
// Write the new PATH back to the registry
|
|
||||||
if RegWriteExpandStringValue(HKEY_LOCAL_MACHINE,
|
|
||||||
'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
|
|
||||||
'Path', NewPath)
|
|
||||||
then
|
|
||||||
Log('Successfully removed path entry: ' + PathToRemove)
|
|
||||||
else
|
|
||||||
Log('Failed to write modified PATH to registry');
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
|
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
|
||||||
|
|||||||
Reference in New Issue
Block a user