0

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 echoed.

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:

1
  • What is the goal? You open a window to open a new one, this is your goal? If that's it, just create a batch opening another with the start command
    – Paul
    Sep 26, 2015 at 20:21

1 Answer 1

2

You are way over thinking this. You can't seem to see the forest for the trees ;-)

First off, "%~dp0" gives the location of your currently executing script, not the current directory.

Secondly, you are already using %CD% to print out the current directory at the end - why not use it when you extend your PATH !?

@echo off
pushd "\\NetworkPath\Users\myname\Downloads"
path %path%;"%cd%"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.