Modern Methods of Working with Files and Folders

There is a hidden treasure in your computer called Windows Scripting Host (WSH), which allows you to create little programs that control the Windows operating system and its appli-

wshom.ocx Properties

Version | Security | Summary ¡

Description: Windows Scrçjl Host Runtime Library Copyright Copnight e Microsoft Coip. 1999

Other veííion inloímation Item ríame:

Company Name

Microsoft (r) Windows Scnprt j|

Internal Name

Host Runtime Library

Language

"r'a'r'3"r''Rnarr<i

Product Version

M

cations as well as retrieve information from the operating system. WSH is an ActiveX control found in the Wshom.ocx file. This file is automatically installed in the Windows Sys-tem32 folder if you are running Windows 95, 98, NT 5.0, 2000, XP or Internet Explorer 4, 5, or 6.

WSH is a scripting language. A script is a set of commands that can be run automatically. Scripts can be created and run directly from the command prompt by using the Command Scripting Host (Cscript.exe) or from Windows by using the Windows Scripting Host (Wscript.exe). In the following sections of this chapter, you will learn how the Windows Scripting Host works together with VBA.

WSH has its own object hierarchy. Using the CreateObject function, you can refer to WSH objects from your VBA procedure. Before you start writing VBA procedures that utilize WSH objects, let's take a look at some of the objects you will be able to control.

Cancel

Figure 8-5: Windows Scripting Host is an ActiveX control used to create scripts that perform simple or complex operations which previously could only be performed by writing batch files (.bat) in the MS-DOS operating system.

1. In the Visual Basic Editor window, choose Tools | References.

2. In the References dialog box, locate and select Microsoft Scripting Runtime.

References - FileMan

Available References:

□ Microsoft Remote Data Services Server 2.5 Library

□ Microsoft Rendezvous control 1.0 Type Library

□ Microsoft Repository

□ Microsoft Repository Add-In Type Library CH Microsoft Script Control 1.0

D Microsoft Scriptlet Library __i

□ Microsoft Sdpblb 1.0 Type Library

□ Microsoft Search - Protocol handler Helper Type Libra

□ Microsoft Search Codasses Type Library

□ Microsoft Search Interface Type Library

□ Microsoft Shell Controls And Automation

□ Microsoft Smart Tags 1.0 Type Library PI Microsoft SourceSafe 6,0 Tvoe Librarv a___r

Priority

Help

Microsoft Scripting Runtime

Location; C:\WINNT\System32\scrrun.dll Language: Standard

Figure 8-6:

Creating a reference to the Microsoft Scripting Runtime

3. Now press F2 to open the Object Browser.

4. In the combo box <All libraries> choose Scripting. You will see a list of objects that are part of the Windows Scripting Host library.

Object Browser

Scripting

| Classes

1 Members of'File' i

Q «globalst

*

I?

Attributes

Dictionary

if

Date Created

Si Drive

if?

DateLastAccessee

0 Drives

ËS1

DateLastModified

Encoder

¡ri?

Drive-

NIFile

if

Name

ÏS Filesf

el?

ParentFolder

Fi leEiystem Object

If

Path

S3 Folder

if

ShortName

® Folders:

if

ShortPath

S TextStrearn

d

if?

Size

.CI a s s File

±i

Member of Scni>tin<i

File object

_

_ li

After establishing a reference to the Microsoft Scripting Runtime (see Figure 8-6), the Object Browser displays many objects that allow you to work with disks, folders, files, and their content.

The Windows Scripting Host allows you to quickly obtain answers to such questions as "On which disk can I locate a particular file?" (GetDrive method), "What is the extension of a filename?" (GetExtensionName method), "When was this file last modified?" (DateLastModified property), and "Does this folder or file exist on a given drive?" (FolderExists and FileExists methods).

0 0

Post a comment