AgainPsychoX
AgainPsychoX

Reputation: 1801

How change PATH env in cmd.exe /c in Windows

I want run something like this:

cmd /c "set ""Path=V:\;%Path%"" & ECHO. %Path% & PAUSE"

But there is a problem: the command line window appears only for a moment, then disappears. I create a test .bat (like echo.1: %1 and 2,3...) to check out how it behave and...

0: "V:\test.bat"
1: /c
2: "set ""Path=V:\;
3: 
(4-9 the same)

When I replace % with for example 5, second argument look like "set ""Path=V:\;5Path5"" & ECHO. 5Path5 & PAUSE", but it is not what I want. I think, there may be problem with parsing %Path% inside this shell command.

I tried aslo:

cmd /c "set ""Path=V:\;%%Path%%"" & ECHO. %%Path%% & PAUSE"
cmd /c "set ""Path=V:\;%%%Path%%%"" & ECHO. %%%Path%%% & PAUSE"
cmd /c "set ""Path=V:\;%%%%Path%%%%"" & ECHO. %%%%Path%%%% & PAUSE"
cmd /c "set ""Path=V:\;%%%%%Path%%%%%"" & ECHO. %%Path%%%%% & PAUSE"
cmd /c "set ""Path=V:\;%%%%%%%%Path%%%%%%%%"" & ECHO. %%%%%%%%Path%%%%%%%% & PAUSE"
cmd /c "set ""Path=V:\;^%Path^%"" &ECHO.^%Path^%&PAUSE"
cmd /c "set ""Path=V:\;^%%Path^%%"" &ECHO.^%Path^%&PAUSE"
cmd /c "set ""Path=V:\;\%Path\%"" &ECHO.\%Path\%&PAUSE"

I will editing post during my next tests...

Edit: I just noticed that I cannot even use simply: cmd /c "echo %PATH% & PAUSE"

Upvotes: 1

Views: 2266

Answers (2)

AgainPsychoX
AgainPsychoX

Reputation: 1801

I come to the solution. What I needed:

cmd /c "set ""Path=V:\;%Path:~0%"" &ECHO.%Path:~0%&PAUSE"

It's look like Windows replace %PATH% with this env-variable content (more paths by semicolons), even in Run (Win+R), CMD (.exe), ShellExec (wscript/winapi). So it can not be used as part of argument in shortcut (i needed to).

Upvotes: 1

Jean-François Fabre
Jean-François Fabre

Reputation: 140186

That doesn't seem to work because xx is evaluated before cmd is started

cmd /c "set xx=yyyyyy & echo %xx% & PAUSE"

That proves that it works, the variable is properly set:

cmd /c "set xx=yyyyyy & set xx & PAUSE"

output:

xx=yyyyyy

Other way to escape the %:

cmd /c "set xx=yyyyyy & ECHOxx.bat & PAUSE"

(ECHOxx.bat is a script which does echo %xx%)

output

yyyyyy

For the path, it's slightly complex as you noticed yourself, but it works fine too:

cmd /c "set PATH=%CD%\subdir;"%PATH%" & echoxx.bat"

it works: echoxx.bat had been moved in the subdir directory, and it is found.

Upvotes: 0

Related Questions