Question

BAT File For Creating Folders from a TXT File List

Asked by: KarateKid

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

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-08-29 at 14:24:30ID24692397
Tags

Bat file

,

txt file

,

folders

Topic

MS DOS

Participating Experts
3
Points
200
Comments
13

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

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.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

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.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

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.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Unable To Allow Download of BAT or COM Files ("Page C…
    Hello, My intention is to store scripts in an SQL Server database as plain text, then when someone selects to download from my ASP page, the plain text is written to a BAT or COM file and the user is redirected to this file. I have set up the process on my local development...
  2. .bat file
    How can I turn a .bat file into a .exe I have some logon scripts and scripts that get information form clients that I want to make them .exe's
  3. Need a bat file
    I have multiple txt files, all in the same format. This txt files are data coming off a database. All columns are delimited and formatted the same. I have about 100 files. I need to copy and paste them into one large file so that I can pump it into access. Do you have a bat...
  4. Change .txt to .bat
    I have a folder with a long list of .txt files that I would like to change into .bat files. Can this be done with VB Script?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

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.

Join the Community

Answers

 

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.

 

by: leewPosted on 2009-08-29 at 14:35:33ID: 25215445

If you NEED it in a batch file - same command but double the % signs:

for /f "tokens=*" %%a in (YourFile.txt) do @MD "%%a"

 

by: KarateKidPosted on 2009-08-29 at 14:39:22ID: 25215455

Imagine you are dealing with the dumbest person on earth.  Then explain what you just said above =) What part do i putin the bat file?

 

by: KarateKidPosted on 2009-08-29 at 14:40:28ID: 25215461

I mean take a look at the username i picked... I am very dumb...

 

by: KarateKidPosted on 2009-08-29 at 14:42:18ID: 25215469

I dont even know where i would enter a command file... infact it was just pointed out to me by a friend that i used "putin" not "paste"  very very dumb over here...

 

by: KarateKidPosted on 2009-08-29 at 14:46:00ID: 25215482

I use to have this code so I know it exists but me lose it when I got a good new computer.

 

by: leewPosted on 2009-08-29 at 14:54:50ID: 25215516

Sorry - I have to go out right now.  If you need more help, you need to provide DETAILS.  For example, what version of Windows are you using?  Windows XP?  Vista?  What?  The exact steps to getting to a command prompt are different for each.  Where is the text file you have saved?

 

by: QlemoPosted on 2009-08-30 at 01:15:51ID: 25216743

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.

 

by: BillDLPosted on 2009-08-30 at 07:50:24ID: 25217842

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_Questions
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_Exchange_Questions"
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\LOCALS~1\Temp
TMP=C:\DOCUME~1\Bill\LOCALS~1\Temp

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_Exchange_Questions"
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_Questions"
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

 

by: QlemoPosted on 2009-08-30 at 11:04:55ID: 25218534

Bill,

I appreciate that you take the time to answer in such big amount of words :-)
And yes, I'm not on earth anymore regarding batch files. I write them if I don't want to strain my brain ;-)

KarateKid,
I hope that gives you something you can work on.

 

by: KarateKidPosted on 2009-08-30 at 11:33:35ID: 31622115

Thank you!

 

by: BillDLPosted on 2009-08-30 at 15:58:56ID: 25219761

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

 

by: QlemoPosted on 2009-08-30 at 17:27:42ID: 25219986

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.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...