1

I need to find the folder name of the batch file that I am executing. All the other approaches provide me with the full path. I only need the parent folder name. Also, the other mentioned approaches use looping, which is quite lengthy and verbose for my needs. Is there any method for finding out the parent folder name directly through the % parameters(or their combinations) ?

Community
  • 1
  • 1
Sumit Bisht
  • 1,507
  • 1
  • 16
  • 31
  • possible duplicate of [.bat current folder name](http://stackoverflow.com/questions/3848597/bat-current-folder-name) – Ken White Jun 07 '12 at 12:31
  • That question has an accepted answer that was provided with iteration. I am looking for a parameter/any other approach that is not lengthy – Sumit Bisht Jun 07 '12 at 12:41
  • Look at the second answer, however. (The accepted answer often is not the only useful information.) – Ken White Jun 07 '12 at 13:14
  • I had looked for all the posts/answers(the second answer you are mentioning has come up here also). I'll reiterate my query; I need the parent folder, not the parent path of the bat file – Sumit Bisht Jun 07 '12 at 13:37

3 Answers3

2

The direct answer to your question is "No". There is no direct method using only expansion modifiers without using a FOR loop or CALL.

The referenced thread in your question pretty much has everything you need.

Here are two optimized techniques that are very similar to what has already been posted. Method1 should be the fastest:

Edit - I further optimized by removing the substring operation.
Edit2 - I substituted simple FOR in place of FOR /F

@echo off
setlocal

::method 1 using FOR
for %%F in ("%~p0\.") do set "method1=%%~nxF"
if not defined method1 set method1=\

::method 2 using CALL
call :getName "%~p0\." method2

::print results
set method
exit /b

:getName  PathWithTrailing\.  ReturnVar
set "%~2=%~nx1"
if not defined %~2 set %~2=\
exit /b
dbenham
  • 127,446
  • 28
  • 251
  • 390
1

Try

echo %~dp0 

in your bat file. %0 is the name of the batch file. ~dp gives you the drive and path of the specified argument.

David Brabant
  • 41,623
  • 16
  • 83
  • 111
  • No, that has been mentioned before. While doing this, I get the entire path. What I need is only the parent folder. eg: C:\myprogram\bin\myfile.bat => outputs bin – Sumit Bisht Jun 07 '12 at 12:29
1

There really isn't an easy way to do it. I know you mentioned that you didn't want a lengthy looping approach, so here's a short looping approach:

for %%D in (.) do @echo %%~nD

Note that this code only works inside of a batch file. Outside of it, you should use % instead of %%.

You should check out Rob van der Woude's scripting pages. He has a lot of good info for DOS scripting on his site.

Aaron
  • 55,518
  • 11
  • 116
  • 132