as shown below, I have a batch file named 'Current Path.bat' being stored in 'C:\\A\\B\\'.
C:\A\B\Current Path.bat
What I want to do is to output the full path of the parent directory / upper directory of the the current directory (ie where the batch file is being stored / executed).
The expected result in this case should be:
C:\A\
Currently I have figured out a work-around for this, but it is not the perfect one. I am wondering if there is a better solution for this.
My work-around for the moment:
@echo off
set current_path=%cd%
echo Current Path: %current_path%
cd ..
set upper_path= %cd%
echo Upper Path: %upper_path%
The result is as below:
C:\>cd a
C:\A>cd b
C:\A\B>"Current Path.bat"
Current Path: C:\A\B
Upper Path: C:\A
There is nothing wrong with your approach; to avoid changing the current directory you can do, for example,
pushd ..
set upper_path=%CD%
popd
If you want to avoid changing directories even for an instant you can use a for
:
for %%x in ("%CD%") do set upper_path=%%~dpx
Which of the two is more readable is your choice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.