user1964974
user1964974

Reputation: 41

Batch: Get the full path of the folder in the previous directory?

I am trying to get the path of a folder that is one level up in the hierarchy of a directory and set it to a variable.

Now I have:

set LOCALFOLDER=%project.root%\builds\%BUILDFOLDER%

where %project.root% is the full path of of project directory (in batch string?).

Let's just say %project.root% is "C:\Hardware\project".

I would like to go one level back the %project.root% (i.e. C:\Hardwre) and store that path in a variable. Is there a way?

Upvotes: 2

Views: 2832

Answers (2)

Joey
Joey

Reputation: 354496

Path manipulation gets a little messy. One option is the following:

set "project.root=C:\Hardware\project"
set "X=%project.root%"
:l
if "%X:~-1%"=="\" goto al
set "X=%X:~0,-1%"
goto l
:al
set "X=%X:~0,-1%"
set "project.parent=%X%"
echo %project.parent%

which successively removes the last character until a \ is removed.

If you're dealing with actual paths in your file system I'd say you use the pushd approach:

pushd %project.root%
cd ..
set project.parent=%CD%
popd

This temporarily sets your current directory, stores the path and returns.

Upvotes: 4

Jayden
Jayden

Reputation: 1

This is quite simple:

To get the current directory in a variable simply say set /a VARIABLE=cd To go back a level say: cd ..

now if you say "set /a VARIABLE=cd" It is set to the previous directory

Upvotes: -1

Related Questions