Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch file to list folders within a folder to one level

Tags:

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.

like image 776
Jonny Avatar asked Jan 26 '13 14:01

Jonny


People also ask

How do I create a list of folders?

Using COMPUTER or WINDOWS EXPLORER navigate to the folder containing the files you want to make a list of. o Do not open the folder– you should be 'one level' up so you see the folder itself and not the contents. Press and hold the SHIFT key and then right-click the folder that contains the files you need listed.

What does %1 mean in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.


2 Answers

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 
like image 171
David Ruhmann Avatar answered Oct 21 '22 18:10

David Ruhmann


print all folders name where batch script file is kept

for /d %%d in (*.*) do (     set test=%%d     echo !test! ) pause 
like image 33
Ashish Singhal Avatar answered Oct 21 '22 16:10

Ashish Singhal