|
| 1 | +[Code] |
| 2 | +
|
| 3 | +// Version log: |
| 4 | +// 03/31/2003: Initial release (thv(at)lr.dk) |
| 5 | +
|
| 6 | +const |
| 7 | + // Modification method |
| 8 | + pmAddToBeginning = $1; // Add dir to beginning of Path |
| 9 | + pmAddToEnd = $2; // Add dir to end of Path |
| 10 | + pmAddAllways = $4; // Add also if specified dir is already included in existing path |
| 11 | + pmAddOnlyIfDirExists = $8; // Add only if specified dir actually exists |
| 12 | + pmRemove = $10; // Remove dir from path |
| 13 | + pmRemoveSubdirsAlso = $20; // Remove dir and all subdirs from path |
| 14 | +
|
| 15 | + // Scope |
| 16 | + psCurrentUser = 1; // Modify path for current user |
| 17 | + psAllUsers = 2; // Modify path for all users |
| 18 | +
|
| 19 | + // Error results |
| 20 | + mpOK = 0; // No errors |
| 21 | + mpMissingRights = -1; // User has insufficient rights |
| 22 | + mpAutoexecNoWriteacc = -2; // Autoexec can not be written (may be readonly) |
| 23 | + mpBothAddAndRemove = -3; // User has specified that dir should both be removed from and added to path |
| 24 | +
|
| 25 | +
|
| 26 | +{ Helper function: Split a path environment variable into individual dirnames } |
| 27 | +function SplitPath(Path: string): TStringList ; |
| 28 | +var |
| 29 | + Pos: Integer; |
| 30 | + S: string; |
| 31 | +begin |
| 32 | + Result := TStringList.Create; |
| 33 | + S := ''; |
| 34 | + for Pos :=1 to Length(Path) do |
| 35 | + begin |
| 36 | + if Path[Pos] <> ';' then |
| 37 | + S := S + Path[Pos]; |
| 38 | + if (Path[Pos] = ';') or (Pos = Length(Path)) then |
| 39 | + begin |
| 40 | + S := Trim(s); |
| 41 | + S := RemoveQuotes(s); |
| 42 | + S := Trim(s); |
| 43 | + if S <> '' then |
| 44 | + Result.Add(S); |
| 45 | + S := ''; |
| 46 | + end; |
| 47 | + end; |
| 48 | +end; // function SplitPath |
| 49 | +
|
| 50 | +
|
| 51 | +{ Helper procedure: Concatenate individual dirnames into a path environment variable } |
| 52 | +function ConcatPath(Dirs: TStringList; Quotes: Boolean): string; |
| 53 | +var |
| 54 | + Index, MaxIndex: Integer; |
| 55 | + S: string; |
| 56 | +begin |
| 57 | + MaxIndex := Dirs.Count-1; |
| 58 | + Result := ''; |
| 59 | + for Index := 0 to MaxIndex do |
| 60 | + begin |
| 61 | + S := Dirs.Strings[Index]; |
| 62 | + if Quotes and (pos(' ', S) > 0) then |
| 63 | + S := AddQuotes(S); |
| 64 | + Result := Result + S; |
| 65 | + if Index < MaxIndex then |
| 66 | + Result := Result + ';' |
| 67 | + end; |
| 68 | +end; // function ConcatPath |
| 69 | +
|
| 70 | +
|
| 71 | +{ Helper function: Modifies path environment string } |
| 72 | +function ModifyPathString(OldPath: string; DirName: string; Method: Integer; Quotes: Boolean): string; |
| 73 | +var |
| 74 | + Dirs: TStringList; |
| 75 | + DirNotInPath: Boolean; |
| 76 | + I: Integer; |
| 77 | +begin |
| 78 | + // Remove quotes form DirName |
| 79 | + DirName := Trim(DirName); |
| 80 | + DirName := RemoveQuotes(DirName); |
| 81 | + DirName := Trim(DirName); |
| 82 | +
|
| 83 | + // Split old path in individual directory names |
| 84 | + Dirs := SplitPath(OldPath); |
| 85 | +
|
| 86 | + // Check if dir is already in path |
| 87 | + DirNotInPath := True; |
| 88 | + for I:=0 to Dirs.Count-1 do |
| 89 | + begin |
| 90 | + if AnsiUpperCase(Dirs.Strings[I]) = AnsiUpperCase(DirName) then |
| 91 | + DirNotInPath := False; |
| 92 | + end; |
| 93 | +
|
| 94 | + // Should dir be removed from existing Path? |
| 95 | + if (Method and (pmRemove or pmRemoveSubdirsAlso)) > 0 then |
| 96 | + begin |
| 97 | + for I:=0 to Dirs.Count-1 do |
| 98 | + begin |
| 99 | + if (((Method and pmRemoveSubdirsAlso) > 0) and (pos(AnsiUpperCase(DirName) + '', AnsiUpperCase(Dirs.Strings[I])) = 1)) or |
| 100 | + (((Method and (pmRemove) or (pmRemoveSubdirsAlso)) > 0) and (AnsiUpperCase(DirName) = AnsiUpperCase(Dirs.Strings[I]))) |
| 101 | + then |
| 102 | + Dirs.Delete(I); |
| 103 | + end; |
| 104 | + end; |
| 105 | +
|
| 106 | + // Should dir be added to existing Path? |
| 107 | + if (Method and (pmAddToBeginning or pmAddToEnd)) > 0 then |
| 108 | + begin |
| 109 | + // Add dir to path |
| 110 | + if ((Method and pmAddAllways) > 0) or DirNotInPath then |
| 111 | + begin |
| 112 | + // Dir is not in path already or should be added anyway |
| 113 | + if ((Method and pmAddOnlyIfDirExists) = 0) or DirExists(DirName) then |
| 114 | + begin |
| 115 | + // Dir actually exists or should be added anyway |
| 116 | + if (Method and pmAddToBeginning) > 0 then |
| 117 | + Dirs.Insert(0, DirName) |
| 118 | + else |
| 119 | + Dirs.Append(DirName); |
| 120 | + end; |
| 121 | + end; |
| 122 | + end; |
| 123 | +
|
| 124 | + // Concatenate directory names into one single path variable |
| 125 | + Result := ConcatPath(Dirs, Quotes); |
| 126 | + // Finally free Dirs object |
| 127 | + Dirs.Free; |
| 128 | +end; // function ModifyPathString |
| 129 | +
|
| 130 | +{ Main function: Modify path } |
| 131 | +function ModifyPath(Path: string; Method: Integer; Scope: Integer): Integer; |
| 132 | +var |
| 133 | + RegRootKey: Integer; |
| 134 | + RegSubKeyName: string; |
| 135 | + RegValueName: string; |
| 136 | + OldPath, ResultPath: string; |
| 137 | + OK: Boolean; |
| 138 | +begin |
| 139 | + // Check if both add and remove has been specified (= error!) |
| 140 | + if (Method and (pmAddToBeginning or pmAddToEnd) and (pmRemove or pmRemoveSubdirsAlso)) > 0 then |
| 141 | + begin |
| 142 | + Result := mpBothAddAndRemove; |
| 143 | + Exit; |
| 144 | + end; |
| 145 | +
|
| 146 | + // Perform directory constant expansion |
| 147 | + Path := ExpandConstantEx(Path, ' ', ' '); |
| 148 | +
|
| 149 | + // Initialize registry key and value names to reflect if changes should be global or local to current user only |
| 150 | + case Scope of |
| 151 | + psCurrentUser: |
| 152 | + begin |
| 153 | + RegRootKey := HKEY_CURRENT_USER; |
| 154 | + RegSubKeyName := 'Environment'; |
| 155 | + RegValueName := 'Path'; |
| 156 | + end; |
| 157 | + psAllUsers: |
| 158 | + begin |
| 159 | + RegRootKey := HKEY_LOCAL_MACHINE; |
| 160 | + RegSubKeyName := 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'; |
| 161 | + RegValueName := 'Path'; |
| 162 | + end; |
| 163 | + end; |
| 164 | +
|
| 165 | + // Read current path value from registry |
| 166 | + OK := RegQueryStringValue(RegRootKey, RegSubKeyName, RegValueName, OldPath); |
| 167 | + if not OK and (Scope = psAllUsers) then |
| 168 | + begin |
| 169 | + Result := mpMissingRights; |
| 170 | + Exit; |
| 171 | + end; |
| 172 | +
|
| 173 | + // Modify path |
| 174 | + ResultPath := ModifyPathString(OldPath, Path, Method, False); |
| 175 | +
|
| 176 | + // Write new path value to registry |
| 177 | + if not RegWriteStringValue(RegRootKey, RegSubKeyName, RegValueName, ResultPath) then |
| 178 | + begin |
| 179 | + Result := mpMissingRights; |
| 180 | + Exit; |
| 181 | + end; |
| 182 | +
|
| 183 | + // Expect everything to be OK |
| 184 | + Result := mpOK; |
| 185 | +end; // ModifyPath |
0 commit comments