Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a list of folders in a directory, i need to copy just all the folder name to a file and the file should be in a single line seperated by delims. can any help me with this in a batch file.


share|improve this question
What you have tried so far? – Siva Charan Sep 15 '12 at 15:40
@SivaCharan it's not so simple, as echo produces a new line. – barlop Sep 15 '12 at 16:11
The echo command built into cmd.exe doesn't support it, but the echos built into the Unix shells add a -n option that suppresses the trailing newline. – Nicole Hamilton Sep 15 '12 at 16:15

In batch you'd do it like this:

@echo off

setlocal EnableDelayedExpansion

for /D %%f in (*.*) do set n=!n!;%%f

if defined n echo %n:~1% > list.txt


To recurse into subfolders replace /D with /R and *.* with ..

for /R %%f in (.) do set n=!n!;%%f

By adding qualifiers to the loop variable you can specify if you want just the name of the folder (%%~nxf) or the full path (%%~dpnxf).

share|improve this answer
This gets just the files, no subdirectories. That could be what the OP meant, but I interpreted his question (perhaps mistakenly) as asking for the opposite, subdirs only, no files. But I still gave you an upvote for doing it with cmd.exe. – Nicole Hamilton Sep 15 '12 at 18:33
My mistake. I forgot the /D for directory enumeration. It's fixed now in my answer. – Ansgar Wiechers Sep 15 '12 at 22:51

There is not single batch file command that does what you want, but you could use a batch file to accomplish the task and call it from your own. Ansgar Wiechers' answer had the right basic idea (I see he's fixed his version after I posted mine).

Anyway, here's a version that will produce a semicolon delimited list of first level subfolders in either the current directory or one passed to it as an argument. I chose to use a semicolon delimiter rather than, say, a comma, because it could never be part a valid filename (and commas can be, although it's uncommon).

If a given folder name has spaces in it, it must be quoted it like this (otherwise quoting the folder's directory path is optional):

subdirs "C:\Documents and Settings"

Note it only prints the list to the console (stdout), so if you want to save that in a file, you'll need append a > filename.ext after the script file name (and any folder name) to redirect the output into file of your choice:

subdirs C:\Windows > dirlist.txt

== subdirs.bat ==

@echo off
setlocal EnableDelayedExpansion
pushd %1
for /D %%d in (*.*) do set n=!n!;%%d
if defined n echo %n:~1%

If you'd like to produce the full path to each folder name, you can change the set n=!n!;%%d to set n=!n!;%%~fd in the for loop line.

share|improve this answer
Might be easier to cd into the root directory than to prepend the pattern with a path: if "%1" neq "" cd "%1" – Ansgar Wiechers Sep 15 '12 at 22:59
@Ansgar Wiechers: You're right, but it'll need to save and restore the current directory so that's not a permanent side-effect. I'll incorporate that along with another simplification I have planned. Thanks for the suggestion. – martineau Sep 16 '12 at 0:56
+1 for pushd. Haven't used this in ages. – Ansgar Wiechers Sep 16 '12 at 13:19
@Ansgar Wiechers: Thanks again. I've simplified my answer even more after realizing checking whether a folder path argument was supplied or not wasn't really necessary. I also changed the delimiter to a semicolon to avoid confusion with possible subfolder names. – martineau Sep 16 '12 at 16:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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