3

By using:

dir /s /b /o:n /a:d > foldername.txt

I get the following output:

D:\Project\Java\MyName
D:\Project\Java\Object

But I want the output to look like this:

MyName
Object  

The output have to be folder names without their paths?

paulcab
  • 1,102
  • 1
  • 9
  • 16
  • 1
    Possible duplicate of [How to split the filename from a full path in batch?](http://stackoverflow.com/questions/9252980/how-to-split-the-filename-from-a-full-path-in-batch) – Jonny Henly Mar 24 '16 at 07:03
  • Your desired output cannot be achieved by solely using `dir`. – Jonny Henly Mar 24 '16 at 07:18
  • what to do for getting the desired result? – paulcab Mar 24 '16 at 07:43
  • Did you take a look at the link in my first comment? – Jonny Henly Mar 24 '16 at 07:45
  • i looked the link and it is splitting the filename from a full path in batch but mine is directly getting the file name. i got the right answer here. the problem is my question is now having a duplicate flag. how can i remove it. Thanks for your help – paulcab Mar 25 '16 at 06:06

2 Answers2

3

The FOR loop has variable modifiers such that only the file name and extension can be presented. Note that a directory can have an extension. Use FOR /? for information about the variable settings.

FOR /F "usebackq tokens=*" %d IN (`DIR /S /B /A:D /O:N`) DO (ECHO "%~nxd")

Or, to put the names into a file without quoting:

DEL foldername.txt
FOR /F "usebackq tokens=*" %d IN (`DIR /S /B /A:D /O:N`) DO (ECHO>>foldername.txt %~nxd)
lit
  • 14,456
  • 10
  • 65
  • 119
0

just use

DIR D:\Project\Java\ /b

the result will be just like what you want

  • That will correctly list the directories within `D:\Project\Java`, but the OP is looking for sub-directories as well (i.e. the `/s` switch). – YowE3K Oct 18 '17 at 18:46
  • This will list the paths too, which is what OP wants to avoid. – flo5783 May 04 '20 at 16:11