windows 从批处理脚本中获取父目录的父目录

xesrikrc  于 2023-06-07  发布在  Windows
关注(0)|答案(4)|浏览(213)

我有脚本位于:C:\A\B\test.bat
在脚本中,我通过命令获取脚本的父目录:parent_dir=~dp0 = >我得到:C:\A\B
如何获取parent dir的parent:C:\A并设置为变量

ejk8hzay

ejk8hzay1#

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

响应注解的完整代码

@ECHO OFF
setlocal
SET parent=%~dp0
ECHO parent=%parent%
FOR %%a IN ("%parent:~0,-1%") DO SET grandparent=%%~dpa
ECHO grandparent=%grandparent%
bnl4lu3b

bnl4lu3b2#

试试这个:

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

s4chpxco3#

当Alireza的答案被应用时,这起作用。

@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
v09wglhw

v09wglhw4#

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

相关问题