Batch file to create multiple folders

Posted on 2005-05-12
Last Modified: 2007-12-19
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.
Question by:bigbadchad
    LVL 95

    Expert Comment

    by:Lee W, MVP
    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
    LVL 10

    Expert Comment

    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
    LVL 53

    Accepted Solution

    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.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    centeredAverage challenge 8 90
    has77  challenge 9 56
    wefewf 2 28
    create an incrementing variable name AutoHotKey 5 31
    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now