Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating folders and sub folder in a batch file

Posted on 2008-10-24
6
Medium Priority
?
755 Views
Last Modified: 2012-06-21
Hi,

Is there a way of creating a range of folders and within the range of folders are 3 sub-folders in a batch file?
For instance i want to create a set of folders from 100 to 150 and within each of these folders are the exact same sub-folders called Reports, Misc and Spreadsheets.

I kind of figured out how to create a single folder and with the 3 sub folder but not too create a whole range without having to input the number manually.
So so far i have a createfol.bat file with the follwoing commands:
mkdir %1\reports
mkdir %1\spreadsheets
mkdir %1\misc

I can run this .bat in the cmd by typing
createfol.bat 100
Now this creates the folder 100 with the subfolders i want but i dont want to have to do this another 200-300 times to create the others.

Is there an easier way? any help really appreciated

Thanks for you help in advance
0
Comment
Question by:Lau888
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:jonathonberg
ID: 22794883
Create another batch file using creatfol.bat 100, use excel to create the command for each folder (100-159) and paste the commands into the new batch file and run.
0
 

Author Comment

by:Lau888
ID: 22794937
Hi,
Thanks for the quick reply.

Apologies if i am being stupid, but i am not quite sure what it is you mean? The createfol.bat i have includes the commands to create the sub-folders only. So in order for me to create the root folder i.e 100 i have to go into command prompt and type in "createfol.bat 100" so now if i wish to create a folder called 101 with the same sub-folders i have to type teh following in comman prompt again "createfol.bat 101"

So what would i need put into excel in order to create the new batch file? Sorry about this bit of a begineer when it comes to scripting.

Thanks
0
 
LVL 85

Expert Comment

by:oBdA
ID: 22795116
Try the script below. It uses a loop ("for /L"), and the script now requires two command line arguments: the first folder number to be created and the last one.
createfol 100 150
It's currently in test mode, so that you can try it; it will only display the "md" commands it would otherwise run.
Remove the capitalized ECHOs in front of the md commands to run it for real.
@echo off
setlocal
if "%~2"=="" (
  echo Syntax: %~nx0 ^<starting number^> ^<end number^>
  goto :eof
)
set RangeStart=%~1
set RangeEnd=%~2
for /l %%i in (%RangeStart%, 1, %RangeEnd%) do (
  echo Processing folder %%i ...
  ECHO md "%%i\reports"
  ECHO md "%%i\spreadsheets"
  ECHO md "%%i\misc"
)

Open in new window

0
Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

 

Author Comment

by:Lau888
ID: 22796096
Sorry odba, you will have to excuse my poor knowledge of all this. I have tried to use your script and removed ECHO but it doesnt seem to work. Also changed the where it says "RangeStart" and " RangeEnd" to the number i require and it still doesnt work. Am i doing something incorrect???

Apologies again if it is a simple mistake.

Thanks
0
 
LVL 85

Accepted Solution

by:
oBdA earned 1000 total points
ID: 22796267
Copy and paste the script above as it is, and save it as "CreateFolders.cmd".
Open a command prompt in the folder you've saved the script in, and enter
CreateFolders 100 150
The result should look something like this:

C:\Temp>CreateFolders 100 150
Processing folder 100 ...
md "100\reports"
md "100\spreadsheets"
md "100\misc"
Processing folder 101 ...
md "101\reports"
md "101\spreadsheets"
md "101\misc"
Processing folder 102 ...
etc.

The <md "101\reports"> lines are the commands that the script would run if you remove the ECHO commands, so that you can verify it actually generates the folder names you want.
You can add the absolute target path to the "md" commands as well, that will make the script independent of the folder it's started from, for example like this (with the main folder being "D:\Data\Files"):
  ECHO md "D:\Data\Files\%%i\reports"
%%i goes wherever you want the "numbered" folder appear.
0
 

Author Comment

by:Lau888
ID: 22796394
That worked!!

Many thanks obda, you are a life saver!!!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question