Can't CD to a location stored in a variable

November 15, 2009 at 12:40:02
Specs: Windows XP-SP2
Hello!

CD (or ChDir) fails in my batch files when used with variables.
I want my batch files to use variables to navigate the directory structure relative to a top directory which may change from run to run.
I have the following simplified example:
SET v_WorkDir=
SET V_TopDir=%CD%
then later
SET V_WorkDir=%V_TopDir%/MySubDir
The following line fails ( Error is: The system cannot find the path specified):
CD %V_WorkDir%
To solve this, I tried all combinations I could think of of %, single-quotes, double-quotes - no success.
When I type the command interactively at the C:\ prompt(C:\ CD then the actual path in V_WorkDir), it works fine.
I have already made sure there are NO spaces or strange characters in V_WorkDir, and yes, MySubDir exists as a sub-directory.

What am I doing wrong?
Can this be done? How?

Thanks for any help you can provide.

J. Nicolet


See More: Cant CD to a location stored in a variable

Report •


#1
November 15, 2009 at 14:34:34
Maybe this is a dumb tip. but replace the / (slash) with a \ (backslash), i.e.

SET V_WorkDir=%V_TopDir%/MySubDir

with

SET V_WorkDir=%V_TopDir%\MySubDir


Report •

#2
November 16, 2009 at 04:09:37
Hi!
Nothing dumb about it!
I have already tried that, without success either...
:(

Thanks anyway!!


Report •

#3
November 16, 2009 at 04:47:40
A wild guess, but does your path contain a "!" exclamation mark and do you have delayed expansion enabled?

Can you post exactly what a path that does it is?


Batch Variable how to


Report •

Related Solutions

#4
November 16, 2009 at 05:52:02
Sure!
Here is the directory where testing is taking place.
C:\JRN\2009_2_Fall\COMP-2192-DWH\Assignment_Solutions\ETL
It has 2 sub-directories (so far):
LOG and OLTP_Data
So, V_WorkDir contains C:\JRN\2009_2_Fall\COMP-2192-DWH\Assignment_Solutions\ETL
(I verified this by doing ECHO %V_WorkDir%)
Then the batch file tries:
SET CD %V_WorkDir%
which results in the error mentioned in the initial post (path not found).

Thanks


Report •

#5
November 16, 2009 at 06:07:29
Maybe you wanted to post

CD %V_WorkDir%

instead of

SET CD %V_WorkDir%

sorry to bore you, but precision is the essence of software.


Report •

#6
November 16, 2009 at 07:16:09
SET CD %V_WorkDir%

Won't do much of anything.


=====================================
Helping others achieve escape felicity

M2


Report •

#7
November 16, 2009 at 08:44:49
You are correct!
I was trying to rush to a meeting and I mistyped the code.

The batch file does a
CD %V_WorkDir%
without the SET, which will not work, as you pointed out.

I also had a colleague test my code on his machine (identical hardware & windows version).
The code runs fine on his machine, but not on mine...
I may have some environment variable(s) that are causing this...
I'll look into that now.

Thanks


Report •

#8
November 16, 2009 at 09:00:26
echo %V_WorkDir%

and post result


=====================================
Helping others achieve escape felicity

M2


Report •

#9
November 16, 2009 at 13:24:43
Remove (if any) the @echo off initial statement and insert a pause command after the indicted one.

Examine carefully what is displyed and report if you can't discover anything abnormal. This is the most straightforward way to debug a misterious failure inside a batch script.


Report •

#10
November 17, 2009 at 05:41:55
Remember that the CD command just sets the current working directory of the specified drive and does not change the current drive. So:

C:\> d:
D:\> cd c:\windows
D:\> rem Eh? Why aren't we in C:\Windows? Oh I know...
D:\> c:
C:\Windows> rem Here we are!

Alternatively, do CD /D instead of CD and it will also change the current drive.


Report •

#11
November 17, 2009 at 06:40:49
Thanks for all the info!

I had already tried CD /d without success.
I simplified my files for testing this.
Here are my 2 test files (they reside in directory C:\Btest). The sub-directory C:\btest\DATA exists

File BatchA.bat:
REM =================================================
REM =================================================
REM Initialize variables
SET V_WorkPathTop=
SET V_HomeDir=
REM Define Data sub-directory
SET V_DataDir=Data
SET V_HomeDir=%CD%
REM =================================================
REM Now, call the other batch file with the location of the data files
REM =========
PAUSE
REM Call test #1
call BatchB %V_HomeDir%\%V_DataDir%
PAUSE
REM Call test #2
call batchB C:\btest\data
pause

File BatchB.bat:

PAUSE
CD /d %1%
PAUSE
DIR
PAUSE
ECHO Leaving BatchB

Both tests #1 & #2 in file A fail the CD command with the error "The System cannot find the path specified"

I'll keep digging...


Report •

#12
November 17, 2009 at 06:57:37
CD /d %1%

is wrong: an external variable has just ONE % prefixing the number, i.e.

CD /d %1


Report •

#13
November 17, 2009 at 07:01:51
Removing that second % solved the problem in tha submitted code.

Thanks!


Report •

#14
November 17, 2009 at 08:22:05
This is a good example of why it is essential, when asking for support, to show exactly all of the steps involved. In this case, the issue took a long time to resolve because you hadn't told us you were trying to change directory by calling another batch file, and we were thinking you were calling CD directly.

Report •

#15
November 17, 2009 at 08:25:31
You are right - I screwed up.
Apologies.

Report •


Ask Question