Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to list the directory name (only name, not full path) and sub directories (only name, not full path) inside it. There is only one level of sub directories. I used below script to do this:

@echo off&setlocal enableextensions

for /r  "PATH" %%G in (.) do (
Pushd %%G
for %%* in (.) do set MyDir=%%~n*
echo %MyDir% 
endlocal&goto :eof

The problem with the above script is that it is showing root directory with the full path. How can I just make it show the directories in relation to the one I am working in?

share|improve this question
up vote 1 down vote accepted

Taking you at your word that you only want one level of child folders:

@echo off
for %%A in ("yourRootPath\*") do for /d %%B in ("%%~A\*") do echo %%~nxA\%%~nxB

If your root path is passed in as a batch parameter then the solution is even easier

@echo off
for /d %%B in ("%~1\*") do echo %~nx1\%%~nxB
share|improve this answer
Now I am Trying to rename files inside subfolders using---for %%A in ("Mypath") do for /d %%B in ("%%~A*") do ( cd %%~nxA\%%~nxB FOR %%v IN (A20*.*) DO REN "%%v" "%%~nxB_%%v" )---.But renaming of files in only one sub folder is possible. How can I do it in each sub folder. – shekhar Dec 18 '12 at 6:47

You can use:

for %f in ("C:\Some\Path\Like\This") do echo "%~nf"

to display only:


If it is the root like this:

for %f in ("C:\") do echo "%~nf"

it will display nothing like:


But you said:

problem with the above script is it showing root directory wit full path.

Do you mean, for example, when run at the root of the drive it will display:


What is it actually displaying and what do you want to display in case of the root ? I guess it should be nothing (empty), but that may not be what you want.

Let me know what you want for this case and I will see if I can fix your batch file.


I guess what you mean by "root" is whatever directory you are starting your search from. I thought you meant you were running the batch file in the drive-root like "C:\".

This batch file should do what you want...

If you save it to a file named:


Then you can run it like this:

getdirname.cmd "C:\Some\PATH"
getdirname.cmd C:\Some\PATH

if you run it without any arguments like this:


it will use the current directory as the starting point.

Here is the batch file:

@echo off

set "zstartpath=%~1"
if "%zstartpath%."=="." set "zstartpath=."

for %%f in ("%zstartpath%") do set "zfullpath=%%~dpnxf\"

for /r "%zfullpath%" %%f in (.) do call :work "%%~f"

set "zdfname="
set "zdp="
set "zdpath="
set "zfullpath="
set "zstartpath="
set "ztarget="

goto :EOF


set "ztarget=%~1"

for %%g in ("%ztarget%") do set "zdpath=%%~dpnxg"

for %%g in ("%zdpath%") do set "zdfname=%%~nxg"

echo "%zdfname%"

goto :EOF

I have a directory structure like:


If I am logged into (CD) the path:


and I run getdirname like any of:

getdirname C:\Some\PATH
getdirname C:.\Path
getdirname C:Path
getdirname .\Path
getdirname Path

the output will look like:


If I run getdirname like:

getdirname C:
getdirname .
getdirname .\

the output will look like:


If I run getdirname like:

getdirname \
getdirname C:\

and run the batch file, the output will look like:


Let me know if this is doing what you need, or if it needs any "adjustments".

share|improve this answer
To display presently working directory we can use for %%* in (.) do set MyDir=%%~n* .But I want to list sub directories as well. That is why I am recursively accessing the sub dirs using for /r. – shekhar Dec 11 '12 at 8:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.