I would like to that a cmd command prompt, which maps a network directory to a local path, and then adds that local path to the (local) PATH variable.
I have prepared this kind of .bat script, which runs as expected:
pushd \\NetworkPath\Users\myname\Downloads
chdir
SET CurrentDir="%~dp0"
SET var=%cd% & SET CDIR=`chdir`
for /f "usebackq" %%x in (`chdir`) do set bvar=%%x
SET PATH=%PATH%;%bvar%
@echo IT IS: %CurrentDir%, %var%, %CDIR%, %bvar% ;;; %PATH% --- %CD%
Essentially, this is the script I'd like to run upon the start of new cmd.exe
when I run the .bat
file. The problem is that when a network path is "mounted" (e.g. when pushd
is invoked), it always gets a new and different local drive letter (X:, Y:, Z: ...), so I have to detect what this local drive path equivalent is (e.g. via chdir
) before I can add it to PATH for that session; and also, I'm puzzled why SET PATH=%PATH%;%bvar%
always doubles the semicolon ;
before the appended part, when the final concatenated PATH
is echo
ed.
I've learned that percent sign %
is escaped by doubling it (%%
); that ampersand &
is the separator to concatenate multiple lines of code into a single line, which is escaped with caret ^
, but I'm still puzzled as double quotes "
seem to end up verbatim in either variables via SET, or out of echo
; also the for
command always seems to execute (and I cannot escape it with caret) - even if it is a part of a SET variable statement.
So ultimately, I cannot get this script converted to a one-liner, so I can use it as an argument to start.exe
as startup commands; note that I would not like to have two batch files, I'd like to have only one. I'd like to first store this script in a variable, so I can echo it for debugging - and this is how far I got:
SET TCMD="" pushd \\NetworkPath\Users\myname\Downloads ^& chdir ^& SET CurrentDir="%%~dp0" ^& SET var=^%%cd^%% ^& SET CDIR=^(`chdir`) ^& SET PATH=%%PATH%%;%%CD%% ^& echo %%CurrentDir%%, %%var%%, %%CDIR%%, %%bvar%% ;;; %%PATH%% --- %%CD%% ""
echo %TCMD%
start cmd.exe /k "%TCMD%"
Problem - the new cmd.exe starts with:
'""' is not recognized as an internal or external command,
operable program or batch file.
Then, %CD%
refers to the previous directory (one the .bat script was started from), not to the new one.
None of these retrieve the new drive/path name when ran like this - except maybe for /f "usebackq" %%x in (`chdir`)
, which I cannot really capture (I can see its output), and doesn't seem all that consistent either (sometimes it seems it reports empty).
How can I get my first script escaped, so it can serve as a one-liner in the start
argument of my second script, so I can start cmd.exe
terminal as intended?
References:
- http://ss64.com/nt/syntax-esc.html
- https://stackoverflow.com/questions/9546324/adding-directory-to-path-environment-variable-in-windows
- Can you browse a UNC path using a command line environment without mapping it to a network drive?
- Batch file that runs cmd.exe, a command, and then stays open at prompt
- Execute multiple commands with 1 line in Windows commandline?