IT tutorials
 
Technology
 

Windows Server 2008 : Creating Basic Visual Basic Scripts - Working with filesystemobject

8/24/2013 9:21:53 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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 ElementsComments
PropertiesProperties 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.
MethodsMethods 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

CodeExplanation
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.closeThe 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.”

Figure 1. Viewing the results of the script

You can use the following steps to create this script.

StepAction
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


 
Others
 
- Windows 7 : Installing and Upgrading Programs - Common Installation Prompts (part 2) - Type of Installation
- Windows 7 : Installing and Upgrading Programs - Common Installation Prompts (part 1) - Compliance check , The End User License Agreement
- Windows 7 : Installing and Upgrading Programs - Installing and Upgrading from a Disk
- Windows Server 2012 : Using Capacity-Analysis Tools - Other Microsoft Assessment and Planning Tools, Third-Party Toolset
- Windows Server 2012 : Using Capacity-Analysis Tools - Windows Performance Monitor
- Windows Server 2012 : Using Capacity-Analysis Tools - Network Monitor (part 2) - Capturing Network Traffic Between Computers , Parsing Captured Network Traffic Data
- Windows Server 2012 : Using Capacity-Analysis Tools - Network Monitor (part 1) - Using Network Monitor 3.4
- Windows Server 2012 : Using Capacity-Analysis Tools - Task Manager
- Windows Server 2012 : Capacity Analysis and Performance Optimization - Defining Capacity Analysis
- Windows Phone 7 : AppHub and the Windows Marketplace for Mobile (part 2) - Marketplace on the Device, Limiting Distribution of Applications
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us