Set the directory above to a Variable

Microsoft Windows xp professional w/serv...
February 11, 2010 at 09:48:37
Specs: Windows XP
How do I set the directory above the current directory to a variable? The following code works, but isn't there a simpler / straight forward way to do this? I actually need to set it to two directories above.

@echo off
FOR %%A IN (..) DO (
SET BUILD_DIR=%%~dpnA
)
ECHO %BUILD_DIR%


See More: Set the directory above to a Variable

Report •


#1
February 11, 2010 at 10:06:34
"The following code works, but isn't there a simpler / straight forward way to do this? I actually need to set it to two directories above."

It really depends on how you intend to use it, if you need the full path the for loop is probably the easiest way to go.

Since it's two directories up you could use:

..\..

You can either put that in the for loop or depending on the situation you may be able to use it as is instead of the variable. Most(if not all) commands support it in place of a full path.

Edit:

Forgot to mention you can also reference files in the dir by tacking them on the end:

type ..\..\2dirsback.txt


Report •

#2
February 11, 2010 at 10:13:55
yeah, i need the full path. and have to use it in multiple places in the script. so, i will go with the for loop. thanks judago.

Report •

#3
February 12, 2010 at 02:37:15
Hi ZacharyTaylor,

Weren't you President a while back?

:(

Nothinmg wrong with a for loop. This should do you, also.

==================================

@echo off & setLocal EnableDELAYedeXpansion & pushd .. & set var=!CD! & popd & echo !var!


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

M2


Report •

Related Solutions

#4
February 12, 2010 at 02:52:07
Note that if using SETLOCAL, the script wont work as a standalone script (if you decide it to use that way), since he would set the variable only inside the script, then lose it. So, based on on the already improved code of M2, here's an alternative:

@echo off
pushd ..
set var=%CD%
popd
echo %var%


Report •

Ask Question