Need a Script to Create Multiple Folders on a Server

Posted on 2011-09-14
Last Modified: 2012-05-12
Hello all,
 I stood up a file server running Server 2008 that I need to create approximately 157 individual folders on the will reside in a "Backups" directory that I have also created. I can't create the folders through a logon script so I need to do it locally on the machine. Does anyone have a script to simply create and name a bunch of folders?
Question by:BoxunloX
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

Expert Comment

ID: 36536160
Not able to help in VB, but I can provide a perl script to do that. All you would have to do is install ActivePerl ( and your ready to go.

The script uses a CSV input file and will create as many or few folder as possible. You just input the full path you want the folder created at and your good to go. The .bat is optional, but makes it easier to run the script. Hope this helps if no one provides any VB for you.

Create a file and put the below code in (name the file

use strict;

my $file = 'folders.csv';

open (F, $file) || die ("Could not open $file!");

while (my $line = <F>){

  my ($folder) = split ',', $line;
  print "Creating folder ($folder)\n";
  mkdir "$folder";
  if(-d $folder){
    print "Folder created\n";
    print "Failed to create\n";


close (F);

Open in new window

Create a file and put the below code in (name the file folder.csv)

Open in new window

Create a file and put the below code in (name the file

Open in new window


Expert Comment

ID: 36536191
One last not. If you don't want to specify a directory to create the folder in and just a list of folders you could do that as well and just copy and run the script in the directory you want the folder to be created in. So if you copied all 3 files to C:\temp\ and use the below .csv file it would create:


Create a file and put the below code in (name the file folder.csv)

Open in new window


Accepted Solution

BoxunloX earned 0 total points
ID: 36536583
Thanks KGNickl for the assistance! I actually found a one-liner CMD command that did the trick. I simply  put the .CSV file in the directory that I wanted to create the folders in, opened the command prompt, and typed the following:

FOR /F "delims=~" %f in (usernames.txt) DO MD "%f"

Open in new window

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now


Author Closing Comment

ID: 36558755
I found a simple one-line command that did exactly what I was looking for.
LVL 55

Expert Comment

by:Bill Prew
ID: 36536619
Easy enough to do in a simple Batch script (BAT) using the MD command, like:

md c:\dir1
md c:\dir2
. . .

my question is, is there any logic to the names of the folders that you need to create, or is it a list in a txt file or something.  If so we could enhance the BAT script to be smarter and save work.  But need to better understand the set of folders you need to create.

LVL 14

Expert Comment

by:Don Thomson
ID: 36536649
For Something like that I would just use something like Excel and build a quick and dirty batch file
Final                                    ColB             ColC               ColD
MD c:\backup\Folder1\      MD       c:\backup\      Folder1\

Column A  use the formula =+b1&C1&D1
in Column b type MD plus a space
in column c your master Backup Path
Col D  is where you put the folder names you want
Fill in this list first
Then Copy Contents of A1, b1 and C1 down to fill in the three column til you have everything covered

Then do File SaveAs - Type TXT - Name it Myfolders.txt
Close the excel Spreadsheet then open the Myfolders.txt
The formulas will have changed to the real words - just delete column B, C &D
Then resave the file - Type txt  to Folder.txt

Put it in the root folder of C:\
then in a command box  (Run CMD)  type ren folders.txt *.bat
then just type myfolders and the folders will be created


Expert Comment

ID: 36536695
Try this vbScript

Open in new window

' Usage: cscript CreateFolders.vbs inputfile.csv
' One absolute path per line
On Error Resume Next
Dim arrFolderName(200), i, objArgs, objFSO, strNextLine, x
Const ForReading = 1
Set objArgs = WScript.Arguments
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check if inputfile exists
if not (objFSO.FileExists(objArgs(0))) Then
      wscript.echo "Can't find "& objArgs(0) & vbcrlf & "This file must reside in the same dir as this script."
end If

' Read input file into an array
Set objTextFile = objFSO.OpenTextFile(objArgs(0), ForReading)
i = 0
Do While objTextFile.AtEndOfStream <> True
    strNextLine = objTextFile.Readline
    If(Len(strNextLine)<>0)Then      'If line isn't null. More error checking maybe needed.
          arrFolderName(i) = strNextLine
          WScript.Echo i & "=" & strNextLine
    End If
    i = i + 1

' Main loop - Create the folders if they don't exist
For x = 0 To i - 1
      If Not objFSO.FolderExists(arrFolderName(x)) Then
          Set objFolder = objFSO.CreateFolder(arrFolderName(x))
          WScript.Echo arrFolderName(x) & " already exists."
      End If

Set objFSO = Nothing

Open in new window


Expert Comment

ID: 36543788

' Main script
filepath = " C:\Backups\ "
call Create_folder(filepath)
' Function to Create 157 folders, parameter should be filepath'
Function Create_folder(filepath)
Set fso = createobject("scripting.filesystemobject")
For i = 1 to 157
fso.createfolder filepath&"Folder"&i
set fso = nothing
End Function


Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…

734 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