VBScript Create Folder
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.
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 (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.
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.
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.
' NewFolder.vbs
' Free example VBScript to create a folder (Simple)
' Author Guy Thomas http://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")
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 Tool
I like the Permissions 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!
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.
Option Explicit
Dim objFSO, objFolder, objShell, strDirectory
strDirectory = "c:\logs"
WScript.Quit
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.
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!
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.