Server Fault is a question and answer site for system and network administrators. 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

Scripting question. Dang, it should be easier than this. I have a file with a list of names. I'd like to turn those names into folders. Whenever I try the FOR loop, all I get is the name of the file I want to read from being created as a folder, not the contents of the file being turned into folders. Just want to mkdir for each item in the list.

share|improve this question

One way is to open the list of names in word and do copy and replace. Replace ^p with ^pmkdir this will replace all carraige returns (^p) with a carriage return and the mkdir command. Then save it as a bat and run it. There's def a cleaner way to do this if its going to be used over and over, but this is quick quick.

share|improve this answer
+1 - I like it. Quick, simple, easy, and it works. – Mark Henderson Sep 1 '09 at 0:08
I generally use Excel for generating batch files like this, but this method will work too. – 21st Century Moose Sep 1 '09 at 9:20

Here you go:

for /f %d in (listfiles) do md %d

This makes a directory under the current directory for each line in a file called "listfiles" in the current directory.

If you saved the file as a .txt file, make sure to reflect that in the command.

for /f %d in (listfiles.txt) do md %d 
share|improve this answer

Assuming list of names are stored in a text file called dirlist.txt, then the command should be like this:

for /F "tokens=1 delims= " %i in (dirlist.txt) do md %i

share|improve this answer

The code for /f %d in (listfile) do md %d is nice but if you want to use it in a batch file you need to add extra % to the variable. for /f %%d in (listfile) do md %%d

share|improve this answer

if you have powershell then
gc listfile | %{md $_}

share|improve this answer

None of the other suggestions worked. I finally got it working in a bat file with:

% for /F %%d IN (yourfile.txt) DO md %%d
share|improve this answer

The other examples will read only up until it encounters a space or \newline (by default). Directories, of course, can have spaces. I just used this in batch to create 120+ directories (with spaces in names) using lines copied out of excel (items were already quoted).

If your folders/dirs have spaces in them, wrap quotes around each filename in the text file. (Of course if you need to do this, then you could just as easily search/replace and prepend 'md ' to each line.. and run that)

for /f "tokens=* delims=,"  %%d in (dirs.txt) do md %%d
share|improve this answer

Using %%d allows md to make directories with spaces.

for /F "tokens=1 delims=," %%d IN (folderlist.txt) DO md "%%d"
share|improve this answer

protected by Sven Jul 15 '15 at 21:53

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?