2

I know I can use the following code to get the current directory name.

for %%* in (.) do echo %%~nx*

But that won't work if I have this setup.

folderA
    a.bat
    folderB
        b.bat

Inside b.bat, I have:

%~dp0..\a.bat

in a.bat, I have:

for %%* in (.) do echo %%~nx*

But the output is folderB probably because when b.bat is executing, it's executing in the sub folder's context. But I want a.bat to output its own directory name which is folderA.

How to do that?

Ray Cheng
  • 12,230
  • 14
  • 74
  • 137

2 Answers2

2

in a.bat, use

for /f %%q in ("%~dp0.") do echo %%~nxq
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • btw - with your structure description, you are executing `a.bat` from `b.bat`, so there is no return from `a.bt` to `b.bat`. If you were `call`ing `a.bat` as per your description, you'd need to `call %~dp0..\a.bat` (and you omitted the `~` from your posting.) – Magoo Dec 16 '16 at 01:53
2

There is the environment variable CD which always has the path of current directory. Run in a command prompt window set /? to get displayed on several pages the help of this command listing at end also the dynamic environment variables like CD, DATE or TIME which can't be seen on running just set which outputs all currently defined static environment variables.

The current directory can be equal the directory of the started batch file which is usually the case on double clicking on a batch file.

But the current directory can be also any other directory different to directory of the batch file. For example the current directory is %SystemRoot%\System32 on running a batch file as administrator or as scheduled task with system account or %SystemRoot% on double clicking on a batch file on a network resource accessed using a UNC path.

Let us assume there is C:\folderA\a.bat with the command lines:

@echo off
echo Running %~nx0
echo/
echo Current directory is:  %CD%
echo Directory of %~nx0 is: %~dp0

And there is C:\folderA\folderB\b.bat with the command lines:

@echo off
echo Running %~nx0
echo/
echo Current directory is:  %CD%
echo Directory of %~nx0 is: %~dp0
echo/
call "%~dp0..\a.bat"
echo/
echo Running again %~nx0
echo/
echo Current directory is:  %CD%
echo Directory of %~nx0 is: %~dp0
echo/
for /F %%I in ("%~dp0..\") do echo Batch parent directory is:   %%~dpI
for /F %%I in ("%CD%\..\") do echo Current parent directory is: %%~dpI

Double clicking on batch file C:\folderA\folderB\b.bat results in output:

Running b.bat

Current directory is:  C:\folderA\folderB
Directory of b.bat is: C:\folderA\folderB\

Running a.bat

Current directory is:  C:\folderA\folderB
Directory of a.bat is: C:\folderA\

Running again b.bat

Current directory is:  C:\folderA\folderB
Directory of b.bat is: C:\folderA\folderB\

Batch parent directory is:   C:\folderA\
Current parent directory is: C:\folderA\

Removing the command call in line 7 of b.bat and running again b.bat results in output:

Running b.bat

Current directory is:  C:\folderA\folderB
Directory of b.bat is: C:\folderA\folderB\

Running a.bat

Current directory is:  C:\folderA\folderB
Directory of a.bat is: C:\folderA\

There is no return to b.bat on reaching end of a.bat without using call.

Next let us look on output on running b.bat having again call on line 7 from C:\Windows\System32 with the command line C:\folderA\folderB\b.bat:

Running b.bat

Current directory is:  C:\Windows\system32
Directory of b.bat is: C:\folderA\folderB\

Running a.bat

Current directory is:  C:\Windows\system32
Directory of a.bat is: C:\folderA\

Running again b.bat

Current directory is:  C:\Windows\system32
Directory of b.bat is: C:\folderA\folderB\

Batch parent directory is:   C:\folderA\
Current parent directory is: C:\Windows\

ATTENTION:

Path of current directory hold by environment variable CD is without backslash at end with one exception: the current directory is the root directory of a drive. In this case CD is for example C:\ instead of just C:.

When working with %~dp0 please take into account:
What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory?

Mofi
  • 46,139
  • 17
  • 80
  • 143