1

I have the following command which will loop over all the subdirectories in a specific location and output the full path:

for /d %i in ("E:\Test\*") do echo %i

Will give me:

E:\Test\One
E:\Test\Two

But how do I get both the full path, and just the directory name, so the do command might be something like:

echo %i - %j

And the output might be something like:

E:\Test\One - One
E:\Test\Two - Two

Thanks in advance!

Jack Sleight
  • 17,010
  • 6
  • 41
  • 55

2 Answers2

4

The following command syntax can be used to return the full path or directory name only:

%~fI        - expands %I to a fully qualified path name
%~nI        - expands %I to a file name only

Using your example, the following command will list directories in the format that you specified:

for /d %i in ("E:\Test*") do echo %~fi - %~ni
Craig Lebakken
  • 1,853
  • 15
  • 10
0

You can use "%~ni". This is an enhanced substitution that will return the file name of a path (or, more accurately, the last part, which is the directory name in your case):

for /d %i in ("E:\Test\*") do echo %i - %~ni

See also this question: What does %~d0 mean in a Windows batch file?

Community
  • 1
  • 1
efotinis
  • 14,565
  • 6
  • 31
  • 36