Get parent directory name for a particular file using DOS Batch scripting

后端 未结 9 1301
無奈伤痛
無奈伤痛 2020-12-08 21:33

I need to find the name of the parent directory for a file in DOS

for ex.

Suppose this is the directory

C:\\test\\pack\\a.txt
9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 22:15

    It can be very simple to get the parent folder of the batch file:

    @echo off
    for %%a in ("%~dp0\.") do set "parent=%%~nxa"
    echo %parent%
    

    And for a parent of a file path as per the question:

    @echo off
    for %%a in ("c:\test\pack\a.txt") do for %%b in ("%%~dpa\.") do set "parent=%%~nxb"
    echo %parent%
    

提交回复
热议问题