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.
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.
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
Copy and paste the example script below into notepad or a VBScript editor.
Decide whether to change the value for strDirectory (Particularly the drive letter).
Save the file with a .vbs extension, for example, NewFolder.vbs
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 http://computerperformance.co.uk/ ' Version 2.4 - September 2005 '
---------------------------------------------------------------' 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
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).
Guy
Recommends: A Free tool from SolarWinds: Config Generator
Config Generator (CG) 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'. This is a brand new free utility which Solarwinds released on January 26th
2010.
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 Script to Create a Folder
' NewFolderEC.vbs ' Free example VBScript to create a folder with error-correcting Code. ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.6 - May 2005 '
---------------------------------------------------------------'
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
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'.
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.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.