Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get parent of parent directory from batch script

I have script located at : C:\A\B\test.bat

In script I get parent directory of script by command : parent_dir=~dp0 = > I got : C:\A\B

So how can I get parent of parent dir : C:\Aand set to a variable

like image 445
Ryo Avatar asked Jul 16 '14 09:07

Ryo


People also ask

How do I navigate to parent folder?

1. Go to the parent folder. (Command + Up Arrow) In Finder, we can press Command + ↑ to access the parent folder of any item or folder.

What is %% P in batch file?

The /P switch allows you to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty.

How do I get to the parent directory in Windows?

To go up one level of the directory tree, type the following: cd .. The special file name, dot dot ( .. ), refers to the directory immediately above the current directory, its parent directory.

What is %% A in CMD?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


1 Answers

FOR %%a IN ("%parent:~0,-1%") DO SET grandparent=%%~dpa

Full code in response to comment

@ECHO OFF
setlocal
SET parent=%~dp0
ECHO parent=%parent%
FOR %%a IN ("%parent:~0,-1%") DO SET grandparent=%%~dpa
ECHO grandparent=%grandparent%
like image 63
Magoo Avatar answered Nov 15 '22 00:11

Magoo