Visual Basic (VB) script has access to many
precompiled objects. These objects give you access to advanced
capabilities through the scripting language without having to program
the underlying code.
Objects have properties and methods that provide the means to interact with an instance of the object. For example, the filesystemobject provides access to the file system.
Note
Some objects also have events that perform an action when a specific trigger occurs.
Object Elements | Comments |
---|
Properties | Properties
are values that you can set or retrieve on an object. For example, a
file can have a name and a path. You can use the filesystemobject getabsolutepathname method to view the path of a file and the getfilename method to get the name of the file. |
Methods | Methods are actions that you can invoke. For example, filesystemobjectcreatetextfile to create a file, writeline to put text into the file, and close to close the file. includes methods such as |
The following code shows how to interact with the filesysptemobject, and the lines are explained in the following table.
set objfso = createobject("scripting.filesystemobject")
set txtfile = objfso.createtextfile("c:\scripts\log.txt",true)
txtfile.writeline("Logging an event")
txtfile.close
wscript.echo "getabsolutepathname = " & objfso.getabsolutepathname
("c:\scripts\log.txt")
wscript.echo "getfilename = " & objfso.getfilename("c:\scripts\log.txt")
if objfso.fileexists("c:\scripts\log.txt") then
wscript.echo "File exists."
end if
Code | Explanation |
---|
set objfso = createobject ("scripting.filesystemobject")
| This line creates an instance of the filesystemobject and names it objfso.
Note
It’s a common practice to prefix object names with the letters obj. Instead of naming it objfso, it can be named simply fso, or anything else such x or y. However, if you name it x, it’s clear what the variable represents, but if you name it objfso, it’s a reminder that it is an object representing the file’s system object.
|
set txtfile = objfso.createtextfile ("c:\scripts\log.txt",true)
| The createtextfile method creates a text file named c:\scripts\log.txt and creates an object named txtfile that refers to the created file.
Note
If the file exists, it will be overwritten because the value at the end of the line is true. If this value was omitted or set to false, the command would fail if the file exists.
|
txtfile.writeline("Logging an event")
| The writeline method writes the message into the text file. |
txtfile.close | The close method closes the file so that it no longer consumes resources and can be accessed by other processes. |
wscript.echo "getabsolutepathname = " & objfso.getabsolutepathname ("c:\scripts\log.txt")
| The getabsolutepathname method retrieves the full path and name of the file (c:\scripts\log.txt).
|
wscript.echo "getfilename = " & objfso.getfilename("c:\scripts\ log.txt")
| The getfilename method retrieves just the name of the file without the path and echoes it to the command prompt. |
if objfso.fileexists("c:\scripts\ log.txt") then wscript.echo "File exists." end if
| The last line in the script checks whether the file exists and uses the echo method to show that it does exist.
|
In addition to showing the output of the preceding script, Figure 1 also includes the line type c:\scripts\log.txt to show the contents of the log file. After running the script, it has only one line, “Logging an event.”
You can use the following steps to create this script.
Step | Action |
---|
1. | Start a command prompt. |
2. | Type the following text and press Enter:
notepad c:\scripts\fso.vbs
|
3. | When prompted to create the file, click Yes. |
4. | Add the following lines to the script:
set objfso = createobject("scripting.filesystemobject") set txtfile = objfso.createtextfile("c:\scripts\log.txt",true) txtfile.writeline("Logging an event") txtfile.close wscript.echo "getabsolutepathname = " & objfso.getabsolutepathname ("c:\scripts\log.txt") wscript.echo "getfilename = " & objfso.getfilename("c:\scripts\log.txt") if objfso.fileexists("c:\scripts\log.txt") then wscript.echo "File exists." end if
|
5. | Press Ctrl+S to save the script. |
6. | Return to the command prompt, type the following text, and press Enter:
Cscript c:\scripts\fs0.vbs
|