VBScript CreateFolder Method
last modified April 9, 2025
The CreateFolder
method in VBScript is part of the
FileSystemObject
. It creates a new folder at the specified path.
This method is essential for directory management in VBScript scripts. It
returns a Folder object representing the newly created directory.
CreateFolder
will raise an error if the folder already exists or if
the path is invalid. Proper error handling should be implemented when using this
method. This tutorial covers CreateFolder
with practical examples.
CreateFolder Method Overview
The CreateFolder
method takes one parameter: the path of the folder
to create. It creates a new directory at the specified location if possible.
The method requires proper permissions to create folders in the target location.
Key features include automatic path resolution and immediate folder creation. It doesn't create parent directories if they don't exist. Understanding this method helps automate directory creation tasks in scripts.
Basic Folder Creation
This example demonstrates the simplest use of CreateFolder
to
create a single directory. It shows how to create a folder at a specified path.
The script includes basic error handling for common scenarios.
On Error Resume Next Set fso = CreateObject("Scripting.FileSystemObject") fso.CreateFolder "C:\Temp\NewFolder" If Err.Number = 0 Then WScript.Echo "Folder created successfully" Else WScript.Echo "Error creating folder: " & Err.Description End If Set fso = Nothing
The script creates a FileSystemObject
and calls
CreateFolder
. The folder "NewFolder" is created in C:\Temp.
Error handling checks for success or failure. The script reports the outcome.
Creating Nested Folders
This example shows how to create a nested folder structure. It demonstrates creating multiple levels of directories. Each parent folder must exist before creating subfolders.
On Error Resume Next Set fso = CreateObject("Scripting.FileSystemObject") basePath = "C:\Projects" fso.CreateFolder basePath fso.CreateFolder fso.BuildPath(basePath, "Website") fso.CreateFolder fso.BuildPath(basePath, "Website\Images") If Err.Number = 0 Then WScript.Echo "Folder structure created successfully" Else WScript.Echo "Error creating folders: " & Err.Description End If Set fso = Nothing
The script first creates the base folder "C:\Projects". Then it creates
subfolders "Website" and "Website\Images". The BuildPath
method
helps construct proper paths. Error handling reports any issues.
Creating Folder with Current Date
This example demonstrates creating a folder named with the current date. It shows dynamic folder naming based on system date. The date format ensures folder names are sortable and unique.
On Error Resume Next Set fso = CreateObject("Scripting.FileSystemObject") folderName = "Backup_" & Year(Date) & "-" & Right("0" & Month(Date), 2) & "-" & Right("0" & Day(Date), 2) fso.CreateFolder "C:\Backups\" & folderName If Err.Number = 0 Then WScript.Echo "Dated folder created: " & folderName Else WScript.Echo "Error creating folder: " & Err.Description End If Set fso = Nothing
The script generates a folder name like "Backup_2025-04-09". It uses date functions to construct the name. The folder is created in C:\Backups. Error handling ensures the script reports success or failure.
Creating Folder with User Input
This example shows creating a folder based on user input. It demonstrates interactive folder creation. The script prompts the user for a folder name.
On Error Resume Next folderName = InputBox("Enter folder name to create in C:\Temp:") If folderName <> "" Then Set fso = CreateObject("Scripting.FileSystemObject") fso.CreateFolder "C:\Temp\" & folderName If Err.Number = 0 Then WScript.Echo "Folder created: C:\Temp\" & folderName Else WScript.Echo "Error: " & Err.Description End If Set fso = Nothing Else WScript.Echo "No folder name provided" End If
The script uses InputBox
to get user input. It creates the folder
in C:\Temp with the provided name. Error handling catches invalid names or
permission issues. The script provides feedback about the operation.
Checking Before Creating Folder
This example demonstrates checking for folder existence before creation. It
shows how to avoid errors when a folder already exists. The script uses
FolderExists
to check first.
Set fso = CreateObject("Scripting.FileSystemObject") folderPath = "C:\Temp\TestFolder" If Not fso.FolderExists(folderPath) Then fso.CreateFolder folderPath WScript.Echo "Folder created: " & folderPath Else WScript.Echo "Folder already exists: " & folderPath End If Set fso = Nothing
The script checks if the folder exists before attempting creation. If the folder doesn't exist, it creates it. If the folder exists, it informs the user. This approach prevents errors from duplicate folder creation.
Source
FileSystemObject Documentation
In this article, we have explored the CreateFolder
method in VBScript,
covering its usage and practical applications. From basic folder creation to
dynamic naming and user interaction, these examples demonstrate robust directory
management. With this knowledge, you can enhance your scripts with automated
folder creation capabilities.
Author
List all VBScript tutorials.