Next Generation Emulation banner
1 - 4 of 4 Posts

· Registered
Joined
·
412 Posts
Discussion Starter · #1 · (Edited)
[CMD, WMIC, HKLM, HKCU, SETX, PATH, 2001]

the problem is probably more than 20 years old:

- setx Path "%Path%;NewPath" pollutes the user path with the system path.

- setx Path "%Path%;NewPath" /M pollutes the system path with the user path.

- the pollution: 2022-03-14_09-02-33-CMD-PATH-x264-faststart

I am not sure if there is a safe native solution that contemplate NON-ANSI directories making exclusive use of the command prompt.

this is my attempt!

- using Microsoft Windows [Version 10.0.19042.868]

0 - check the paths

Code:
echo %Path%
reg query "HKCU\Environment" /v Path
reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path
reg EXPORT "HKCU\Environment" UserEnvironment.reg /y
reg EXPORT "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" SystemEnvironment.reg /y
wmic ENVIRONMENT where "username='<SYSTEM>' AND name='Path'" get  variablevalue
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get  variablevalue
tip: wmic ENVIRONMENT get * [to see more]

info: a registry backup was performed using the above commands. the files have been saved in the current directory.

1 - backup user path at %USERPATH%

- the commands pre-suppose
  • - non-null path;
  • - functional path;
- if null or unfunctional path do only step "2"

Code:
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get  variablevalue | more +1 > %temp%\userpathvalue.txt
info: there is some pollution* at the end of the string.

Code:
set /P USERPATH=<%temp%\userpathvalue.txt
- add '/' to better see spaces

Code:
set "USERPATH=%USERPATH%/"
info: Naming Files, Paths, and Namespaces - Win32 apps

- remove up to 15 end spaces, then remove '/'

Code:
set "USERPATH=%USERPATH:        /=/%"
set "USERPATH=%USERPATH:    /=/%"
set "USERPATH=%USERPATH:  /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH:/=%"
info: the number of spaces introduced was NOT consistent in my tests.

Code:
echo %USERPATH%[checking final string]
setx USERPATH "%USERPATH%"
* when dealing with invisible characters it is easier to identify them by converting them to hexadecimal, do:

Code:
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get  variablevalue | more +1 > %tmp%\0 && set /P TEMPREADER=<%tmp%\0
echo %TEMPREADER% >%tmp%\1 && echo %TEMPREADER%/ && certutil -f -encodehex "%tmp%\1" "%tmp%\2" 11 | more +4 && type "%tmp%\2"
REM to open on notepad, type: certutil -f -encodehex "%tmp%\1" "%tmp%\2" 5 &&  cmd /C START "" /MAX notepad %tmp%\2
tip: use 4, 5, 10, 11 (same as none) in encodehex

see: CryptBinaryToStringA function (wincrypt.h) - Win32 apps

2 - set¹²³ user path as %USERPATH% only

Code:
setx PATH ^%USERPATH^%
done!

¹ info: setx PATH "%USERPATH%" sets the variable value instead of the literal %USERPATH%

² info: using the literal is better, because the behavior becomes dynamic instead of static. i.e., by using the literal instead of the value, only the %USERPATH% variable needs to be changed in the future. whereas, by using the value (i.e., the addresses present inside the variable), the %PATH% user variable must also be updated upon change in %USERPATH% [which is not desirable].

³ info: below methods NOT recommended! it works, but it is easier to make mistakes.

Code:
reg add "HKEY_CURRENT_USER\Environment" /v PATH /d ^%USERPATH^% /f
wmic ENVIRONMENT set name="PATH", variablevalue=^%USERPATH^%, username="%COMPUTERNAME%\\%USERNAME%"
3 - for now on just use %USERPATH%

Code:
setx USERPATH "%USERPATH%;ENTER-NEW-PATH-HERE"
tip: close all programs, especially terminals, before continuing.

4 - re-check

Code:
echo %Path%
reg query "HKCU\Environment" /v Path
reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path
wmic ENVIRONMENT where "username='<SYSTEM>' AND name='path'" get  variablevalue
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='path'" get  variablevalue
5 - visual check

Code:
rundll32 sysdm.cpl,EditEnvironmentVariables
info: the system PATH can only be changed if the previous command was launched with administrative privilege;

6 - good sources

"Microsoft - Windows Commands"
Windows commands

"SS64 - Windows CMD Shell How-to guides and examples"
Windows CMD Command Syntax - SS64.com

DosTips - The DOS Batch Guide
DosTips - The DOS Batch Guide

"Rob van der Woude's Scripting Page"
Rob van der Woude's Scripting Pages

"TLDP - Appendix N. Converting DOS Batch Files to Shell Scripts"
https://tldp.org/LDP/abs/html/dosbatch.html

"Stack Overflow - cmd"
https://stackoverflow.com/questions/tagged/cmd

"Super User - batch"
https://superuser.com/questions/tagged/batch

Dave Benham - dbenham

Jan Erik - jeb

7 - official sound tracks

Arnaldo Baptista - Cê Tá Pensando Que Eu Sou Loki? (1974)

Ivan Lins - Dinorah, Dinorah (1977)

Bruce Springsteen - Adam Raised a Cain (1978)

8 - TL;DR: do it! / or don't!

Code:
REM the commands pre-suppose
REM - non-null path;
REM - functional path;
REM if null or unfunctionl path don't progress!

reg query "HKCU\Environment" /v Path
reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path

reg EXPORT "HKCU\Environment" UserEnvironment.reg /y
reg EXPORT "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" SystemEnvironment.reg /y

wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get  variablevalue | more +1 > %temp%\userpathvalue.txt
set /P USERPATH=<%temp%\userpathvalue.txt

set "USERPATH=%USERPATH%/"

set "USERPATH=%USERPATH:        /=/%"
set "USERPATH=%USERPATH:    /=/%"
set "USERPATH=%USERPATH:  /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH:/=%"

echo %USERPATH%[checking final string] && echo %USERPATH% > %tmp%\0
certutil -f -encodehex "%tmp%\0" "%tmp%\1" 11 | more +4 && type "%tmp%\1"

setx USERPATH "%USERPATH%"

setx PATH ^%USERPATH^%

echo done!

REM setx USERPATH "%USERPATH%;ENTER-NEW-PATH-HERE"

REM please close all programs
cheers!
 

· No sir, I don't like it.
Joined
·
7,022 Posts
Code:
::Get currently logged in profile PATH and only save first line of output to variable "userpath".

@echo off

set "userpath="
for /f "skip=1 tokens=*" %%a in ('wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get  variablevalue') do (
    if not defined userpath set "userpath=%%a"
)
echo.
echo %userpath%
echo.
pause
Copy-pasta the above into a blank .bat (DOS) or .cmd (Windows) batch file.

Should get output similar to this:

Code:
C:\Users\nfinite.recursion\Desktop>test

%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;C:\Users\nfinite.recursion\AppData\Local\GitHubDesktop\bin

Press any key to continue . . .
This is just to simplify getting the user's path variable more reliably, btw. Generally speaking, you should avoid using locale-specific characters for Windows file and directory names.

Windows CMD also has a Unicode mode that can be invoked with 'cmd /u'. This allows for the special characters not typically found in ANSI or ASCII character encodings. Not 100% sure if it would be helpful in this case as some CMD commands and applications don't work properly in this mode.
 

· ...
Joined
·
398 Posts
Too late =P
Code:
for /f "tokens=2 delims=[]" %%a in ('ver') do set wv=%%a
for /f "tokens=2 delims=. " %%a in ('echo %wv%') do set wv=%%a
if not defined LOCALAPPDATA goto vb
reg query HKCU\Environment /v Path>nul 2>&1
if errorlevel 1 goto fix
reg query HKCU\Environment /v Path 2>&1 | find "ffmpeg" >nul
if errorlevel 1 goto fix
exit
:fix
reg add HKCU\Environment /v Path /d "%ProgramFiles%\XScript" /f
notepad "%ProgramFiles%\XScript\path.txt"
if %wv% GEQ 6 (
    rundll32 sysdm.cpl,EditEnvironmentVariables
) else (
    rundll32 %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\sysdm.cpl,,3
)
exit
:vb
echo WScript.CreateObject("WScript.Shell").Environment("VOLATILE").Item("LOCALAPPDATA") = WScript.CreateObject("Shell.Application").NameSpace(28).Self.Path> "%TEMP%\lapp.vbs"
cscript //nologo "%TEMP%\lapp.vbs"
ping -n 3 127.0.0.1 >nul
del /f /q "%TEMP%\lapp.vbs"
echo Oops, try again...
pause
exit
It's good to know about the existence of setx, but for my particular case, it is not useful to me. I like to use dynamic variables over fixed variables and limit everything to user space =P
 
1 - 4 of 4 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top