JavaScript: add current folder to %PATH% var , remove trailing \ from %P

English support forum

Moderators: Hacker, petermad, Stefan2, white

Post Reply
KozakMak
Senior Member
Senior Member
Posts: 489
Joined: 2021-05-24, 12:39 UTC
Location: UA

JavaScript: add current folder to %PATH% var , remove trailing \ from %P

Post by *KozakMak »

Hi to all. My task needs to have a button to add the current folder to the path.
For this case, I found this script:
var wsh_shell = WScript.CreateObject("WScript.Shell");
var path = wsh_shell.ExpandEnvironmentStrings(WScript.Arguments.Unnamed(0));

var v = "";
try {
v = wsh_shell.RegRead("HKCU\\Environment\\Path");
}
catch ( e ) {
}

if ( v != "" ) {
for ( var i = new Enumerator(v.split(";")); !i.atEnd(); i.moveNext())
if ( path.toUpperCase() == i.item().toUpperCase() )
WScript.Quit(0);
if ( v.charAt(v.length-1) != ";" )
v += ";";
}
v += path;
if ( wsh_shell.Run("setx Path \"" + v + "\"",0,true) != 0 )
WScript.Echo("setx failed.");
It works, but if do like this:

Code: Select all

cscript //nologo user_path_add.js "%P"
it adds to the end of the path "

The question is how to force %P not to adds \ to the end or how to fix the script?
OS: Win10 | TC: latest x64
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: add current folder to path

Post by *Stefan2 »

KozakMak wrote: 2022-10-11, 07:49 UTC Hi to all. My task needs to have a button to add the current folder to the path.


The question is how to force %P not to adds \ to the end or how to fix the script?

Other way around.
Use your script to do the work:

var wsh_shell = WScript.CreateObject("WScript.Shell");
var path = wsh_shell.ExpandEnvironmentStrings(WScript.Arguments.Unnamed(0));

path = path.slice(0, -1);

var v = "";






TESTing code:
TC-Button
cmd= "d:\rive\path to the\MyScript.js"
param= "%P"

"MyScript.js":
var wsh_shell = WScript.CreateObject("WScript.Shell");
var path = wsh_shell.ExpandEnvironmentStrings(WScript.Arguments.Unnamed(0));
pathNew = path.slice(0, -1);
wsh_shell.Popup(path+'\n'+pathNew);






 
KozakMak
Senior Member
Senior Member
Posts: 489
Joined: 2021-05-24, 12:39 UTC
Location: UA

Re: add current folder to path

Post by *KozakMak »

nice!
OS: Win10 | TC: latest x64
KozakMak
Senior Member
Senior Member
Posts: 489
Joined: 2021-05-24, 12:39 UTC
Location: UA

Re: JavaScript: add current folder to %PATH% var , remove trailing \ from %P

Post by *KozakMak »

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 :shock:
OS: Win10 | TC: latest x64
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: JavaScript: add current folder to %PATH% var , remove trailing \ from %P

Post by *Stefan2 »

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 :shock:


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 :shock:

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



 
KozakMak
Senior Member
Senior Member
Posts: 489
Joined: 2021-05-24, 12:39 UTC
Location: UA

Re: JavaScript: add current folder to %PATH% var , remove trailing \ from %P

Post by *KozakMak »

but also remove the path if already in
Very cool! But Error when env path doesn't exist.

Code: Select all

---------------------------
Windows Script Host
---------------------------
Сценарий:	D:\Program Files\Total Commander\Scripts\folder_to_pathv2.js
Строка:	13
Символ:	1
Ошибка:	'sCurrentValue' - определение отсутствует
Код:	800A1391
Источник: 	Ошибка выполнения Microsoft JScript

---------------------------
ОК   
---------------------------
https://ibb.co/cwXYXLF
OS: Win10 | TC: latest x64
Fla$her
Power Member
Power Member
Posts: 2998
Joined: 2020-01-18, 04:03 UTC

Re: JavaScript: add current folder to %PATH% var , remove trailing \ from %P

Post by *Fla$her »

KozakMak wrote: 2022-10-11, 07:49 UTCThe question is how to force %P not to adds \ to the end or how to fix the script?
Sometimes it's useful to look into the help:
Dialog box: Configuration - Change button bar wrote:Note: All parameters now support substring fields in the form :~start,length, e.g. %N:~2,5 or %N:~-8,5 . To append a number directly after the length value, use another ~ character, e.g. %N:~2,5~2. Negative vaules are measured from the end of the string. Example: %P:~0,-1 cuts off the backslash from the path.
Overquoting is evil! 👎
KozakMak
Senior Member
Senior Member
Posts: 489
Joined: 2021-05-24, 12:39 UTC
Location: UA

Re: JavaScript: add current folder to %PATH% var , remove trailing \ from %P

Post by *KozakMak »

2Fla$her
ha! nice one!

2Stefan2
and a couple more notes:
1) change made through SETX starts working immediately (for new processes, of course), while changes saved through the registry will only start working after the user logs out/logs in
2) can you add msg like "folder add to path", "folder deleted from path"?
OS: Win10 | TC: latest x64
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: JavaScript: add current folder to %PATH% var , remove trailing \ from %P

Post by *Stefan2 »

KozakMak wrote: 2022-10-12, 06:01 UTC But Error when env path doesn't exist.
OK, code adjusted above, we do now catch this error.



Fla$her wrote: 2022-10-12, 06:13 UTC Sometimes it's useful to look into the help:
Example: %P:~0,-1 cuts off the backslash from the path.
Thanks for the reminder, I had thought about that too, but was too lazy to look this up.




Time to check that out at least now:

Code: Select all

TOTALCMD#BAR#DATA
cmd /c
ECHO "%P:~0,-1"|CLIP
C:\windows\system32\cmd.exe
Tooltip - ECHO  "%P:~0,-1" | CLIP

1
-1

Works fine:
"%P" ----------- "C:\temp\Test1\"
"%P:~0,-1" --- "C:\temp\Test1"

Thank you @2ghisler(Author)


But still with trailing line feed





 
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: JavaScript: add current folder to %PATH% var , remove trailing \ from %P

Post by *Stefan2 »

KozakMak wrote: 2022-10-12, 06:48 UTC 2Stefan2
and a couple more notes:
1) change made through SETX starts working immediately (for new processes, of course), while changes saved through the registry will only start working after the user logs out/logs in
I see.
Then you must find a piece of code to escape the %-signs for SETX, because if setx finds %var%, it tries to expand them always.
For DOS command line there it works with ^-sign to escape %-sign, . . . .but with JS , I don't know.
Maybe I could try with replacing % by \xXXX ? ... but setx may write that as literal text
and not get the % back, so setx may be the wrong tool for this task here. Or I lack the right idea.



KozakMak wrote: 2) can you add msg like "folder add to path", "folder deleted from path"?
You can do that yourself by adding
wsh_shell.Popup( ' KozakMak says ' );

EDIT:
Ah, I see , with the code above that would be a bit to much for you , so wait ...

So here is the same code but with message at the end: ( see post above)






 
KozakMak
Senior Member
Senior Member
Posts: 489
Joined: 2021-05-24, 12:39 UTC
Location: UA

Re: JavaScript: add current folder to %PATH% var , remove trailing \ from %P

Post by *KozakMak »

Stefan2 wrote: 2022-10-12, 06:53 UTC I don't know.
2all
who can adapt script (with msg and remove the path if already in) BUT using setx ?
OS: Win10 | TC: latest x64
Fla$her
Power Member
Power Member
Posts: 2998
Joined: 2020-01-18, 04:03 UTC

Re: JavaScript: add current folder to %PATH% var , remove trailing \ from %P

Post by *Fla$her »

Stefan2 wrote: 2022-10-12, 06:48 UTCBut still with trailing line feed
This is not a problem.
Command: %ComSpec% /q/c <nul set
Parameters: /p="%P:~0,-1"|clip

2KozakMak
And why do you need environment variables in PATH at all? :shock:
Overquoting is evil! 👎
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: JavaScript: add current folder to %PATH% var , remove trailing \ from %P

Post by *Stefan2 »

Fla$her wrote: 2022-10-12, 11:19 UTC
Stefan2 wrote: 2022-10-12, 06:48 UTCBut still with trailing line feed
This is not a problem.
Command: %ComSpec% /q/c <nul set
Parameters: /p="%P:~0,-1"|clip
Yes, but depends on how you want to use the %P (on the fly or via script) and may not work on all OSes.



Fla$her wrote: 2KozakMak
And why do you need environment variables in PATH at all? :shock:
Because his script first reads the registry value, and that may already contain %env_vars%
(if that will make sense or not, think company environment)





 
Fla$her
Power Member
Power Member
Posts: 2998
Joined: 2020-01-18, 04:03 UTC

Re: JavaScript: add current folder to %PATH% var , remove trailing \ from %P

Post by *Fla$her »

Stefan2 wrote: 2022-10-12, 12:23 UTCYes, but depends on how you want to use the %P (on the fly or via script) and may not work on all OSes.
What exactly might not work on other OSes? The echo problem is not the problem of %P and where the variable will be useful.
Stefan2 wrote: 2022-10-12, 12:23 UTCBecause his script first reads the registry value, and that may already contain %env_vars%
And why read the registry value, which may not be there, if it's easier to immediately read the value of the environment variable, which is always there?
And this does not explain the need to write another variable to PATH, because it will not be read by the system. It will not work from any location to launch the application by name without writing the absolute path to PATH.
Overquoting is evil! 👎
Post Reply