If you NEED it in a batch file - same command but double the % signs:
for /f "tokens=*" %%a in (YourFile.txt) do @MD "%%a"
Main Topics
Browse All TopicsCan someone please give me the code for creating a BAT File For Creating Folders from a TXT File List... Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
KarateKid,
it's good idea to provide the experience level when you start a question. Experts will know if they have to start on Big Bang then :-)
I can't get too much into detail, as it would fill several pages. But let's put some basics here.
A batch file is a simple text file containing commands for CMD.EXE (if you are with Windows NT, 2000, XP, Vista, 7).
CMD.EXE is the so-called DOS Pompt. It executes batch files, internal commands, and starts other programs. It is commandline driven (not graphically as the Windows).
Leew provided the commands to use to read a text file, and for each line of it create a folder in the current folder. To use them, you have to create a text file, e.g. with Notepad. Write one folder at each line. Use full path, like c:\temp\oneFolder. Store the file somewhere you find it easily, and name it YourFile.txt
Call cmd.exe (Start -> Run -> cmd.exe). Then type:
cd WhereYourFileIs
for /f "tokens=*" %a in (YourFile.txt) do @MD "%a"
And you have your folders created.
A word of warning! If you do not want to make this often, it is much better to create the folders manually in Explorer. Only if this is a task to do every week or so, the effort to understand and maintain the "batch" is reasonable.
That's quite a good explanation by Qlemo, but you need to remember that the experts who hang around this type of Zone can write code in their sleep. It's sometimes hard for such seasoned experts to put themselves in the position of somebody who is just starting out and it all looks like gobbledegook.
I'm not nearly as well versed in batch files as Qlemo, leew, and a number of other respected names in this area, so maybe I am a bit "fresher" at this and (seeing as I have a bit of spare time right now), can explain it in an idiot-proof way that will help you.
Forget the FOR command that has been given just for now and just think about a single "Make Directory" command - MD or MKDIR in "DOS". I'm calling this DOS for simplicity, even though the command line in Windows 2000, XP, and Vista differs in the way it works behind the scenes from Windows 98 and older.
First off, for most things DOS doesn't are if you use uppercase or lowercase commands, so MD would work just the same as md or Md or mD. It's clearer if you specify commands in uppercase though.
To create a new folder from a DOS window you can do one of two things:
1. Navigate INTO the folder where you want to create the folder and run the simple command MD FolderName
2. From wherever you are (the DOS "Prompt" shows you this), type the full path to where you want your new folder to be created, eg.
MD "C:\Documents and Settings\Bill\My Documents\MyNewFolderName"
It is a very good practice to surround all paths that contain spaces with quotes as shown in the 2nd option above. Although Windows 2000 and XP don't object to spaces in file or folder names in the same way as older proper versions of DOS did, you are eliminating the possibility that some instance may not work without the quotes.
OK, so say you have a text file named "folders.txt" in which you have the following list of folder names:
Sample Images
Bank Statements
Lottery Numbers
Stuff
My_Experts_Exchange_Questi
NewVids
To create new sub-folders with those names in one folder, let's say within an existing folder
"C:\Documents and Settings\KarateKid\My Documents"
you have several options.
The easiest way, and it all depends on how many folder names are in your text file, is to turn your list of folder names into your batch file like this:
@echo off
echo Creating folders ...
MD "C:\Documents and Settings\KarateKid\My Documents\Sample Images"
MD "C:\Documents and Settings\KarateKid\My Documents\Bank Statements"
MD "C:\Documents and Settings\KarateKid\My Documents\Lottery Numbers"
MD "C:\Documents and Settings\KarateKid\My Documents\Stuff"
MD "C:\Documents and Settings\KarateKid\My Documents\My_Experts_Excha
MD "C:\Documents and Settings\KarateKid\My Documents\NewVids"
echo.
echo Finished folder creation.
echo.
PAUSE
EXIT
You don't really need the @echo off and EXIT lines. The @echo off line just tells it not to echo back everything to screen unless the command always does so, or where you want to have some text echoed to screen as I have done above. Those are just adornments, as is the PAUSE. A batch file like that normally would open the Command Window for as long as it took to create the folders and then close. The PAUSE command just stops this so that you can verify what happened, because the MD command will echo what it did to screen. Any Error messages would also show, so pausing at the end, with a "Press any key to continue ..." prompt makes sense unless you want it to just exit.
OK, so if you want to save your file as a batch file from Notepad, you can just do a File > Save As, and type a new File Name. To save it as a .BAT or .CMD file, just enclose the file name with quotes and include the dot and the cmd or bat extension, eg. "Make-Folders.cmd". This ensures that Notepad saves the file as a file of that type rather than as a .TXT file. The preerred file type for batch files in Windows 2000 upwards is .CMD. In previous Windows versions, use the .BAT extension.
Now, seeing as you gave the full paths to your new folders in the above example, you can put that batch file anywhere you wish and, when double-clicked, it will work fine.
Just a note about "Variables" in Windows. There are preset variables that can be used to save a lot of typing. To see these variables, you can open up a new Command (DOS) Window and type the single command SET then press the Enter key. Picking out a few useful ones, you will see:
HOMEDRIVE=C:
SystemDrive=C:
SystemRoot=C:\WINDOWS
windir=C:\WINDOWS
HOMEPATH=\Documents and Settings\Bill
USERNAME=Bill
USERPROFILE=C:\Documents and Settings\Bill
ProgramFiles=C:\Program Files
TEMP=C:\DOCUME~1\Bill\LOCA
TMP=C:\DOCUME~1\Bill\LOCAL
The abreviated paths for TMP and TEMP are actually to the folder:
C:\Documents and Settings\Bill\Local Settings\Temp
To use any of these variables in a batch file, just enclose the name that is before the = sign in % %
For example, instead of specifying the full paths to the "My Documents" folder when creating new folders using the batch file I gave earlier, you can use this path:
"%USERPROFILE%\My Documents\NewFolderName"
@echo off
echo Creating folders ...
MD "%USERPROFILE%\My Documents\Sample Images"
MD "%USERPROFILE%\My Documents\Bank Statements"
MD "%USERPROFILE%\My Documents\Lottery Numbers"
MD "%USERPROFILE%\My Documents\My Documents\Stuff"
MD "%USERPROFILE%\My Documents\My_Experts_Excha
MD "%USERPROFILE%\My Documents\NewVids"
echo.
echo Finished folder creation.
echo.
PAUSE
EXIT
A Variable is just the name for an area that stores some data, and in most cases this is a Text String of some kind. When recalled in a Command window or batch file using %VariableName% it just expands it in real time to whatever the variable has stored.
Hope you're with me so far. I've tried to make it easy to understand.
Of course, instead of specifying the full path on each MD command, you can instead Change Directory INTO the folder where you wish to create the new folders, and then each MD command just provides the new folder name:
@echo off
CD \
CD "%USERPROFILE%"
CD "My Documents"
echo Creating folders ...
MD "Sample Images"
MD "Bank Statements"
MD "Lottery Numbers"
MD "Stuff"
MD "My_Experts_Exchange_Quest
MD "NewVids"
echo.
echo Finished folder creation.
echo.
PAUSE
EXIT
In the above example I have used the "Change Directory" command 3 times. You don't really need the CD \ command to start with. The \ symbolises the Root of your Current Drive, which in most cases is C:\
Seeing as %USERPROFILE% contains the full path to your Profile Folder (C:\Documents and Settings\KarateKid), then this makes the CD \ command superfluous. Changing directory first into the C:\Documents and Settings\KarateKid folder first, and THEN into the "Desktop" folder is also not really needed. Those 3 CD command lines in the batch file could just be specified with one line:
CD "%USERPROFILE%\My Documents"
If you typed this in a Command Window, you would see that the new "DOS Prompt" would be showing as:
C:\Documents and Settings\KarateKid\My Documents>
(Note the > at the end that tells you it is the Prompt).
That can now be referred to as your "Current Directory", so any commands executed while at that Prompt will take effect on that folder. So, each MD command now just creates a new folder in the "Current Directory".
It's useful at this stage to tell you about a couple of helpful methods for creating batch files, in case you weren't aware. The Windows Notepad has a good "Find and Replace" function.
Say for example that you already have your list of folder names already as fully qualified paths as you begin to make it into a batch file, eg.
C:\Documents and Settings\KarateKid\My Documents\Sample Images
C:\Documents and Settings\KarateKid\My Documents\Bank Statements
C:\Documents and Settings\KarateKid\My Documents\Lottery Numbers
You could use the Find and Replace function in Notepad to find all instances of:
C:\Documents and Settings\KarateKid
and Replace each instance with:
MD "%USERPROFILE%
You would then just have to add on the closing " at the end of each line.
Microsoft Excel is also very useful in some cases if you rename your text file to a .CSV file instead of a .TXT file. It will open in Excel when double-clicked. Say you wanted to do just the same steps as you did using Notepad's search and replace but in Excel. When opened, each full path to the new folder will be on a separate line under Column A. If you now Insert a New Column to the left, your paths will be under Column B and you can insert MD plus a trailing space into each line in Column A. You could then highlight all of Column B and do a Search and Replace, finding all instances of:
C:\Documents and Settings\KarateKid
and replacing with "%USERPROFILE%
In Column C you then just enter a " in each line.
When you now Save the file and rename back to a .TXT file, you would have commas like this:
MD ,"%USERPROFILE%\My Documents\Sample Images,"
and so on.
You just do a search and replace now and replace all instances of commas with nothing, and it gets rid of them.
With me so far? I hope so.
OK, now back to leew's earlier suggestion that uses the FOR command or "statement".
The FOR command is used to repeat tasks until they are done, and has equivalents in other programming languages such as WHILE or DO WHILE. It is referred to as a "FOR "loop" for this reason.
for /f "tokens=*" %%a in (YourFile.txt) do @MD "%%a"
The part in brackets is the file to be read through one line at a time. The command knows that it needs to do this because the /F switch is used after the FOR command. Think of it as "File", ie. treat whatever is specified in the brackets as a File.
Ignore the "tokens=*" bit for now.
%%a (or if used straight from a DOS window as %a) is a variable that will hold different data with each pass of the FOR loop, ie with each line read from your .TXT file. %%a is just a convenient starting point being the first letter in the alphabet. Some people start with %%i or use a different letter when running a FOR command for different purposes.
OK, so the FOR command starts by reading sequentially through the target file. As it reads the first line of the text file (referring back to my previous example), the variable %%a will have stored the folder name "Sample Images" (but without the quotes).
The next part of the command that is on the RIGHT of the brackets just tells it to create a new folder with the name held in the variable %%a ie. "Sample Images". The quotes are used in case the folder's name contains spaces. If %%a was not enclosed in quotes, then it would probably create only the part of the folder name up to the first space in it, ie. "Sample".
Once complete, it then reads in the 2nd line of your .TXT file, stores that in %%a and runs the MD command to create a folder of that name. When it has no more lines in the target file to read into the variable %%a, then the FOR loop is finished and exits. It's actually pretty simple.
The "tokens=*" part in leew's batch file is something that is usually involved where you wanted the FOR command to split up whatever it is reading (parsing) on a line by line basis. You can gloss over this or read and learn it, the choice is yours, but you don't need to know this for what you are doing.
---------- optionally ignore ----------------
A "token" is a fragment of whatever is parsed.
In the first line of my example .TXT file it had the folder name "Sample Images" (without quotes). If you really wanted to break this up into separate pieces (tokens), then you would tell the FOR command that there were two tokens separated by a space like this:
FOR /f "tokens=1,2 delims= " %%a
Note the space after the = sign in the "delims" (delimeter) part. So, it's telling it to break each space-separated part of the text string into separate parts.
In this case, because you started with the variable %%a, then %%a would hold the word "Sample" and %%b would then hold "Images". This type of thing is NOT useful to your purposes, but if you wanted to use those Tokens for some other purpose, then you would do so AFTER the part of the command in brackets.
As an example, suppose you wanted to take the 2nd word and put it first, and take the 1st word and put it 2nd, and add a # symbol between the words. The following command would output what you wanted to screen if your .TXT file only contained one line with the unquoted string "Sample Images":
FOR /f "tokens=1,2 delims= " %%a in (YourFile.txt) do @echo %%b#%%a
It becomes a bit more complex after this basic principle, but perhaps you will benefit in the future by knowing this.
---------- end of section to optionally ignore ----------------
So, if you wanted to use leew's batch file exactly as it was given, the following would have to be true:
1. Your text file would have to be in the same folder as your batch file.
2. If your text file ONLY contained folder names, then the batch file would have to be in the folder where you wanted your new folders to be created.
Obviously any batch file can be adapted to suit if the above elements were not true, and hopefully I have provided enough detail to allow you to figure out and adapt it yourself. If not, then just ask.
Please DO NOT experiment with any batch files that use the DEL (Delete) or REN (Rename) commands unless you are absolutely sure of what your batch file will do. batch scripts can be very powerful and if a command acted on the wrong folder you could end up with a real mess.
Regards
Bill
Thank you KarateKid, and to Qlemo and leew for not being too annoyed at me for providing the full explanation. You did say "Imagine you are dealing with the dumbest person on earth", so I did ;-)
There is one thing that I feel a bit guilty about, and that is the fact that leew's answer has been missed out when selecting the "Accepted Answer/Solution". His batch file is all that was really needed, and so it did provide the solution to your question. My explanation was just to make sure firstly that you knew where to put it the batch file or how to execute the command safely, and the rest of my comment was just a walkthrough of what it actually did.
I appreciate that you are brand new here, and that it's not immediately obvious (especially to "the dumbest person on earth" :-) that you can split points between more than one comment. In the end you are the one that makes the final decision, but what should have happened had this been a question by a member more acquainted with the process is that you selected leew's comment ID No. 25215440 or 25215445 as the "Accepted Answer", and then selected other comments such as Qlemo's Comment ID 25216743 and my own comment ID 25217842 as "Assisted Answers". I know there weren't that many points up for grabs here, but I think I have the same attitude to points as the other contributors in that helping others is more important than points.
I'll wait to see what leew says about this when he checks in next, and meantime Qlemo might have something to add about my comment here also.
Bill
Bill,
Points are like the stamps in my passport - they document where I have been, but they never could replace the fun I had in travelling :-)
KarateKid,
I don'nt want this thread to be disturbed too much by reanging the points. But if you understood what I tried to explain in my first post, you should (normally) assign some points to it, and to leew as I explained his post. If you would like to honour that, say so, or press "Request attention" and state you want to re-assign points as reason.
As Bill said already, it is your realm, and I do not care that much about points.
Business Accounts
Answer for Membership
by: leewPosted on 2009-08-29 at 14:34:58ID: 25215440
no batch file - single command:
for /f "tokens=*" %a in (YourFile.txt) do @MD "%a"
(This assumes you have the directories to be created on per line - for example:
Folder1
Folder 2
Folder3
Folder 4
etc.