5

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

0

4 Answers 4

7
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%
Sign up to request clarification or add additional context in comments.

Comments

2

try this:

for %%B in (%~dp0\.) do set c=%%~dpB

2 Comments

This only gets the parent directory, not the parent's parent.
easy to fix: for %%B in (%~dp0\..) do set c=%%~dpB - besides that, this is the best answer. AND it answers the questions correctly (from the example in the question, they want the parent, not the grandparent (just bad wording))
2

This works when Alireza's answer is applied.

@echo off

SET CurrentDirectory=%~dp0
echo %CurrentDirectory%
for %%B in (%CurrentDirectory%.) do set parent=%%~dpB
echo %parent%
for %%B in (%parent%.) do set grandparent=%%~dpB
echo %grandparent%

pause

Comments

0
echo thanks
@echo off
SET CurrentDirectory=%~dp0
echo %CurrentDirectory%
for %%B in (%CurrentDirectory%.) do set parent=%%~dpB
echo %parent%
for %%B in (%parent%.) do set grandparent=%%~dpB
echo %grandparent%
start "" msiexec /a %parent%AcroPro.msi

1 Comment

Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.