1

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.

Thanks

3
  • 2
    What you have tried so far? Sep 15, 2012 at 15:40
  • @SivaCharan it's not so simple, as echo produces a new line.
    – barlop
    Sep 15, 2012 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. Sep 15, 2012 at 16:15

2 Answers 2

1

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

endlocal

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).

2
  • 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. Sep 15, 2012 at 18:33
  • My mistake. I forgot the /D for directory enumeration. It's fixed now in my answer. Sep 15, 2012 at 22:51
0

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%
popd
endlocal

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.

4
  • Might be easier to cd into the root directory than to prepend the pattern with a path: if "%1" neq "" cd "%1" Sep 15, 2012 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, 2012 at 0:56
  • +1 for pushd. Haven't used this in ages. Sep 16, 2012 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, 2012 at 16:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .