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:\A
and set to a variable
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.
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.
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.
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.
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%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With