Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get foldername without path using dir in cmd

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?

like image 899
paulcab Avatar asked Feb 12 '26 01:02

paulcab


1 Answers

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)
like image 89
lit Avatar answered Feb 13 '26 15:02

lit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!