amuser
amuser

Reputation: 841

Get the name of parent directory in batch script

Suppose my directory structure is ..

C:\Program Files\abc\myscript.bat

so from myscript.bat i want to get the parent name of this script which is abc in this case.

Please note that I don't want complete path of parent, i want only parent name like abc.

what is the simple way to achieve this?

Upvotes: 2

Views: 2287

Answers (1)

MC ND
MC ND

Reputation: 70923

Probably the simplest code could be

for %%a in ("%~p0.") do echo(%%~nxa

But this code has a point of failure, since you have not defined what to do when the batch file is located at the root of a drive, that is, what to do when the folder containing the batch file does not have a name.

Upvotes: 5

Related Questions