How to Create a Folder with VBScript

 VBScript Create Folder

My mission is to create a VBScript that will write data to files.  The problem is that before we can run (write to file), we must learn to crawl (create the folder).  To get the most out of this section realize that there are 3 different html pages.  Each page has a tutorial, which explains one of the 3 stages, create the folder, create the file, write data to that text file.  This page starts at the beginning with creating the folder.

(Note I also have an example which creates a user’s home folder)

Topics for Creating a Folder with VBScript

 ♣

Without creating or locating the parent folder object, you cannot manipulate the child file object.  The VBScript examples on this page are at the introduction level, both in terms of ease of scripting and the concept of first creating the folders.

Our FSO Mission and GoalFSO VBScript Example to CreateFolder

Our FSO (File System Object) mission on this page is clear, to create a folder.  However, I have decided to build the script in stages so that you can examine each component.

Scripting folders and files is often an add-on to another script.  Take as an example, where you query the operating system, then display the data.  At this point do you want to echo the output to screen or would you prefer to store the data in a file?  The answer is often store the data in permanently in a text file.

(Note I also have an example which creates a user’s home folder)

Example 1 – Basic VBScript to Create a Folder

Be aware that this script works brilliantly first time.  You get the folder specified by strDirectory, but if you run the script again you get errors.  My work-around is to keep modifying strDirectory, for example

2nd Running change strDirectory = "C: \logs" to strDirectory = "C: \logs\take2".  However, I realize that this work-around is no long-term solution, and in Example 2 I cure the problem with error-correcting code.

Prerequisites

This is a script that will execute equally well on a Windows server or an XP machine.  Should you get permission errors, I recommend that you logon as administrator.

Instructions for Creating Folders

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Decide whether to change the value for strDirectory (Particularly the drive letter).
  3. Save the file with a .vbs extension, for example, NewFolder.vbs 
  4. Double click NewFolder.vbs and check Windows Explorer for strDirectory.

Script to Create a Folder


‘ NewFolder.vbs
‘ Free example VBScript to create a folder (Simple)
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.4 – September 2010
‘ ————————————————‘
Option Explicit
Dim objFSO, objFolder, strDirectory
strDirectory = "c:\logs"
‘ Create FileSystemObject. So we can apply .createFolder method
Set objFSO = CreateObject("Scripting.FileSystemObject")

‘ Here is the key line Create a Folder, using strDirectory
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
WScript.Quit
‘ End of free example VBScript to create a folder

VBScript Tutorial – Learning Points

Note 1:  CreateObject holds the key.  What we want here is a FileSystemObject object suitable for making folders and files.  My point is that for this section, we don’t need a network or an Active Directory object.

Note 2:  Now what we need here is a folder.  So, we apply the .CreateFolder method to the objFSO and thus ensure that we get the desired folder object (and not a file object).

Recommended: Solarwinds’ Permissions Analyzer – Free Active Directory ToolFree Permissions Analyzer for Active Directory

I like thePermissions Analyzer because it enables me to see WHO has permissions to do WHAT at a glance.  When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, and takes into account network share access, then displays the results in a nifty desktop dashboard!

Think of all the frustration that this free SolarWinds utility saves when you are troubleshooting authorization problems for user’s access to a resource.  Give this permissions monitor a try – it’s free!

Download SolarWinds’ Free Permissions Analyser – Active Directory Tool

Example 2 – Create a Folder with error-correcting Code

This script builds on Example 1 by adding code that copes with situations where the folder already exists.  It also employs the objShell to run the Windows Explorer.  This is a bonus method so that you can see that the script executed as designed.

Sample VBScript to Create a Folder

‘ NewFolderEC.vbs
‘ Free example VBScript to create a folder with error-correcting Code.
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.6 – May 2010
‘ ————————————————‘

Option Explicit
Dim objFSO, objFolder, objShell, strDirectory
strDirectory = "c:\logs"

‘ Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

‘ Note If..Exists. Then, Else … End If construction
If objFSO.FolderExists(strDirectory) Then
   Set objFolder = objFSO.GetFolder(strDirectory)
   WScript.Echo strDirectory & " already created "
Else
   Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
End If

If err.number = vbEmpty then
   Set objShell = CreateObject("WScript.Shell")
   objShell.run ("Explorer" &" " & strDirectory & "\" )
Else WScript.echo "VBScript Error: " & err.number
End If

WScript.Quit

‘ End of Sample VBScript to create a folder with error-correcting Code

VBScript Tutorial – Learning Points

Note 1:  The construction, If objFSO.FolderExists (strDirectory) is interesting and slightly unusual.  This is because, while FSO supports the .FolderExists method, unfortunately, not all other objects support the ‘Exists’ family of methods.

Note 2:  The extra section is also interesting in that you can see two different VBScript folder methods, GetFolder (line 16) and CreateFolder (line 19).

Note 3:  Example 2 (above) is a proper script with error-correcting code.  Whether the folder already exists, or whether the script creates the folder, VBScript cannot fail.  The WScript.Echo message is largely for testing, feel free to remove such lines from your production script.

Note 4: objShell = CreateObject("WScript.Shell") is my way of demonstrating that the script has achieved its goal.  The active part is objShell.run.  In this example, it is as if you clicked on the Start (Menu), Run and typed ‘Explorer’.

Solarwinds Config GeneratorGuy Recommends: The Free Config Generator

SolarWinds’ Config Generator is a free tool, which puts you in charge of controlling changes to network routers and other SNMP devices.  Boost your network performance by activating network device features you’ve already paid for.

Guy says that for newbies the biggest benefit of this free tool is that it will provide the impetus for you to learn more about configuring the SNMP service with its ‘Traps’ and ‘Communities’. Try Config Generator now – it’s free!

Download your free copy of Config Generator

PowerShell Create Folder with New-Item

The secret of mastering New-Item is to observe the rhythm, go with the flow of, -path -name -type.

# PowerShell New-Item Folder
Clear-Host
$Location = "E:\PowerShell\"
New-Item -path $Location -name "Ezine" -itemType "directory"

Note 5:  As usual with my -path examples, I introduce a variable mainly to remind you to change its value if you want the script to work on your machine!

Note 6:  The -itemType parameter can be abbreviated to plain -Type.  Also "Directory" does not have to be in quotes.

Note 7:  If the script succeeds PowerShell gives you a summary of what the script achieved, however, I still like to open Windows Explorer to checkout my new folder. 

See also PowerShell Invoke-Item.

Summary of Creating Folders

Our goal is often to write data to text files.  However, we must go through a natural progression, create the folder, then the file, then read or write text to that file.  This page is a tutorial that shows you how to achieve the first goal, to creating a folder using the CreateFolder method.  Next step, let us explore the CreateTextFile method.

If you like this page then please share it with your friends

 


See more VBScript file examples:

VBScript to create folders   • VBScript to create files    • VBScript write file    • VBScript file copy

PowerShell OutFile   • PowerShell Get-ChildItem   • PowerShell create folder • Event Log Administrator

VBScript Event Log   • WMI Event Log • Event Log Example   • SolarWinds Log Event Manager