KozakMak wrote: 2022-10-11, 10:50 UTC
hm...
when using
usercmd.ini its truncated result to "D:\Progra" - why?
Code: Select all
[em_folderpath]
cmd="%COMMANDER_PATH%\Scripts\folder_to_path.js"
param="%P"
...and one more thing. If the variable contains, for example,
%SystemRoot%\system32 after adding new path it becomes
C:\Windows\system32
1) since TC is already there, you can remove "%COMMANDER_PATH%\" here, but it doesn't matter either:
[em_folderpath]
cmd="Scripts\folder_to_path.js"
param="%P"
2)when using usercmd.ini its truncated result to "D:\Progra" - why?
I don't know, the script works for me with spaces. Do you have set %P really in quotes? >>> "%P"
3)If the variable contains, for example, %SystemRoot%\system32 after adding new path it becomes C:\Windows\system32
Yes, SETX expand the env vars.
Maybe try this adjusted JavaScript for your issue:
Code: Select all
wsh_shell = WScript.CreateObject("WScript.Shell");
sRegEntry = "HKCU\\Environment\\Path";
sArgument = wsh_shell.ExpandEnvironmentStrings(WScript.Arguments.Unnamed(0));
sPathToAdd = sArgument.slice(0, -1);
try {
sCurrentValue = wsh_shell.RegRead(sRegEntry);
}
catch ( e ) {
}
try {
sRegValue = sCurrentValue + ';'+ sPathToAdd;
wsh_shell.RegWrite(sRegEntry, sRegValue, "REG_EXPAND_SZ");
}
catch ( e ) {
wsh_shell.Popup('ERROR, '+sRegEntry+' not found or something else.');
}
Feedback from user KozakMak
change made through SETX starts working immediately (for new processes, of course),
while changes saved through the registry (as this scripts here)
will only start working after the user logs out/logs in
- - -
And here the same but also remove the path if already in users %path% var:
Code: Select all
wsh_shell = WScript.CreateObject("WScript.Shell");
sRegEntry = "HKCU\\Environment\\Path";
sArgument = wsh_shell.ExpandEnvironmentStrings(WScript.Arguments.Unnamed(0));
sPathToAdd = sArgument.slice(0, -1);
sPathToAdd = ";"+sPathToAdd
try {
sCurrentValue = wsh_shell.RegRead(sRegEntry);
}
catch ( e ) {
}
try {
if(sCurrentValue.indexOf(sPathToAdd) !== -1){
sRegValue = sCurrentValue.replace(sPathToAdd,"");
}else{
sRegValue = sCurrentValue + sPathToAdd;
}
wsh_shell.RegWrite(sRegEntry, sRegValue, "REG_EXPAND_SZ");
}
catch ( e ) {
wsh_shell.Popup('ERROR, '+sRegEntry+' not found or something else.');
}
And here is the same code again, with also removing the path if already there, but now with a message at the end:
Code: Select all
wsh_shell = WScript.CreateObject("WScript.Shell");
sRegEntry = "HKCU\\Environment\\Path";
sArgument = wsh_shell.ExpandEnvironmentStrings(WScript.Arguments.Unnamed(0));
sPathToAdd = sArgument.slice(0, -1);
sPathToAdd = ";"+sPathToAdd
try {
sCurrentValue = wsh_shell.RegRead(sRegEntry);
}
catch ( e ) {
}
try {
if(sCurrentValue.indexOf(sPathToAdd) !== -1){
sRegValue = sCurrentValue.replace(sPathToAdd,"");
sMessage = "Folder removed\n\nsPathToAdd";
}else{
sRegValue = sCurrentValue + sPathToAdd;
sMessage = "Folder added\n\nsPathToAdd";
}
wsh_shell.RegWrite(sRegEntry, sRegValue, "REG_EXPAND_SZ");
}
catch ( e ) {
wsh_shell.Popup('ERROR, '+sRegEntry+' not found or something else.');
// break or exit or return? or die(); ?
return;
}
wsh_shell.Popup( sMessage );
Edit:
scripts adjusted with try..catch
Edit2:
scripts adjusted with message at the end