23

I have searched and searched to no avail so apologies if the answer does exist.

I am not very good with batch files so please keep this in mind.

All I am after is a single batch file that will list/save to file a list of the folders within the current folder.

So basically if I run this batch file within a certain folder it will output all folders (not files or sub folders, just to one level) within the folder from which the batch file was run.

I assume this is probably an easy request however I have not had any luck on Google, etc.

0

3 Answers 3

67

Dir

Use the dir command. Type in dir /? for help and options.

dir /a:d /b

Redirect

Then use a redirect to save the list to a file.

> list.txt

Together

dir /a:d /b > list.txt

This will output just the names of the directories. if you want the full path of the directories use this below.


Full Path

for /f "delims=" %%D in ('dir /a:d /b') do echo %%~fD

Alternative

other method just using the for command. See for /? for help and options. This can output just the name %%~nxD or the full path %%~fD

for /d %%D in (*) do echo %%~fD

Notes

To use these commands directly on the command line, change the double percent signs to single percent signs. %% to %

To redirect the for methods, just add the redirect after the echo statements. Use the double arrow >> redirect here to append to the file, else only the last statement will be written to the file due to overwriting all the others.

... echo %%~fD>> list.txt
2
  • Ok, I have the following: for /D %%A IN (*) DO echo %%A>> output.txt but the result seems to be in date order. How do I sort by name. Regards, Jonny
    – Jonny
    Commented Jan 26, 2013 at 15:41
  • Unfortunately, to easily sort the results you will want to use the dir command. The only other option is to use the sort command. Add the /o:n parameter to the dir command. See dir /? for all the options. Here are the two dir commands updated. dir /a:d /b /o:n > output.txt or for /f "delims=" %%D in ('dir /a:d /b /o:n') do echo %%~fD>> output.txt Commented Jan 26, 2013 at 16:11
2

I tried this command to display the list of files in the directory.

dir /s /b > List.txt

In the file it displays the list below.

C:\Program Files (x86)\Cisco Systems\Cisco Jabber\XmppMgr.dll

C:\Program Files (x86)\Cisco Systems\Cisco Jabber\XmppSDK.dll

C:\Program Files (x86)\Cisco Systems\Cisco Jabber\accessories\Plantronics

C:\Program Files (x86)\Cisco Systems\Cisco Jabber\accessories\SennheiserJabberPlugin.dll

C:\Program Files (x86)\Cisco Systems\Cisco Jabber\accessories\Logitech\LogiUCPluginForCisco

C:\Program Files (x86)\Cisco Systems\Cisco Jabber\accessories\Logitech\LogiUCPluginForCisco\lucpcisco.dll

What is want to do is only to display sub-directory not the full directory path.

Just like this:

Cisco Jabber\XmppMgr.dll Cisco Jabber\XmppSDK.dll

Cisco Jabber\accessories\JabraJabberPlugin.dll

Cisco Jabber\accessories\Logitech

Cisco Jabber\accessories\Plantronics

Cisco Jabber\accessories\SennheiserJabberPlugin.dll

2

print all folders name where batch script file is kept

SETLOCAL EnableDelayedExpansion

for /d %%d in (*.*) do (
    set test=%%d
    echo !test!
)
pause

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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