• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2130
  • Last Modified:

Batch file to create multiple folders

My company is in the process of restructuring our directory and I need to put together a batch file to create a bunch of folders.  The idea is to match these new folders to the customer numbers in our database but use six digit numbers for the folder names. (i.e.-004532)

On top of that, to make it easier to find the folder we are looking for, we want to separate them into folders containing 500 folders each.  So within a "root" directory we would have a folder "1-500" and within it would have folders "000001", "000001", "000002", etc. and continuing with folders "501-1000", "1001-1500", and so on.

We are looking at about 8,000 customers as of now with more to come in the future so you could imagine that it would be a real pain in the butt to create each folder individually.  Certainly, a batch file to automate the process would make my life much easier.  

Any help would be greatly appreciated.
1 Solution
Lee W, MVPTechnology and Business Process AdvisorCommented:
I know you can use SET /A and a FOR loop to get this COMPLETELY automated, but I always have trouble for batch math.  So, as an alternative, you could setup an Excel sheet with two columns (no column headings) - "parent directory" and "directory"

Then in Parent directory, every 500 change it... the file would look something like this:

000500  000001
000500  000002
000500  000003
001000  000501
001000  000502
001000  000503
008000  007501
008000  007502
008000  007503

... = fill in the blanks

Then save the file as a tab delimited text file. Then the command - NOT script - would look like this:

for /f "tokens=1,2" %a in (TabDelimFile.txt) Do @md x:\path\to\folders\%a\%b

Note - %a would be column 1 and %b column 2.

If you're at all familiar with Excel, setting up the file shouldn't take more than 5 minutes then execute the command (if you save the command to a batch file (kinda pointless if you ask me) then you have to double the percents - in a batch file, use this line:

for /f "tokens=1,2" %%a in (x:\Path\To\TabDelimFile.txt) Do @md x:\path\to\folders\%%a\%%b
I think you'd be better off using vbscript.

Look here for a discussion about creating a folder structure using vbs:
You can use ADO from VBScript, so such would be driven directly from a DB query to provide the customer numbers, and maybe a sub to create the subfolders..?

Don't hesitate if you need further info...

Regards .. Alan
The following batch script will do exactly what you want (if i got you right that is) :

------- batch script start -------
@echo off

if "%1"=="" goto enderror

echo creating %1 customer directories

set dirstart=1
set /A dirend=dirstart+499

mkdir %dirstart%-%dirend%

cd %dirstart%-%dirend%
if %1 leq %dirend% set /A dirend=%1
echo %dirstart%-%dirend%
for /L %%i in (%dirstart%,1,%dirend%) do mkdir %%i
for /D %%j in (?) do rename %%j 00000%%j
for /D %%j in (??) do rename %%j 0000%%j
for /D %%j in (???) do rename %%j 000%%j
for /D %%j in (????) do rename %%j 00%%j
for /D %%j in (?????) do rename %%j 0%%j
cd ..

if %dirend% geq %1 goto endnormal
set /A dirstart=dirstart+500
set /A dirend=dirstart+499
goto loop

echo done
goto end

echo specify the number of client directories to be made as parameter !
goto end

------- batch script end -------

Give the number of clients as parameter, and that number of directories will be made.

This script will only work on Windows NT/2K/XP however.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now