Вы находитесь на странице: 1из 140

VBScript 5.

:
:
Web:
e-mail:


http://av-mag.ru
avprog@narod.ru

http://av-mag.ru/books/vbscript.htm

. ,
(. ).
!
.

.
, , ,
. ,
,
,
.
1.
,
, 2.
.
2.
(,
..). -.
.

-. .
, !

VBScript 5.6

.
,
:
WebMoney:
R195975572634
Z167111238420
B180729885812
.:
4100188102149
,
,
. ,
:
http://how-get-wm.narod.ru

http://www.tz-5133.narod.ru
: , , , , , ,
.

http://www.avprog.narod.ru
, , , .

http://www.av-physics.narod.ru
.

http://av-photography.narod.ru
, .
GIMP.

http://av-assembler.ru
.

""
.

?
, .


, , , .

VBScript 5.6

VISUAL BASIC SCRIPT



2011 .

VBScript 5.6


1.
1.1. VBScript
1.2. ?
1.3. VBScript HTML-
1.3.1. <SCRIPT>
2. VBSCRIPT
2.1. VBScript
2.1.1.
2.1.2. VBScript
2.1.3. VBScript
2.1.4. VBScript
2.1.5.
2.1.6.
2.1.7. VBScript
2.1.8. VBScript
2.2. VBScript Internet Explorer
2.2.1. VBScript
2.2.2. VBScript
2.2.3. VBScript
3. FILESYSTEMOBJECT
3.1. FileSystemObject
3.2. FileSystemObject
Scripting Run-Time Library Reference
3.3. FileSystemObject
3.4. FileSystemObject
3.5.
3.6.
3.7. FileSystemObject
5.
5.1. VBScript
5.1.1. VBScript
5.1.2. VBA, VBScript
5.1.3. VBScript, VBA
5.1.4. Scripting Run-Time Library
Reference Features
5.2.
5.3.
5.3.1. VBScript
5.3.2.
5.3.3.
5.3.4.
5.3.5.
5.3.6.
5.3.7. MsgBox
5.3.8.
5.3.9.
5.3.10.

VBScript 5.6

5.4.
5.4.1. Initialize
5.4.2. Terminate
5.5.
5.6.
5.7.
5.7.1. Class
5.7.2. Dictionary
5.7.2.1. Dictionary
5.7.2.2. Dictionary
5.7.3. Err
5.7.3.1. Err
5.7.3.2. Err
5.7.4. FileSystemObject
5.7.4.1. FileSystemObject
5.7.4.2. FileSystemObject
5.7.5. Drive
5.7.5.1. Drive
5.7.6. File
5.7.6.1. File
5.7.6.2. File
5.7.7. TextStream
5.7.7.1. TextStream
5.7.7.2. TextStream
5.7.8. Match
5.7.8.1. Match
5.7.9. RegExp
5.7.9.1. RegExp
5.7.9.2. RegExp

VBScript 5.6

Visual Basic Scrip (VBScript).


VBScript 5.6, , ,
, .
VBScript 5.6,
Microsoft ( ).
, VBScript,
,
.
. ,
. ,
, . ,
:
http://www.avprog.narod.ru/progs/vbs/vbs-content.htm
( ) ,
,
.

VBScript 5.6

1.
( Script ) , ,
( ), - , -
.. ,
. - .
. :
JavaScript
JScript ( JavaScript)
Visual Basic Script (VBScript)
Delphi Script
Action Script
PHP
, (, JScript, VBScript)
,
Windows.
, .

( ) (
, ).
VBScript , , , JavaScript.
. ? :
1. ,
2. , ,
, , SCADA- ..
3. Microsoft ,
VBScript,
,
VBScript .
.
. -
,
http://www.avprog.narod.ru/progs/vbs/vbs-content.htm
.
, VBScript
.
VBScript Microsoft, ,
. , VBScript
Internet Explorer. ,

VBScript 5.6

1.1. VBScript
Microsoft Visual Basic Scripting Edition
, Web- Microsoft Internet Explorer Web Microsoft Internet Information Service.

Visual Basic Visual Basic for Applications (VBA), VBScript
. Visual Basic, VBScript
, Visual Basic.
, VBScript , .
.
http://www.avprog.narod.ru.

( , ,
..) HTML.
Windows Script
VBScript , Windows Script. Windows
Script
. Windows Script
, . Microsoft
VBScript. Microsoft Internet
Windows Script, . Windows Script
Microsoft Internet Explorer Microsoft Internet Information Service.
VBScript
VBScript,
. Microsoft VBScript 32 Windows API, 16- Windows API Macintosh. VBScript
World Wide Web (WWW). VBScript Windows Script
. , VBScript
HTML-
Internet Explorer.
.

1.2. ?
-
. , Hello, WORLD!.
,
Hello, WORLD!.
VBScript HTML- (
), .
(, )
vbs. , , HELLO.VBS.
, , MS Word, ,
, . ,
, , .

VBScript 5.6

,
, , .

. ,
, .
,
PSPad, :
http://www.pspad.com/ru/
. , HELLO.VBS. ,
:

MsgBox "Hello, WORLD!"


. Windows ,
. (
, , ))),
Hello, WORLD!
(. . 1).

. 1. .
. . . , ,
, MsgBox ,
. . ,
, , , .

- , SOURCE,
, CH_01,
hello.vbs.

VBScript 5.6

10

1.3. VBScript HTML-


SCRIPT VBScript HTML-.

1.3.1. <SCRIPT>
VBScript <SCRIPT>. ,
:
<SCRIPT LANGUAGE="VBScript">
<!-Function CanDeliver(Dt)
CanDeliver = (CDate(Dt) - Now()) > 2
End Function
-->
</SCRIPT>
<SCRIPT> .
LANGUAGE , .
, .
, CanDeliver (<!-- -->).
, , <SCRIPT>,
.
, -
, HEAD HTML:
<HTML>
<HEAD>
<TITLE>Place Your Order</TITLE>
<SCRIPT LANGUAGE="VBScript">
<!-Function CanDeliver(Dt)
CanDeliver = (CDate(Dt) - Now()) > 2
End Function
-->
</SCRIPT>
</HEAD>
<BODY>
...
SCRIPT HTML-.
BODY, HEAD. , ,
HEAD, .
HEAD ,
BODY.

VBScript 5.6

11


. ,
, ,
:
<HTML>
<HEAD>
<TITLE>Test Button Events</TITLE>
</HEAD>
<BODY>
<FORM NAME="Form1">
<INPUT TYPE="Button" NAME="Button1" VALUE="Click">
<SCRIPT FOR="Button1" EVENT="onClick" LANGUAGE="VBScript">
MsgBox "Button Pressed!"
</SCRIPT>
</FORM>
</BODY>
</HTML>
Sub Function,
. VBScript
, SCRIPT.
.
. .

VBScript 5.6

12

2. VBSCRIPT
2.1. VBScript
2.1.1.

.
, VBSCript.
- VBScript , Variant. Variant
, .
.
, -
.
Variant , .
,
,
. , VBScript ,
. ,
, . ,
VBScript ,
(" ").
, Num.
:

Num = 5 + 10
Num = ": " & Num
MsgBox Num
. Num
. "+", "5 + 10" , 15. -
, "&",
. ,
"15", MsgBox.

Num = ": " & Num


Num = ": " + Num


,
(
!!!, ).
VBScript Variant, VBScript
Variant.

VBScript 5.6

13

Variant:
Variant
. ,
, .
, .

Boolean . ,
Variant . ,
, Variant, Variant
, .
, VBScript . ,
:

Num = True
Num = Num + 10
,
. , VBScript ,
. ,
Variant, ,
.
, Variant.

Empty
Null
Boolean
Byte
Integer
Currency
Long
Single
Double

Date (Time)
String
Object
Error

Variant. 0
() .
.
, True () False
()
0255.
-3276832767
, -922337203685477,5808
922337203685477,5807.
-21474836482147483647
-3.402823E38 1.401298E-45 1.401298E-45
3.402823E38 .
-1.79769313486232E308 4.94065645841247E-324
4.94065645841247E-324 1.79769313486232E308
.
, 1-
100 31 9999 .
, 2
.
.
.


. , VarType
, Variant.

VBScript 5.6

14

2.1.2. VBScript

,
,
. (). ,
, , -
. ,
ClickCount -.
, .
,
.
. VBScript Variant (.
VBScript).


Dim, Public
Private. :

Dim DegreesFahrenheit
, , :

Dim Top, Bottom, Left, Right


:

Dim
Dim
Dim
Dim

Top
Bottom
Left
Right

'
'
'
'

,
.
,
. ,
.
, .
, Top,
Top . ,
,
. ,
Option Explicit, .
Option Explicit . :

s = "Test VAR"
MsgBox s
, s .

VBScript 5.6

15

Option Explicit
s = "Test VAR"
MsgBox s
, s
(. . 2.1).

. 2.1. .
, ,
Option Explicit. ,
, - . , Right, -
Right Riht.
Option Explicit, , Riht
.
, Riht, Right,
, Right,
Riht.



VBScript. :


255
,


,
. , , ,
.
. ,
.
.

VBScript 5.6

16

, .

.
.
.
.
, .
, .


(
, ):

B = 200
200.
=.


. ,
.
.
, .
. , ,
.
, 11 :

Dim (10)
10, , ,
11 . , VBScript 0,
1.
1.
.

. ( 11) 0 10
:

A(0) = 256
A(1) = 324
A(2) = 100
. . .
A(10) = 55
:

. . .
SomeVariable = A(8)
. . .

VBScript 5.6

17

. 60
, , 3 4 .

. ,
6 11 :

Dim MyTable(5, 10)


, .
,
. .
Dim ReDim.

. :

Dim MyArray()
ReDim AnotherArray()

ReDim .
, 25.
30, Preserve,
. ,
.

ReDim MyArray(25)
. . .
ReDim Preserve MyArray(30)
,
, ,
.

2.1.3. VBScript

,
. , , .
VBScript ,
. , -
. ,
- .
. ,
,
.

VBScript 5.6

18


VBScript Const.
,
. :

Const MyString = " ."


Const MyAge = 49
, (" "). ,
, .
(#). :

Const CutoffDate = #6-1-97#


, .
. ,
vb con ,
.
,
.

2.1.4. VBScript
VBScript , ,
, () .


,

. .

- , ,
. , , ,
, .
.
,
, ,
.
. ,
, .
,
.
,
, , .
, ,
, ,
.

VBScript 5.6

19

Mod

()

&

<>

<

>

<=

>=

Is

Not

And

Or

Xor

Eqv

Imp

() (&) ,

. Is .
, ,
.

VBScript 5.6

20

2.1.5.


. VBScript,
. VBScript
:
If...Then...Else
Select Case

If...Then...Else
If...Then...Else
True () False (). -
. , (5 + 5 = 10) True,
. (2 * 2 = 5) False, 2 * 2 = 4,
5. -
.
. ,
, .
:

Dim x, y
x = 2
y = 5
If (x * x) = y Then
MsgBox " "
Else
MsgBox " "
End If
, = 5, ( * ) = , False.
, , Else.
4, ( * ) = ,
, , Then,
Else . . ,
, If...Then...Else
....... , ,
:



( )

( If...Then...Else)
,
, , ,
.. . .
If...Then...Else .
.

VBScript 5.6

21

, TRUE
, , True,
If...Then...Else.
. , Else.

Sub FixDate()
Dim myDate
myDate = #2/13/95#
If myDate < Now Then myDate = Now
End Sub
,
( ). End If,
:

Sub AlertUser(value)
If value = 0 Then
AlertLabel.ForeColor = vbRed
AlertLabel.Font.Bold = True
AlertLabel.Font.Italic = True
End If
End Sub
, TRUE,
, FALSE
If...Then...Else
( ,
). , (
TRUE), , ( FALSE).

Sub AlertUser(value)
If value = 0 Then
AlertLabel.ForeColor =
AlertLabel.Font.Bold =
AlertLabel.Font.Italic
Else
AlertLabel.Forecolor =
AlertLabel.Font.Bold =
AlertLabel.Font.Italic
End If
End Sub

vbRed
True
= True
vbBlack
False
= False

VBScript 5.6

22


If...Then...Else .
ElseIf If...Then...Else
, ,
. :

Sub ReportValue(value)
If value = 0 Then
MsgBox value
ElseIf value = 1 Then
MsgBox value
ElseIf value = 2 then
Msgbox value
Else
Msgbox " !"
End If
End Sub
ElseIf, ,
. ElseIf
.
Select Case.

Select Case
Select Case If...Then...ElseIf
.
Select Case If...Then...ElseIf,
.
Select Case , (.
).
Case Select Case. ,
, ,
Case , Case End Select.
:

Select Case x
Case x = 1
MsgBox "x = 1"
Case x = 2
MsgBox "x = 2"
Case x = 3
MsgBox "x = 3"
Case Else
MsgBox " "
End Select
. 1,
= 1, 2, = 2, 3,
= 3, ,
.
, , Case Else.
,
Select Case.

VBScript 5.6

23

, Select Case ,
If...Then...ElseIf,
ElseIf. Select Case
If...Then...ElseIf ,
ElseIf .

2.1.6.

.
, ( False), ,
( True). ,
. VBScript :
Do...Loop. , ,

While...Wend. .
For...Next. .
For Each...Next.
.

Do Loop
Do...Loop
. (
TRUE) , .
,
While Do...Loop.
( ChkFirstWhile)
( ChkLastWhile).
ChkFirstWhile myNum 10 20
, 10 . myNum
10 , ,
. ChkLastWhile .
, - ,
.

Sub ChkFirstWhile()
Dim counter, myNum
counter = 0
myNum = 20
Do While myNum > 10
myNum = myNum - 1
counter = counter + 1
Loop
MsgBox " " & counter & " , myNum = " & myNum
End Sub

VBScript 5.6

24

Sub ChkLastWhile()
Dim counter, myNum
counter = 0
myNum = 9
Do
myNum = myNum - 1
counter = counter + 1
Loop While myNum > 10
MsgBox " " & counter & " , myNum = " & myNum
End Sub

,
Until Do...Loop.
( ChkFirstUntil)
( ChkLastUntil). ,
( myNum 10). myNum 10,
.

Sub ChkFirstUntil()
Dim counter, myNum
counter = 0
myNum = 20
Do Until myNum = 10
myNum = myNum - 1
counter = counter + 1
Loop
MsgBox " " & counter & " , myNum = " & myNum
End Sub
Sub ChkLastUntil()
Dim counter, myNum
counter = 0
myNum = 1
Do
myNum = myNum + 1
counter = counter + 1
Loop Until myNum = 10
MsgBox " " & counter & " , myNum = " & myNum
End Sub

VBScript 5.6

25


, .
Exit Do.
, , ,
, Exit Do
If...Then...Else. , -
, Exit
Do, .

Sub ExitExample()
Dim counter, myNum
counter = 0
myNum = 9
Do Until myNum = 10
myNum = myNum - 1
counter = counter + 1
if counter > 100 Then Exit Do
Loop
MsgBox " " & counter & " , myNum = " & myNum
End Sub
myNum ,
, .
, .
myNum 9, ,
10, , . ,
( counter).
100 , Exit
Do.

While Wend
While...Wend VBScript ,
. , While...Wend ,
Do...Loop.

For Next
For...Next ,
. ,
.
, 50 . For
- ,
. Next 1.

Sub DoMyProc50Times()
Dim x
For x = 1 To 50
MyProc
Next
End Sub

VBScript 5.6

26

Step,
( 1, ).
- j 2 . ,
2, 4, 6, 8 10, 30.

Sub TwosTotal()
Dim j, s, total
For j = 2 To 10 Step 2
If j = 10 Then
s = s & j & ") = "
Else
s = s & j & "+"
End If
total = total + j
Next
MsgBox " : (" & s & total
End Sub
-
Step. ,
. - myNum 2
. , 16, 14, 12,
10, 8, 6, 4 2, 72.

Sub NewTotal()
Dim myNum, total
For myNum = 16 To 2 Step -2
total = total + myNum
Next
MsgBox " : " & total
End Sub
, - .
Exit For.
. Do...Loop.

For Each Next


For Each...Next For...Next,
,
.
, , .
HTML-, , Dictionary,
.
, .
For Each...Next, d
/ . , , ,
- (, , ).
, .
, ,
, .

VBScript 5.6

27

For Each...Next -

Document.frmForm.Elements(i).Value = d.Item(i)
HTML-. .
. , ,
.
. (
Dictionary), . For Each...Next
. For...Next
/ HTML-.
Dictionary .

<html>
<head><title> </title></head>
<SCRIPT LANGUAGE="VBScript">
<!-sub cmdChange_OnClick
dim M()
'
dim d
'
dim n
'
set d = CreateObject("Scripting.Dictionary")
d.Add "0", ""
'
d.Add "1", ""
d.Add "2", ""
n = 0
for each i in d
'
ReDim Preserve M(n+1)
M(n) = d.Item(i)
n = n + 1
next
for i = 0 To (n-1)
' /
Document.frmForm.Elements(i).Value = M(i)
Next
end sub
-->
</SCRIPT>
<body>
<center>
<form NAME="frmForm">
<input Type = "Text"><p>
<input Type = "Text"><p>
<input Type = "Text"><p>
<input Type = "Button" NAME="cmdChange" VALUE=" "><p>
</form>
</center>
</body>
</html>

VBScript 5.6

28

2.1.7. VBScript

VBScript : (Sub) (Function).

VBScript ,
Sub End Sub. ,
( ).
(), , .
,
.
, ,
VBScript: MsgBox InputBox,
. ,
.
Celsius, VBScript .

Sub ConvertTemp()
temp = InputBox(" , F", 1)
MsgBox " , = " & Celsius(temp)
End Sub

VBScript ,
Function End Function. ,
( ).
(), ,
. ,
. ,
,
. Variant.
, ,
(. ).
. ConvertTemp.
.

Function Celsius(fDegrees)
Celsius = (fDegrees - 32) * 5 / 9
End Function



(). ,
. ,
. ,
.
. , .

VBScript 5.6

29

(
), . ,
, . , ,
. ,
.


,
- . :

temp = Celsius(100)

MsgBox " , =

" & Celsius(temp)


, .
. Call ,
, .
MyProc :
Call. .

Call MyProc(firstarg, secondarg)


MyProc firstarg, secondarg
, ,
Call.

, .
. VBScript .
, .
, ,
.
.

' ,
Erase ArrayOfNumbers
' ,
' .
Response = MsgBox("Continue?", vbYesNo, "Confirm")
' ,
' .
MsgBox "Finished.", vbOKOnly, "Information"

VBScript 5.6

30

2.1.8. VBScript
?
, , Microsoft Visual
Basic Scripting Edition. :
,



,
.
, ,
.


VBScript .
, , ,
.
, (_). :

USER_LIST_MAX
NEW_LINE
.
.
Const,
con. :

conYourOwnConstant

,
, .

Boolean

bln

blnFound

Byte

byt

bytRasterData

Date (Time)

dtm

dtmStart

Double

dbl

dblTolerance

Error

err

errOrderNum

Integer

int

intQuantity

Long

lng

lngDistance

Object

obj

objCurrent

Single

sng

sngAverage

String

str

strFirstName

VBScript 5.6

31


,
. VBScrip
:

HEAD HTML-, ,


, ,
, .
. .


dblVelocity
sblnCalcInProgress


,
.
. ,
InitNameArray CloseDialog.

. 32
. , ,
. , Cnt Count
( 2.3.2.
VBScript).

VBScript 5.6

32


,
VBScript.

3D Panel

pnl

pnlGroup

Animated button

ani

aniMailBox

Check box

chk

chkReadOnly

Combo box, drop-down list box

cbo

cboEnglish

Command button

cmd

cmdExit

Common dialog

dlg

dlgFileOpen

Frame

fra

fraLanguage

Horizontal scroll bar

hsb

hsbVolume

Image

img

imgIcon

Label

lbl

lblHelpMessage

Line

lin

linVertical

List Box

lst

lstPolicyCodes

Spin

spn

spnPages

Text box

txt

txtLastName

Vertical scroll bar

bar

vsbRate

Slider

sld

sldScale


,
. (
),
, ,
.
, .
, .
,
, .
, ,
( . ):


( ).
, ,
.
,
.
, .
.
.

VBScript 5.6

33

:

.
, ,

, .
,
, , , .
.


.

. :
4 .

.
4 ,
4
.
, .
, , .
. ,
Microsoft ( ))),
, Microsoft.

'**************************************************************
' :
'
strUserList.
'
' : strUserList():
.
'
strTargetUser:
.
' :
strUserList,
'
strTargetUser.
'
,
'
-1.
'**************************************************************
Function intFindUser (strUserList(), strTargetUser)
Dim i
'
Dim blnFound
'
intFindUser = -1
i = 0
'
Do While i <= Ubound(strUserList) and Not blnFound
If strUserList(i) = strTargetUser Then
blnFound = True
' True
intFindUser = i
'
End If
i = i + 1
' 1
Loop
End Function

VBScript 5.6

34

2.2. VBScript Internet Explorer


2.2.1. VBScript
-
-, ,
Internet Explorer ( VBScript).
, VBScript .

<html>
<head><title> -</title>
<SCRIPT LANGUAGE="VBScript">
<!-sub Button1_OnClick
MsgBox " , "
end sub
-->
</SCRIPT>
</head>
<body>
<h3> -</h3><hr>
<form><input NAME="Button1" TYPE="BUTTON" VALUE=" "></form>
</body>
</html>
.
.
Mirabile visu, (,
))). , .
Internet Explorer , <SCRIPT>, ,
VBScript . , ,
Internet Explorer .
, <SCRIPT> .
: Button_1 OnClick ( ).
NAME <INPUT>.
(_). ,
, Internet Explorer ,
( Button1_OnClick) .
Internet Explorer , ,
Internet Explorer Scripting Object Model.
Microsoft (http://www.microsoft.com).
.
2.2.2. VBScript
.

VBScript 5.6

35


,
, VBScript . Internet
Explorer ,
. ,
, . <INPUT>.

<input NAME="Button2" TYPE="BUTTON" VALUE=" " OnClick='MsgBox


" , INPUT"'>
, ,
MsgBox . ,
(:).
<SCRIPT> ,
:

<SCRIPT LANGUAGE="VBScript" EVENT="OnClick" FOR="Button3">


<!-MsgBox " "
-->
</SCRIPT>
<SCRIPT> ,
Sub End Sub .

2.2.2. VBScript

VBScript .
- .
. -
.
, 2.4.1. VBScript,
Value .
, .
,
. Document,
-. , Document
. , ,
, :

Document.ValidForm.Text1
,
TheForm . ,
, Set, TheForm:

set TheForm = Document.ValidForm

VBScript 5.6

36

-:

<html>
<head><title> </title>
<SCRIPT LANGUAGE="VBScript">
<!-sub Button1_OnClick
dim TheForm
set TheForm = Document.ValidForm
if IsNumeric(TheForm.Text1.Value) then
if TheForm.Text1.Value < 1 or TheForm.Text1.Value > 10 then
MsgBox " 1 10"
else
MsgBox ""
end if
else
MsgBox " "
end if
end sub
-->
</SCRIPT>
</head>
<body>
<h3> </h3><hr>
<form NAME="ValidForm">
1 10:
<input NAME="Text1" TYPE="TEXT" SIZE="2">
<input NAME="Button1" TYPE="BUTTON" VALUE="">
</form>
</body>
</html>

.
IsNumeric, TRUE,
. , FALSE.
.
, .
, VBScript ,

.

, + ()
, . ,
Text1 1, Text2 2, :

A = Text1.Value + Text2.Value
A = CDbl(Text1.Value) + Text2.Value

' A "12"
' A 3

VBScript 5.6

37


.
Submit, ,
. Submit ,
. (
),
:

<SCRIPT LANGUAGE="VBScript">
<!-Sub Button2_OnClick
Dim TheForm
Set TheForm = Document.ValidForm
If IsNumeric(TheForm.Text1.Value) Then
If TheForm.Text1.Value < 1 Or TheForm.Text1.Value > 10 Then
MsgBox " 1 10"
Else
MsgBox ""
TheForm.Submit ' , .
End If
Else
MsgBox " "
End If
End Sub
-->
</SCRIPT>
Submit Form.
. Submit
Internet Explorer Scripting Object Model.
Microsoft (http://www.microsoft.com).
HTML <FORM>. Internet Explorer
ActiveX (
OLE) Java .

2.2.3. VBScript

, ActiveX ( OLE) Java
, Microsoft Visual Basic Scripting Edition Internet Explorer
. Internet Explorer
Label, .
, <OBJECT>
<PARAMS>. Visual Basic, ,
<PARAMS>
.

VBScript 5.6

38

, ActiveX
Label:

<object
classid="clsid:99B42120-6EC7-11CF-A6C7-00AA00A47DD2"
id=lblActiveLbl
width=250
height=250
align=left
hspace=20
vspace=0
>
<param NAME="Angle" VALUE="90">
<param NAME="Alignment" VALUE="4">
<param NAME="BackStyle" VALUE="0">
<param NAME="Caption" VALUE="A Simple Desultory Label">
<param NAME="FontName" VALUE="Verdana, Arial, Helvetica">
<param NAME="FontSize" VALUE="20">
<param NAME="FontBold" VALUE="1">
<param NAME="FrColor" VALUE="0">
</object>
,
. ,
<FORM>, Label.

<form NAME="LabelControls">
<input TYPE="TEXT" NAME="txtNewText" SIZE=25>
<input TYPE="BUTTON" NAME="cmdChangeIt" VALUE=" ">
<input TYPE="BUTTON" NAME="cmdRotate" VALUE="">
</form>
, cmdChangeIt, Label:

<SCRIPT LANGUAGE="VBScript">
<!-sub cmdChangeIt_onClick
dim TheForm
set TheForm = Document.LabelControls
lblActiveLbl.Caption = TheForm.txtNewText.Value
end sub
-->
</SCRIPT>
, /
, 2.4.2. VBScript .
ActiveX Internet Explorer.
, ,
(CLSID) Microsoft
(http://www.microsoft.com). <OBJECT>
Internet Explorer 4.0 Author's Guide and HTML Reference.

VBScript 5.6

39

Internet Explorer ({})


, W3C.
This page uses an outdated version of the
<OBJECT> tag
, , ,
<OBJECT>.
. . ( ), Label
, .
, .
,
<IMG>.

<html>
<head><title> </title>
<SCRIPT LANGUAGE="VBScript">
<!-sub cmdPlusSize_onClick
imgTest.Width = imgTest.Width * 1.1
imgTest.height = imgTest.height * 1.1
end sub
sub cmdMinusSize_onClick
imgTest.Width = imgTest.Width / 1.1
imgTest.height = imgTest.height / 1.1
end sub
-->
</SCRIPT>
</head>
<body>
<h3> </h3><hr>
<img
border="0"
src="vbs-error.jpg"
width="542"
height="204"
id=imgTest
>
<form NAME="LabelControls">
<input TYPE="BUTTON" NAME="cmdPlusSize" VALUE=" 10%">
<input TYPE="BUTTON" NAME="cmdMinusSize" VALUE=" 10%">
</form>
</body>
</html>
10% ,
. ,
.

VBScript 5.6

40

3. FILESYSTEMOBJECT
3.1. FileSystemObject
ASP, Windows Scripting Host ,
, , ,
() -.
, .
, ,
FileSystemObject (FSO), .

3.2. FileSystemObject Scripting Run-Time Library


Reference
FileSystemObject (FSO)
. ,
.
:
HTML -
Windows Scripting Host Windows
Script Control ,

FSO

,
FSO , -
. , Internet Explorer
,
FileSystemObject .
,
.
FSO ,
, ,
, . ,
, ..
FSO .
. ,
, . ,
Access SQL Server, ,
. ,
,
.
FSO Scripting type library (Scrrun.dll),

TextStream. ,
.

VBScript 5.6

41

3.3. FileSystemObject
FileSystemObject (FSO) :
/

FileSystemObject

. , , ,
, . ,
, FSO ( ).

Drive

. , ,
. , Drive - .
CD-ROM, - ..
. .

Drives

. , ,
. , . , (
- CD-ROM, ,
).

File

. , ,
. , .

Files

. , .

Folder

. , ,
. , .

Folders

. , Folder.

TextStream

. .

3.4. FileSystemObject
FileSystemObject (FSO):
CreateObject FileSystemObject


FSO Scripting type library,
Scrrun.dll.
-, FileSystemObject.

FileSystemObject
FileSystemObject, CreateObject. VBScript
:

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Scripting , FileSystemObject ,
. FileSystemObject,
, .

VBScript 5.6

42


FileSystemObject. ,
, CreateTextFile CreateFolder (
FSO ).
DeleteFile DeleteFolder
FileSystemObject. ,
.

FileSystemObject . ,
CopyFile FileSystemObject,
Copy File. ,
.

,
, , get
FileSystemObject:
GetDrive
GetFolder
GetFile
VBScript:

Dim fso, f1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.GetFile("c:\test.txt")
get , create
. , ,
CreateFolder, GetFolder , ,
. CreateFolder ,
, ,
. :

Sub CreateFolder
Dim fso, fldr
Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.CreateFolder("C:\MyTest")
End Sub

, . ,
, ,
(
GetFolder, ).
GetFolder:

Set fldr = fso.GetFolder("c:\Windows")

VBScript 5.6

43

, Folder, Name
():

MsgBox " : " & fldr.Name


, :

Dim fso, f1
' File.
Set f1 = fso.GetFile("c:\Windows\desktop.ini")
' .
MsgBox " : " & f1.DateLastModified

3.5.
FileSystemObject (FSO)
, Windows .
, ..


Drive , .
, .
:
( TotalSize)
( AvailableSpace FreeSpace)
( DriveLetter)
: , , , CD-ROM RAM- (
DriveType)
( SerialNumber)
: FAT, FAT32, NTFS . ( FileSystem)
( IsReady)
( ShareName VolumeName)
( Path RootFolder)
. 3.7.
FileSystemObject.

Drive
Drive .
Drive . GetDrive
Drive ( drv).

VBScript 5.6

44

Drive VBScript:

Sub ShowDriveInfo(drvPath)
Dim fso, drv, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set drv = fso.GetDrive(fso.GetDriveName(drvPath))
s = " " & UCase(drvPath) & " - "
s = s & drv.VolumeName
s = s & ", : " & FormatNumber(drv.TotalSize / 1024, 0)
s = s & " Kb"
s = s & ", : " & FormatNumber(drv.FreeSpace / 1024, 0)
s = s & " Kb"
MsgBox s
End Sub
ShowDriveInfo("C:")

FileSystemObject.CreateFolder

Folder.Delete FileSystemObject.DeleteFolder

Folder.Move FileSystemObject.MoveFolder

Folder.Copy FileSystemObject.CopyFolder

Folder.Name

FileSystemObject.FolderExists

Folder FileSystemObject.GetFolder

FileSystemObject.GetParentFolderName

FileSystemObject.GetSpecialFolder

. 3.7.
FileSystemObject.

VBScript 5.6

45

Folder FileSystemObject,
:

Sub ShowFolderInfo()
Dim fso, fldr, s
' FileSystemObject.
Set fso = CreateObject("Scripting.FileSystemObject")
' Drive.
Set fldr = fso.GetFolder("c:")
' .
MsgBox " : " & fldr
' .
MsgBox " " & fldr.Drive
' .
If fldr.IsRootFolder = True Then
MsgBox " "
Else
MsgBox " "
End If
'
fso.CreateFolder ("C:\Bogus")
MsgBox " C:\Bogus"
'
MsgBox " = " & fso.GetBaseName("c:\bogus")
'
fso.DeleteFolder ("C:\Bogus")
MsgBox " C:\Bogus"
End Sub

3.6.
:
,
,


( text
stream).
CreateTextFile.
, , :

Dim fso, f1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
. 3.7.
FileSystemObject.

VBScript 5.6

46

OpenTextFile FileSystemObject
ForWriting. , ,
:

Dim fso, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting. FileSystemObject")
Set ts = fso.OpenTextFile("c:\test.txt", ForWriting, True)
OpenAsTextStream
ForWriting. , ,
:

Dim fso, f1, ts


Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("c:\test1.txt")
Set f1 = fso.GetFile("c:\test1.txt")
Set ts = f1.OpenAsTextStream(ForWriting, True)


, ,
:



OpenTextFile
FileSystemObject OpenAsTextStream File.
Write, WriteLine
WriteBlankLines TextStream, ,
:

Write

WriteLine

WriteBlankLines

Close TextStream.
Close FileSystemObject.
. 3.7. FileSystemObject.

( )
( / ).
, .

VBScript 5.6

47

,
, :

Sub CreateFile()
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile("c:\testwritefile.txt", True)
' .
tf.WriteLine(" 1, 2, 3.")
' .
tf.WriteBlankLines(3)
'
tf.Write (" ")
tf.Close
End Sub


Read, ReadLine ReadAll
TextStream. , ,
:

Read

( , )

ReadLine

ReadAll

. 3.7.
FileSystemObject.
Read ReadLine
, Scip ScipLine.
,
, (,
Left, Right Mid), ..
, , ,
:

Sub ReadFiles
Dim fso, f1, ts, s
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testreadfile.txt", True)
'
f1.WriteLine "Hello World"
f1.WriteBlankLines(1)
f1.Close
'
Set ts = fso.OpenTextFile("c:\testreadfile.txt", ForReading)
s = ts.ReadLine
MsgBox " = '" & s & "'"
ts.Close
End Sub

VBScript 5.6

48

,
FSO ,
. , ,
:

File.Move FileSystemObject.MoveFile

File.Copy FileSystemObject.CopyFile

File.Delete FileSystemObject.DeleteFile

. 3.7.
FileSystemObject.
,
. \tmp,
\temp, .
tmp
temp.

Sub ManipFiles
Dim fso, f1, f2, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
'
f1.Write ("This is a test.")
'
f1.Close
' C:\.
Set f2 = fso.GetFile("c:\testfile.txt")
' \tmp
f2.Move ("c:\tmp\testfile.txt")
' \temp.
f2.Copy ("c:\temp\testfile.txt")
'
Set f2 = fso.GetFile("c:\tmp\testfile.txt")
Set f3 = fso.GetFile("c:\temp\testfile.txt")
'
f2.Delete
f3.Delete
MsgBox " !"
End Sub

VBScript 5.6

49

3.7. FileSystemObject
, ,
FileSystemObject .
,
.
, ,
.
Active Server Pages Windows Scripting Host.
Active Server Pages, :
1.
2.
3.
4.

- .asp
<BODY>...</BODY>
<%...%>
Option Explicit
, <HTML>
5. Option Explicit <%...%> ,

6. :
Sub Print(x)
Response.Write "<PRE><FONT FACE=""Courier New"" SIZE=""1"">"
Response.Write x
Response.Write "</FONT></PRE>"
End Sub
Main
,
, .
Windows Scripting Host
:
Sub Print(x)
WScript.Echo x
End Sub
Main
FileSystemObject:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' FileSystemObject
'
' Copyright 1998 Microsoft Corporation. All Rights Reserved.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit

VBScript 5.6
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' :
'
' 1)
'
"&". ,
'
.
'
,
'
, ,
'
, . ,
'
, .
'
' 2) "Option Explicit" ,
'
, .
'
, , , DriveTypeCDORM DriveTypeCDROM.
'
' 3) , .
'
, , ,
'
.
'
On Error Resume Next Err .
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
'
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim TabStop
Dim NewLine
Const TestDrive = "C"
Const TestFilePath = "C:\Test"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' , Drive.DriveType
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const
Const
Const
Const
Const

DriveTypeRemovable = 1
DriveTypeFixed = 2
DriveTypeNetwork = 3
DriveTypeCDROM = 4
DriveTypeRAMDisk = 5

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' , File.Attributes
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const FileAttrNormal = 0
Const FileAttrReadOnly = 1
Const FileAttrHidden = 2
Const FileAttrSystem = 4
Const FileAttrVolume = 8
Const FileAttrDirectory = 16
Const FileAttrArchive = 32
Const FileAttrAlias = 64
Const FileAttrCompressed = 128
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
'
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const OpenFileForReading = 1
Const OpenFileForWriting = 2
Const OpenFileForAppending = 8

50

VBScript 5.6

51

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' ShowDriveType
'
' :
'
' , Drive.
'
'
'
' - Drive.DriveType
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ShowDriveType(Drive)
Dim S
Select Case Drive.DriveType
Case DriveTypeRemovable
S = ""
Case DriveTypeFixed
S = ""
Case DriveTypeNetwork
S = ""
Case DriveTypeCDROM
S = "CD-ROM"
Case DriveTypeRAMDisk
S = "RAM-"
Case Else
S = ""
End Select
ShowDriveType = S
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' ShowFileAttr
'
' :
'
' , .
'
'
'
' - File.Attributes
' - Folder.Attributes
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ShowFileAttr(File) ' File -
Dim S
Dim Attr
Attr = File.Attributes
If Attr = 0 Then
ShowFileAttr = "Normal"
Exit Function
End If
If
If
If
If
If
If
If
If

Attr
Attr
Attr
Attr
Attr
Attr
Attr
Attr

And
And
And
And
And
And
And
And

FileAttrDirectory
FileAttrReadOnly
FileAttrHidden
FileAttrSystem
FileAttrVolume
FileAttrArchive
FileAttrAlias
FileAttrCompressed

ShowFileAttr = S
End Function

Then
Then
Then
Then
Then
Then
Then
Then

S
S
S
S
S
S
S
S

=
=
=
=
=
=
=
=

S
S
S
S
S
S
S
S

&
&
&
&
&
&
&
&

" "
" "
" "
" "
" "
" "
" "
" "

VBScript 5.6

52

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' GenerateDriveInformation
'
' :
'
' , .
'
'
'
' - FileSystemObject.Drives
' - Iterating the Drives collection
' - Drives.Count
' - Drive.AvailableSpace
' - Drive.DriveLetter
' - Drive.DriveType
' - Drive.FileSystem
' - Drive.FreeSpace
' - Drive.IsReady
' - Drive.Path
' - Drive.SerialNumber
' - Drive.ShareName
' - Drive.TotalSize
' - Drive.VolumeName
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GenerateDriveInformation(FSO)
Dim Drives
Dim Drive
Dim S
Set Drives = FSO.Drives
S = " :" & TabStop & Drives.Count & NewLine & NewLine
'
S
S
S
S
S
S


= S & String(2,
= S & String(3,
= S & TabStop &
= S & TabStop &
= S & TabStop &
= S & TabStop &

.
TabStop) & ""
TabStop) & ""
""
""
""
"" & NewLine

'
S
S
S
S
S
S
S
S
S
S


= S & ""
= S & TabStop &
= S & TabStop &
= S & TabStop &
= S & TabStop &
= S & TabStop &
= S & TabStop &
= S & TabStop &
= S & TabStop &
= S & TabStop &

.
""
""
""
""
""
""
""
""
"" & NewLine

' .
S = S & String(105, "-") & NewLine

VBScript 5.6

53

For Each Drive In Drives


S
S
S
S

=
=
=
=

S
S
S
S

&
&
&
&

Drive.DriveLetter
TabStop & Drive.Path
TabStop & ShowDriveType(Drive)
TabStop & Drive.IsReady

If Drive.IsReady Then
If DriveTypeNetwork = Drive.DriveType Then
S = S & TabStop & Drive.ShareName
Else
S = S & TabStop & Drive.VolumeName
End If
S
S
S
S
S

=
=
=
=
=

S
S
S
S
S

&
&
&
&
&

TabStop
TabStop
TabStop
TabStop
TabStop

&
&
&
&
&

Drive.FileSystem
Drive.TotalSize
Drive.FreeSpace
Drive.AvailableSpace
Hex(Drive.SerialNumber)

End If
S = S & NewLine
Next
GenerateDriveInformation = S
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' GenerateFileInformation
'
' :
'
' , .
'
'
'
' - File.Path
' - File.Name
' - File.Type
' - File.DateCreated
' - File.DateLastAccessed
' - File.DateLastModified
' - File.Size
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GenerateFileInformation(File)
Dim S
S
S
S
S
S
S
S
S

=
=
=
=
=
=
=
=

NewLine & ":" & TabStop & File.Path


S & NewLine & ":" & TabStop & File.Name
S & NewLine & ":" & TabStop & File.Type
S & NewLine & ":" & TabStop & ShowFileAttr(File)
S & NewLine & ":" & TabStop & File.DateCreated
S & NewLine & ":" & TabStop & File.DateLastAccessed
S & NewLine & ":" & TabStop & File.DateLastModified
S & NewLine & "" & TabStop & File.Size & NewLine

GenerateFileInformation = S
End Function

VBScript 5.6

54

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' GenerateFolderInformation
'
' :
'
' , .
'
'
'
' - Folder.Path
' - Folder.Name
' - Folder.DateCreated
' - Folder.DateLastAccessed
' - Folder.DateLastModified
' - Folder.Size
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GenerateFolderInformation(Folder)
Dim S
S
S
S
S
S
S
S

=
=
=
=
=
=
=

":" & TabStop & Folder.Path


S & NewLine & ":" & TabStop & Folder.Name
S & NewLine & ":" & TabStop & ShowFileAttr(Folder)
S & NewLine & ":" & TabStop & Folder.DateCreated
S & NewLine & ":" & TabStop & Folder.DateLastAccessed
S & NewLine & ":" & TabStop & Folder.DateLastModified
S & NewLine & ":" & TabStop & Folder.Size & NewLine

GenerateFolderInformation = S
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' GenerateAllFolderInformation
'
' :
'
' ,
'
'
'
'
' - Folder.Path
' - Folder.SubFolders
' - Folders.Count
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GenerateAllFolderInformation(Folder)
Dim
Dim
Dim
Dim
Dim

S
SubFolders
SubFolder
Files
File

S = ":" & TabStop & Folder.Path & NewLine & NewLine


Set Files = Folder.Files
If 1 = Files.Count Then
S = S & " 1 " & NewLine
Else
S = S & " " & Files.Count & " " & NewLine
End If
If Files.Count <> 0 Then
For Each File In Files
S = S & GenerateFileInformation(File)
Next
End If
Set SubFolders = Folder.SubFolders

VBScript 5.6

55

If 1 = SubFolders.Count Then
S = S & NewLine & " 1 " & NewLine & NewLine
Else
S = S & NewLine & " " & SubFolders.Count & " " & NewLine & NewLine
End If
If SubFolders.Count <> 0 Then
For Each SubFolder In SubFolders
S = S & GenerateFolderInformation(SubFolder)
Next
S = S & NewLine
For Each SubFolder In SubFolders
S = S & GenerateAllFolderInformation(SubFolder)
Next
End If
GenerateAllFolderInformation = S
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' GenerateTestInformation
'
' :
'
' , C:\Test
'
'
'
'
' - FileSystemObject.DriveExists
' - FileSystemObject.FolderExists
' - FileSystemObject.GetFolder
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GenerateTestInformation(FSO)
Dim TestFolder
Dim S
If Not FSO.DriveExists(TestDrive) Then Exit Function
If Not FSO.FolderExists(TestFilePath) Then Exit Function
Set TestFolder = FSO.GetFolder(TestFilePath)
GenerateTestInformation = GenerateAllFolderInformation(TestFolder)
End Function

VBScript 5.6
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' DeleteTestDirectory
'
' :
'
' .
'
'
'
' - FileSystemObject.GetFolder
' - FileSystemObject.DeleteFile
' - FileSystemObject.DeleteFolder
' - Folder.Delete
' - File.Delete
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub DeleteTestDirectory(FSO)
Dim TestFolder
Dim SubFolder
Dim File
' :
FSO.DeleteFile(TestFilePath & "\Beatles\OctopusGarden.txt")
Set File = FSO.GetFile(TestFilePath & "\Beatles\BathroomWindow.txt")
File.Delete
' :
FSO.DeleteFolder(TestFilePath & "\Beatles")
FSO.DeleteFile(TestFilePath & "\ReadMe.txt")
Set TestFolder = FSO.GetFolder(TestFilePath)
TestFolder.Delete
End Sub

56

VBScript 5.6

57

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' CreateLyrics
'
' :
'
' .
'
'
'
' - FileSystemObject.CreateTextFile
' - TextStream.WriteLine
' - TextStream.Write
' - TextStream.WriteBlankLines
' - TextStream.Close
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub CreateLyrics(Folder)
Dim TextStream
Set TextStream = Folder.CreateTextFile("OctopusGarden.txt")
TextStream.Write("Octopus' Garden ")
' ,
TextStream.WriteLine("( )")
TextStream.WriteBlankLines(1)
TextStream.WriteLine("I'd like to be under the sea in an octopus' garden in the shade,")
TextStream.WriteLine("He'd let us in, knows where we've been -- in his octopus' garden in the shade.")
TextStream.WriteBlankLines(2)
TextStream.Close
Set TextStream = Folder.CreateTextFile("BathroomWindow.txt")
TextStream.WriteLine("She Came In Through The Bathroom Window (by Lennon/McCartney)")
TextStream.WriteLine("")
TextStream.WriteLine("She came in through the bathroom window protected by a silver spoon")
TextStream.WriteLine("But now she sucks her thumb and wanders by the banks of her own lagoon")
TextStream.WriteBlankLines(2)
TextStream.Close
End Sub

VBScript 5.6
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' GetLyrics
'
' :
'
' .
'
'
'
' - FileSystemObject.OpenTextFile
' - FileSystemObject.GetFile
' - TextStream.ReadAll
' - TextStream.Close
' - File.OpenAsTextStream
' - TextStream.AtEndOfStream
' - TextStream.ReadLine
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GetLyrics(FSO)
Dim TextStream
Dim S
Dim File
' ,
' . :
Set TextStream = FSO.OpenTextFile(TestFilePath & "\Beatles\OctopusGarden.txt", OpenFileForReading)
S = TextStream.ReadAll & NewLine & NewLine
TextStream.Close
Set File = FSO.GetFile(TestFilePath & "\Beatles\BathroomWindow.txt")
Set TextStream = File.OpenAsTextStream(OpenFileForReading)
Do
While Not TextStream.AtEndOfStream
S = S & TextStream.ReadLine & NewLine
Loop
TextStream.Close
GetLyrics = S
End Function

58

VBScript 5.6

59

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' BuildTestDirectory
'
' :
'
' FileSystemObject.
'
' :
'
' C:\Test
' C:\Test\ReadMe.txt
' C:\Test\Beatles
' C:\Test\Beatles\OctopusGarden.txt
' C:\Test\Beatles\BathroomWindow.txt
'
'
'
'
' - FileSystemObject.DriveExists
' - FileSystemObject.FolderExists
' - FileSystemObject.CreateFolder
' - FileSystemObject.CreateTextFile
' - Folders.Add
' - Folder.CreateTextFile
' - TextStream.WriteLine
' - TextStream.Close
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function BuildTestDirectory(FSO)
Dim
Dim
Dim
Dim

TestFolder
SubFolders
SubFolder
TextStream

' (a), (b)


If Not FSO.DriveExists(TestDrive) Then
BuildTestDirectory = False
Exit Function
End If
If FSO.FolderExists(TestFilePath) Then
BuildTestDirectory = False
Exit Function
End If
Set TestFolder = FSO.CreateFolder(TestFilePath)
Set TextStream = FSO.CreateTextFile(TestFilePath & "\ReadMe.txt")
TextStream.WriteLine(" ")
TextStream.Close
Set SubFolders = TestFolder.SubFolders
Set SubFolder = SubFolders.Add("Beatles")
CreateLyrics SubFolder
BuildTestDirectory = True
End Function

VBScript 5.6

60

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
'
'
' .
'
' , .
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Main
Dim FSO
' .
TabStop = Chr(9)
NewLine = Chr(10)
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not BuildTestDirectory(FSO) Then
MsgBox " . ."
Exit Sub
End If
MsgBox GenerateDriveInformation(FSO) & NewLine & NewLine
MsgBox GenerateTestInformation(FSO) & NewLine & NewLine
MsgBox GetLyrics(FSO) & NewLine & NewLine
DeleteTestDirectory(FSO)
End Sub

VBScript 5.6

61

5.
5.1. VBScript
5.1.1. VBScript
, VBScript.
.

Array
Dim, Private, Public, ReDim
IsArray
Erase
LBound, UBound

Set

' Rem

Empty
Nothing
Null
True, False

Do...Loop
For...Next
For Each...Next
If...Then...Else
Select Case
While...Wend
With

Abs
Asc, AscB, AscW
Chr, ChrB, ChrW
CBool, CByte
CCur, CDate
CDbl, CInt
CLng, CSng, CStr
DateSerial, DateValue
Hex, Oct
Fix, Int
Sgn
TimeSerial, TimeValue

Date, Time
DateAdd, DateDiff, DatePart
DateSerial, DateValue
Day, Month, MonthName
Weekday, WeekdayName, Year
Hour, Minute, Second
Now
TimeSerial, TimeValue

Class
Const
Dim, Private, Public, ReDim
Function, Sub
Property Get, Property Let, Property Set

On Error
Err

VBScript 5.6

62

Expressions Eval
Execute
RegExp
Replace
Test

FormatCurrency
FormatDateTime
FormatNumber
FormatPercent

InputBox
LoadPicture
MsgBox

Empty
False
Nothing
Null
True

Atn, Cos, Sin, Tan


Exp, Log, Sqr
Randomize, Rnd

Eval Function
Execute Statement
RGB Function

CreateObject
Err Object
GetObject
RegExp

(+), (-)
(^)
(Mod)
(*), (/)
(\)
(-)
(&)
(=), (<>)
(<), (<=)
(>)
(>=)
Is
And, Or, Xor
Eqv, Imp

Option Explicit

Call
Function, Sub
Property Get, Property Let, Property Set

Abs
Int, Fix, Round
Sgn

ScriptEngine
ScriptEngineBuildVersion
ScriptEngineMajorVersion
ScriptEngineMinorVersion

VBScript 5.6

63

Asc, AscB, AscW


Chr, ChrB, ChrW
Filter, InStr, InStrB
InStrRev
Join
Len, LenB
LCase, UCase
Left, LeftB
Mid, MidB
Right, RightB
Replace
Space
Split
StrComp
String
StrReverse
LTrim, RTrim, Trim

IsArray
IsDate
IsEmpty
IsNull
IsNumeric
IsObject
TypeName
VarType

VBScript 5.6

64

5.1.2. VBA, VBScript


Visual Basic for Applications (VBA),
VBScript. , VBA.

Option Base
0

Add, Count, Item, Remove


! (,
MyCollection!Foo)

#Const
#If...Then...#Else

DoEvents
GoSub...Return, GoTo
On Error GoTo
On...GoSub, On...GoTo
Line numbers, Line labels

CVar, CVDate
Str, Val

, Variant
Type...End Type

Date, Time

DDE

LinkExecute, LinkPoke, LinkRequest, LinkSend

Debug.Print
End, Stop

Declare (for declaring DLLs)


Optional
ParamArray
Static

Erl
Error
Resume, Resume Next

/ Basic

Clipboard
Collection

Like

Deftype
Option Base
Option Compare
Option Private Module

Select Case

, Is, -
. , ,
To.


LSet, RSet
Mid
StrConv

! .

VBScript 5.6

65

5.1.3. VBScript, VBA


VBScript, Visual Basic for
Applications (VBA). , VBA.

Class

Eval
Execute

RegExp

ScriptEngine
ScriptEngineBuildVersion
ScriptEngineMajorVersion
ScriptEngineMinorVersion

5.1.4. Scripting Run-Time Library Reference Features

Drives
Files
Folders

Dictionary
Add, Exists, Items, Keys, Remove, RemoveAll
Count, Item, Key
Drive, File, Folder,
Copy, Delete, Move, OpenAsTextStream
Attributes, Count, DateCreated, DateLastAccessed, DateLastModified, Drive, ParentFolder, Name, Path,
ShortName, ShortPath, Size
AvailableSpace, DriveLetter, DriveType, FileSystem, FreeSpace, IsReady, RootFolder, SerialNumber,
ShareName, TotalSize, VolumeName
FileSystemObject
BuildPath, CopyFile, CopyFolder, CreateFolder, CreateTextFile, DeleteFile, DeleteFolder, DriveExists,
FileExists, FolderExists, GetAbsolutePathName, GetBaseName, GetDrive, GetDriveName,
GetExtensionName, GetFile, GetFileName, GetFolder, GetParentFolderName, GetSpecialFolder,
GetTempName, MoveFile, MoveFolder, OpenTextFile
Drives
TextStream
Close, Read, ReadAll, ReadLine, Skip, SkipLine, Write, WriteBlankLines, WriteLine
AtEndOfLine, AtEndOfStream, Column, Line

VBScript 5.6

5.2.
Abs
Addition (+)
And
Array
Asc
Assignment (=)
Atn
Call
CBool
CByte
CCur
CDate
CDbl
Chr
CInt
Class Object
Class Statement
Clear Method
CLng
Color Constants
Comparison Constants
Concatenation Operator (&)
Const Statement
Cos
CreateObject
CSng
CStr
Date and Time Constants
Date Format Constants
Date
DateAdd
DateDiff
DatePart
DateSerial
DateValue
Day
Description Property
Dictionary Object
Dim Statement
Division (/)
Do...Loop Statement
Empty
Eqv
Erase Statement
Err Object
Eval
Execute Method
Execute Statement
Exit Statement
Exp
Exponentiation Operator (^)

66

VBScript 5.6

False
FileSystemObject Object
Filter
FirstIndex Property
Fix
For...Next Statement
For Each...Next Statement
FormatCurrency
FormatDateTime
FormatNumber
FormatPercent
Function Statement
GetObject
GetRef
Global
Hex
HelpContext Property
HelpFile Property
Hour
If...Then...Else Statement
IgnoreCase Property
Imp Operator
Initialize Event
InputBox
InStr
InStrRev
Int
Integer Division Operator (\)
Is Operator
IsArray
IsDate
IsEmpty
IsNull
IsNumeric
IsObject
Join
LBound
LCase
Left
Len
Length Property
LoadPicture
Log
LTrim
Match Object
Matches Collection
Mid
Minute
Miscellaneous Constants
Mod Operator
Month
MonthName
MsgBox Constants
MsgBox
Muliplication Operator (*)

67

VBScript 5.6

Negation Operator (-)


Not Operator
Now
Nothing
Null
Number Property
Oct
On Error Statement
Operator Precedence
Option Explicit Statement
Or Operator
Pattern Property
Private Statement
PropertyGet Statement
PropertyLet Statement
PropertySet Statement
Public Statement
Raise Method
Randomize Statement
ReDim Statement
RegExp Object
Rem Statement
Replace
Replace Method
RGB
Right
Rnd
Round
RTrim
ScriptEngine
ScriptEngineBuildVersion
ScriptEngineMajorVersion
ScriptEngineMinorVersion
Second
Select Case Statement
Set Statement
Sgn
Sin
Source Property
Space
Split
Sqr
StrComp
String Constants
String
StrReverse
Sub Statement
Subtraction Operator (-)

68

VBScript 5.6

69

Tan
Terminate Event
Test Method
Time
Timer
TimeSerial
TimeValue
Trim
Tristate Constants
True
TypeName
UBound
UCase
Value Property
VarType Constants
VarType
VBScript Constants
Weekday
WeekdayName
While...Wend Statement
With Statement
Xor Operator
Year

5.3.
5.3.1. VBScript
VBScript .

,
.
.
VBScript
. .
VBScript.
. ,
.
. ,
.
. ,
.
. , .
MsgBox. , MsgBox.
. ,
.
. , .
. ,
Variant.

VBScript 5.6

70

5.3.2.
VBScript,
. ,
:

vbBlack

&h00

Black ()

vbRed

&hFF

Red ()

vbGreen

&hFF00

Green ()

vbYellow

&hFFFF

Yellow ()

vbBlue
vbMagenta

&hFF0000 Blue ()
&hFF00FF Magenta ()

vbCyan

&hFFFF00 Cyan ()

vbWhite

&hFFFFFF White ()

5.3.3.
VBScript,
. ,
:

vbBinaryCompare

vbTextCompare

5.3.4.
VBScript,
. ,
:

vbSunday

vbMonday

vbTuesday

vbWednesday

vbThursday

vbFriday

vbSaturday

vbUseSystem

vbUseSystemDayOfWeek

vbFirstJan1

, 1 ( )

vbFirstFourDays

, 4

vbFirstFullWeek

VBScript 5.6

71

5.3.5.
VBScript,
. ,
:

vbGeneralDate

/ . .
, .
, .
.

vbLongDate

, ,
.

vbShortDate

, ,
.

vbLongTime

, ,
.

vbShortTime

, ,
.

5.3.6.
VBScript,
. ,
:

vbObjectError

-2147221504
,
Err.Raise Number = vbObjectError + 1000

VBScript 5.6

72

5.3.7. MsgBox
MsgBox
, .
MsgBox. VBScript,
.
, :

vbOKOnly

OK

vbOKCancel

OK Cancel ()

vbAbortRetryIgnore

Abort (), Retry () Ignore ()

vbYesNoCancel

Yes (), No () Cancel ()

vbYesNo

Yes () No ()

vbRetryCancel

Retry () Cancel ()

vbCritical

16

vbQuestion

32

vbExclamation

48

vbInformation

64

vbDefaultButton1

vbDefaultButton2

256

vbDefaultButton3

512

vbDefaultButton4

768

vbApplicationModal

. ,
, .

vbSystemModal

4096

. Win16 ,
. Win32
,
, .

MsgBox ,
.
, . VBScript
.

vbOK

vbCancel

Cancel ()

vbAbort

Abort ()

vbRetry

Retry ()

vbIgnore

Ignore ()

vbYes

Yes ()

vbNo

VBScript 5.6

73

5.3.8.
VBScript,
. ,
:

vbCr

Chr(13)

vbCrLf

Chr(13) & Chr(10)

vbFormFeed

Chr(12)

, Microsoft Windows

vbLf

Chr(10)

vbNewLine

Chr(13) & Chr(10) Chr(10)

, ,
,
, 0

vbNullChar

Chr(0)

vbNullString

vbTab

Chr(9)

vbVerticalTab

Chr(11)

, Microsoft Windows

(),

5.3.9.
VBScript,
. ,
:

vbUseDefault

-2

vbTrue

-1

True ()

vbFalse

False ()

VBScript 5.6

74

5.3.10.

, . VBScript
.

vbEmpty

( )

vbNull

vbInteger

Integer

vbLong

Long

vbSingle

Single

vbDouble

Double

vbCurrency

Currency

vbDate

Date

vbString

String

vbObject

Object

vbError

10

Error

vbBoolean

11

Boolean

vbVariant

12

Variant ( )

vbDataObject

13

Data access object

vbDecimal

14

Decimal

vbByte

17

Byte

vbArray

8192

Array

VBScript 5.6

75

5.4.
5.4.1. Initialize

Private Sub _Initialize()

End Sub
Initialize :

. , Class.
. , .

5.4.2. Terminate

Private Sub _Terminate()

End Sub
Terminate :

. , Class.
. , .

VBScript 5.6

5.5.
Abs Function
Array Function
Asc Function
Atn Function
CBool Function
CByte Function
CCur Function
CDate Function
CDbl Function
Chr Function
CInt Function
CLng Function
Cos Function
CreateObject Function
CSng Function
CStr Function
Date Function
DateAddFunction
DateDiff Function
DatePart Function
DateSerial Function
DateValue Function
Day Function
Eval Function
Exp Function
Filter Function
Fix Function
FormatCurrency Function
FormatDateTime Function
FormatNumber Function
FormatPercent Function
GetObject Function
GetRef Function
Hex Function
Hour Function
InputBox Function
InStr Function
InStrRev Function
Int Function
IsArray Function
IsDate Function
IsEmpty Function
IsNull Function
IsNumeric Function
IsObject Function
Join Function
LBound Function
LCase Function
Left Function
Len Function
LoadPicture Function
Log Function
LTrim Function

76

VBScript 5.6

Mid Function
Minute Function
Month Function
MonthName Function
MsgBox Function
Now Function
Oct Function
Replace Function
RGB Function
Right Function
Rnd Function
Round Function
RTrim Function
ScriptEngine Function
ScriptEngineBuildVersion Function
ScriptEngineMajorVersion Function
ScriptEngineMinorVersion Function
Second Function
Sgn Function
Sin Function
Space Function
Split Function
Sqr Function
StrComp Function
String Function
StrReverse Function
Tan Function
Time Function
Timer Function
TimeSerial Function
TimeValue Function
Trim Function
TypeName Function
UBound Function
UCase Function
VarType Function
Weekday Function
WeekdayName Function
Year Function

5.6.
Clear Method
Execute Method
Raise Method
Replace Method
Test Method

77

VBScript 5.6

78

5.7.
5.7.1. Class
(. 5.4. ).
Class.
Class. VBScript Class
, Class.
Class,
, :

Dim X
Set X = New
VBScript ,
:

Dim X as New

Dim X
X = New

Set X = New Scripting.FileSystemObject

5.7.2. Dictionary
, -.
Dictionary PERL. ,
, ,
.
, ,
.
, Dictionary:

Dim d
Set d
d.Add
d.Add
d.Add

'
= CreateObject("Scripting.Dictionary")
"a", ""
'
"b", ""
"m", ""

MsgBox d.Item("m")

VBScript 5.6

79

5.7.2.1. Dictionary
CompareMode

Dictionary. :

.CompareMode[ = ]
:

. Dictionary.

. , ,
, StrComp.

vbBinaryCompare

vbTextCompare

2 , Locale IDs
(LCID). Dictionary, ,
.
CompareMode ,
StrComp.
CompareMode:

Dim d
'
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare
d.Add "a", ""
'
d.Add "b", ""
d.Add "m", ""
d.Add "M", ""
' Add ,
'
d.CompareMode = vbTextCompare
d.CompareMode = vbBinaryCompare, ,
m .

VBScript 5.6

80

Count

Dictionary. :

.Count
Count:

Function ShowKeys
Dim a, d, i, s
'
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", ""
'
d.Add "b", ""
d.Add "m", ""
a = d.Keys
'
For i = 0 To d.Count -1 '
s = s & a(i) & ", "
'
Next
ShowKeys = s
End Function

Item
Dictionary.
. :

.Item() [= ]
:

. Dictionary.

. , ,
.

. Dictionary.
. , , .

,
, ,
. ,
, .
Item:

Dim d, s
'
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", ""
'
d.Add "b", ""
d.Add "m", ""
s = d.Item("x")
d.Item("z") = ""

'
'

VBScript 5.6

81

Key
Dictionary. :

.Key() =
:

. Dictionary.

. , .

. , .

, .
Key:

Function DicDemo
Dim d
'
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", ""
'
d.Add "b", ""
d.Add "m", ""
d.Key("m") = "c"
' "m" "c"
DicDemo = d.Item("c")
' ,
End Function

5.7.2.2. Dictionary
Add
Dictionary /. :

.Add ,
:

. Dictionary.

. , .

. , .

, .
Add:

Dim d
Set d
d.Add
d.Add
d.Add

'
= CreateObject("Scripting.Dictionary")
"a", ""
'
"b", ""
"m", ""

VBScript 5.6

82

Exists
TRUE, Dictionary,
FALSE. :

.Exists()
:

. Dictionary.

. , Dictionary.

Exists:

Function KeyExistsDemo
Dim d, msg
'
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", ""
'
d.Add "b", ""
d.Add "m", ""
If d.Exists("m") Then
msg = " m "
Else
msg = " m "
End If
KeyExistsDemo = msg
End Function

Items
, Dictionary. :

.Items
:

. Dictionary.

Items:
Function DicDemo
Dim a, d, i, s
'
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", ""
'
d.Add "b", ""
d.Add "m", ""
a = d.Items
'
For i = 0 To d.Count - 1 '
s = s & a(i) & vbCrLf '
Next
DicDemo = s
End Function

VBScript 5.6

83

Keys
, Dictionary. :

.Keys
:

. Dictionary.

Keys:

Function DicDemo
Dim a, d, i, s
'
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", ""
'
d.Add "b", ""
d.Add "m", ""
a = d.Keys
'
For i = 0 To d.Count - 1 '
s = s & a(i) & vbCrLf '
Next
DicDemo = s
End Function

Remove
Dictionary -. :

.Remove()
:

. Dictionary.

. , , ( )
Dictionary.

, .
Remove:

Dim a, d
'
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", ""
'
d.Add "b", ""
d.Add "m", ""
...
d.Remove("b")
' /

VBScript 5.6

84

RemoveAll
Dictionary . :

.RemoveAll
:

. Dictionary.

RemoveAll:

Dim a, d
'
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", ""
'
d.Add "b", ""
d.Add "m", ""
...
d.RemoveAll
'

5.7.3. Err
,
. Raise Clear.
Err .
. .
Err Number. Err.Number
SCODE.
, Err
, ,
. .
Raise.
Err ()
On Error Resume Next. Clear
.
Err:

On Error Resume Next


Err.Raise 6 '
MsgBox ("Error # " & CStr(Err.Number) & " " & Err.Description)
Err.Clear
'

VBScript 5.6

85

5.7.3.1. Err
Description
. :

.Description [= ]
:

. Err.

. , .

Description:

On Error Resume Next


Err.Description = " "
Err.Raise 6 '
MsgBox ("Error # " & CStr(Err.Number) & " " & Err.Description)
Err.Clear
'

HelpContext
(ID)
HLP. :

.HelpContext [= ]
:

. Err.

.
. , 0.

Helpfile Err,
HelpContext ,
. (Helpfile HelpContext)
, Number.
VBScript, .
Number VBScript,
VBScript.
HLP.
(, CHM) VBScript .
(. )
HelpContext (,
Err.HelpContext = 2). (,
),
0.

VBScript 5.6

86

HelpContext:

const IDH_Test = 0
const IDH_2 = 1
const IDLast = 2
On Error Resume Next
Dim Msg
Err.Clear
Err.Raise 6
'
Err.Helpfile = "HELP-TEST.HLP"
Err.HelpContext = IDH_Test
If Err.Number <> 0 Then
Msg = " F1 " & Err.Helpfile & _
" , HelpContext: " & Err.HelpContext
MsgBox Msg, , ": " & Err.Description, Err.Helpfile,
Err.HelpContext
End If

HelpFile

HLP. :

.HelpFile [= ]
:

. Err.

. .

Helpfile,
F1
. HelpContext
, .
Helpfile , VBScript.
Helpfile:
const IDH_Test = 0
const IDH_2 = 1
const IDLast = 2
On Error Resume Next
Dim Msg
Err.Clear
Err.Raise 6
'
Err.Helpfile = "HELP-TEST.HLP"
Err.HelpContext = IDH_Test
If Err.Number <> 0 Then
Msg = " F1 " & Err.Helpfile & _
" , HelpContext: " & Err.HelpContext
MsgBox Msg, , ": " & Err.Description, Err.Helpfile, Err.HelpContext
End If

VBScript 5.6

87

Number
. Number
Err . :

.Number [= ]
:

. Err.

VBScript SCODE.

,
Err.Number vbObjectError
.
Number:
On Error Resume Next
Err.Raise vbObjectError + 1, "SomeObject" ' #1
MsgBox (" # " & CStr(Err.Number) & " " & Err.Description)
Err.Clear
'

Source
,
. :

.Source [= ]
:

. Err.

, , .

Source ,
(ID) , .
Source ,
, .
, Microsoft Excel,
, Microsoft Excel Err.Number,
Source Excel.Application. ,
, Microsoft Excel, Excel
Err.Number.
Source ,
.
. ,
Err
.

VBScript 5.6

88

, Source
.
Source:
On Error Resume Next
Err.Raise vbObjectError + 1, "SomeObject" ' #1
MsgBox (" # " & CStr(Err.Number) & " " & Err.Description & Err.Source)
Err.Clear
'
Source SomeObject.
Source Microsoft VBScript.
On Error Resume Next
Err.Raise 6
'
MsgBox (" # " & CStr(Err.Number) & " " & Err.Description & "/" & Err.Source)
Err.Clear
'

5.7.3.2. Err
Clear
Err. :

.Clear
:

. Err.

Clear Err .
, On Error
Resume Next. VBScript Clear ,
:
On Error Resume Next
Exit Sub
Exit Function
Clear:

On Error Resume
On Error Resume
Err.Raise 6
MsgBox ("
Err.Clear
'

Next
Next
'
# " & CStr(Err.Number) & " " & Err.Description)

VBScript 5.6

89

Raise
. :

.Raise(, , , , )
:

. Err.

Long, . VBScript (
VBScript, )
065535.

, ,
. ,
project.class. ,
VBScript.

, . , ,
.
VBScript, , VBScript .

, . ,
VBScript VBScript.

, ,
. , ,
VBScript ,
.

, , .
Raise , Err ,
Raise, Err.
,

vbObjectError. , 1050,
vbObjectError + 1050.
Raise:

On Error Resume
On Error Resume
Err.Raise 6
MsgBox ("
Err.Clear
'

Next
Next
'
# " & CStr(Err.Number) & " " & Err.Description)

VBScript 5.6

90

5.7.4. FileSystemObject
.
, FileSystemObject
TextStream, :

Dim fso, MyFile


Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("testfile.txt", True)
MyFile.WriteLine(" ")
MyFile.Close
CreateObject FileSystemObject (fso).
CreateTextFile TextStream, WriteLine
. Close .

5.7.4.1. FileSystemObject
Drives
Drives (),
Drive, . :

.Drives
:

. FileSystemObject.

, ,
Drives.
Drives, For Each...Next
:

Function ShowDriveList
Dim fso, d, dc, s, n
Set fso = CreateObject("Scripting.FileSystemObject")
Set dc = fso.Drives
For Each d in dc
n = ""
s = s & d.DriveLetter & " - "
If d.DriveType = 3 Then
n = d.ShareName
ElseIf d.IsReady Then
n = d.VolumeName
End If
s = s & n & vbCrLf
Next
ShowDriveList = s
End Function

VBScript 5.6

91

5.7.4.2. FileSystemObject
BuildPath
. :

.BuildPath(, )
:

. FileSystemObject.

. , .
( ).

. , ,
.

BuildPath
.
BuildPath:

Function GetBuildPath(path)
Dim fso, newpath
Set fso = CreateObject("Scripting.FileSystemObject")
newpath = fso.BuildPath(path, "")
GetBuildPath = newpath
End Function

CopyFile
. :

.CopyFile , [, ]
:

. FileSystemObject.

. , ,
. (),
.

. , , ,
. ( ,
*).

. , , ,
, . True, ,
False, . True. ,
CopyFile , True, ,
.

VBScript 5.6

92


. :

FileSystemObject.CopyFile "c:\mydocuments\letters\*.doc", "c:\tempfolder\"


:

FileSystemObject.CopyFile "c:\mydocuments\*\R1???97.xls", "c:\tempfolder"



(\), , ,
, . ,
, . :
, .
, .
, False,
. ( ),
, .
, .
\ ,
.
,
, .
.
, ,
. CopyFile
. .

CopyFolder
. :

.CopyFolder , [, ]
:

. FileSystemObject.

. , ,
. (),
.

. , , ,
. ( ,
*).

. , ,
, , . True,
, False, . True.

VBScript 5.6

93


. :

FileSystemObject.CopyFolder "c:\mydocuments\letters\*", "c:\tempfolder\"


:

FileSystemObject.CopyFolder "c:\mydocuments\*\*", "c:\tempfolder\"



(\), , ,
, . ,
,
-.
:
,
. ,
.
, .
,
. ,
, False. True,
.

, ,
, False.
, ,
. CopyFolder
. .

CreateTextFile
TextStream,
. :

.CreateTextFile([, [, ]])
:

. FileSystemObject.

. , .

. , , ,
, . True, ,
False, . ( )
.

. , ,
Unicode ASCII. True, Unicode,
ASCII. , ASCII-.

VBScript 5.6

94

, CreateTextFile
:

Sub CreateAfile
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine(" .")
MyFile.Close
End Sub
False , ,
, .

DeleteFile
. :

.DeleteFile [, ]
:

. FileSystemObject.

. , .

. , , ,
, . True,
, False ( ), .

, . DeleteFile
.
. DeleteFile:

Sub DeleteAFile(filespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(filespec)
End Sub

VBScript 5.6

95

DeleteFolder
. :

.DeleteFolder [, ]
:

. FileSystemObject.

. , .

. , ,
, , . True,
, False ( ), .

DeleteFolder ,
. , ,
.
, . DeleteFolder
.
.
DeleteFolder:

Sub DeleteAFolder(filespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder(filespec)
End Sub

DriveExists
TRUE, . ,
FALSE. :

.DriveExists()
:

. FileSystemObject.

. ( , , ,
, , :\).

DriveExists TRUE,
. IsReady Drive
.

VBScript 5.6

96

DriveExists:

Function ReportDriveStatus(drv)
Dim fso, msg
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.DriveExists(drv) Then
msg = (" " & UCase(drv) & " .")
Else
msg = (" " & UCase(drv) & " .")
End If
ReportDriveStatus = msg
End Function

FileExists
TRUE, . ,
FALSE. :

.FileExists()
:

. FileSystemObject.

. , .
( ), ,
.

FileExists:

Function ReportFileStatus(filespec)
Dim fso, msg
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(filespec)) Then
msg = filespec & " ."
Else
msg = filespec & " ."
End If
ReportFileStatus = msg
End Function

VBScript 5.6

97

FolderExists
TRUE, . ,
FALSE. :

.FolderExists()
:

. FileSystemObject.

. , .
( ), ,
.

FolderExists:

Function ReportFolderStatus(fldr)
Dim fso, msg
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FolderExists(fldr)) Then
msg = fldr & " ."
Else
msg = fldr & " ."
End If
ReportFolderStatus = msg
End Function

GetAbsolutePathName
. :

.GetAbsolutePathName()
:

. FileSystemObject.

. , .

, ,
.

VBScript 5.6

98

, c:\mydocuments\reports,
, :

"c:"

"c:\mydocuments\reports"

"c:.."

"c:\mydocuments"

"c:\\\"

"c:\"

"c:*.*\may97"

"c:\mydocuments\reports\*.*\may97"

"region1"

"c:\mydocuments\reports\region1"

"c:\..\..\mydocuments"

"c:\mydocuments"

GetAbsolutePathName:

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
MsgBox fso.GetAbsolutePathName("HELP-TEST.HLP")
HELP-TEST.HLP,
, .
. (
c:\mydocuments\reports):
c:\mydocuments\reports\HELP-TEST.HLP
,
:

MsgBox fso.GetAbsolutePathName("")
:
c:\mydocuments\reports

GetBaseName
,
. :

.GetBaseName()
:

. FileSystemObject.

. .

GetBaseName
.
, GetBaseName .
.

VBScript 5.6

99

GetBaseName:

Function GetTheBase(filespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
GetTheBase = fso.GetBaseName(filespec)
End Function

GetDrive
Drive. .
:

.GetDrive
:

. FileSystemObject.

. (), (:),
(:\) ,
(\\computer2\share1).

, , .
, , , -
. GetDrive
.
:
DriveSpec = GetDriveName(GetAbsolutePathName(Path))
GetDrive:

Function ShowFreeSpace(drvPath)
Dim fso, d, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(fso.GetDriveName(drvPath))
s = " " & UCase(drvPath) & " - "
s = s & d.VolumeName & vbCrLf
s = s & ": " & FormatNumber(d.FreeSpace/1024, 0)
s = s & " "
ShowFreeSpace = s
End Function

VBScript 5.6

100

GetDriveName
, . :

.GetDriveName()
:

. FileSystemObject.

. , .

GetDriveName , .
GetDriveName:

Function GetAName(DriveSpec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
GetAName = fso.GetDriveName(Drivespec)
End Function

GetExtensionName
, .
:

.GetExtensionName()
:

. FileSystemObject.

. , .

.
GetExtensionName , .
GetExtensionName:

Function GetAnExtension(DriveSpec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
GetAnExtension = fso.GetExtensionName(Drivespec)
End Function

VBScript 5.6

101

GetFile
File, , . :

.GetFile()
:

. FileSystemObject.

. ( ),
.

, .
GetFile:

Function ShowFileAccessInfo(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
s = f.Path & vbCrLf
s = s & " : " & f.DateCreated & vbCrLf
s = s & " : " & f.DateLastAccessed & vbCrLf
s = s & " : " & f.DateLastModified
ShowFileAccessInfo = s
End Function

GetFileName
. :

.GetFileName()
:

. FileSystemObject.

. ( ).

GetFileName ,
.

VBScript 5.6

102

GetFileName:

Function GetAName(DriveSpec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
GetAName = fso.GetFileName(DriveSpec)
End Function
, GetFileName ,
. .

GetFolder
Folder, . :

.GetFolder()
:

. FileSystemObject.

. ( ).

, .
GetFolder:

Sub AddNewFolder(path, folderName)


Dim fso, f, fc, nf
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(path)
Set fc = f.SubFolders
If folderName <> "" Then
' ,
Set nf = fc.Add(folderName)
'
Else
' ,
Set nf = fc.Add(" ") '
End If
End Sub

VBScript 5.6

103

GetParentFolderName
, ,
. :

.GetParentFolderName()
:

. FileSystemObject.

. ,
.

GetParentFolderName ,
() .
GetParentFolderName:

Function GetTheParent(DriveSpec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
GetTheParent = fso.GetParentFolderName(Drivespec)
End Function
, GetParentFolderName ,
.
().

GetSpecialFolder
. :

.GetSpecialFolder()
:

. FileSystemObject.

. . ,
.

WindowsFolder

Windows , Windows

SystemFolder

System ,

TemporaryFolder

Temp .
TMP.

VBScript 5.6

104

GetSpecialFolder:

Function GetSysFolder
Dim fso, sfolder, p
Set fso = CreateObject("Scripting.FileSystemObject")
Set sfolder = fso.GetSpecialFolder(0)
p = sfolder.Path
GetSysFolder = p
End Function

GetTempName

, .
:

.GetTempName
:

. FileSystemObject.

GetTempName , ,
CreateTextFile.
GetTempName:

Dim fso, tempfile


Set fso = CreateObject("Scripting.FileSystemObject")
Function CreateTempFile
Dim tfolder, tname, tfile
Const TemporaryFolder = 2
Set tfolder = fso.GetSpecialFolder(TemporaryFolder)
tname = fso.GetTempName
Set tfile = tfolder.CreateTextFile(tname)
Set CreateTempFile = tfile
End Function
Set tempfile = CreateTempFile
tempfile.WriteLine "Hello World"
tempfile.Close

VBScript 5.6

105

MoveFile
.
:

.MoveFile ,
:

. FileSystemObject.

. , .
,
.

. , .
.


(\), ,
.
, .
:
, . .
, .
, .
, ,
, . MoveFile
, ,
, .
MoveFile:

Sub MoveAFile(Drivespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.MoveFile Drivespec, "c:\windows\desktop\"
End Sub

,
.

VBScript 5.6

106

MoveFolder
.
:

.MoveFolder ,
:

. FileSystemObject.

. , .
,
.

. , .
.


(\), ,
.
, .
:
, . .
, .
, .
, ,
, . MoveFolder
, ,
, .
MoveFolder:

Sub MoveAFolder(Drivespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.MoveFolder Drivespec, "c:\windows\desktop\"
End Sub

,
.

VBScript 5.6

107

OpenTextFile
TextStream,
, . :

.OpenTextFile([, [, [, ]]])
:

. FileSystemObject.

. , .

. -. :
ForReading, ForWriting ForAppending.

. , ,
, , . TRUE,
, FALSE ( ), .

. ,
. , ASCII.

ForReading

. .

ForWriting

. .

ForAppending

TristateUseDefault

-2

, .

TristateTrue

-1

Unicode.

TristateFalse

ASCII.

OpenTextFile:

Sub OpenTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("testfile.txt", ForWriting, True)
f.Write ", !"
f.Close
End Sub

VBScript 5.6

108

5.7.5. Drive
.
Drive :

Function ShowFreeSpace(drvPath)
Dim fso, d, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(fso.GetDriveName(drvPath))
s = " " & UCase(drvPath) & " - "
s = s & d.VolumeName & vbCrLf
s = s & " : " & FormatNumber(d.FreeSpace/1024, 0)
s = s & " "
ShowFreeSpace = s
End Function
MsgBox ShowFreeSpace("C:\")

5.7.5.1. Drive
AvailableSpace

. :

.AvailableSpace
:

. Drive.

, AvailableSpace ,
FreeSpace.
, .
AvailableSpace:

Function ShowAvailableSpace(drvPath)
Dim fso, d, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(fso.GetDriveName(drvPath))
s = " " & UCase(drvPath) & " - "
s = s & d.VolumeName & vbCrLf
s = s & " : " & FormatNumber(d.AvailableSpace/1024, 0)
s = s & " "
ShowAvailableSpace = s
End Function

VBScript 5.6

109

DriveLetter
. .
:

.DriveLetter
:

. Drive.

DriveLetter (),
, , , .
DriveLetter:

Function ShowDriveLetter(drvPath)
Dim fso, d, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(fso.GetDriveName(drvPath))
s = " " & d.DriveLetter & ": - "
s = s & d.VolumeName & vbCrLf
s = s & " : " & FormatNumber(d.FreeSpace/1024, 0)
s = s & " "
ShowDriveLetter = s
End Function

DriveType
, . :

.DriveType
:

. Drive.

VBScript 5.6

110

DriveType:

Function ShowDriveType(drvpath)
Dim fso, d, t
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(drvpath)
Select Case d.DriveType
Case 0: t = ""
Case 1: t = ""
Case 2: t = ""
Case 3: t = ""
Case 4: t = "CD-ROM"
Case 5: t = "RAM-"
End Select
ShowDriveType = " " & d.DriveLetter & ": - " & t
End Function

FileSystem
, . :

.FileSystem
:

. Drive.

, : FAT, NTFS CDFS.


FileSystem:

Function ShowFileSystemType(drvspec)
Dim fso,d
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(drvspec)
ShowFileSystemType = " : " & d.FileSystem
End Function

FreeSpace

. :

.FreeSpace
:

. Drive.

VBScript 5.6

111

, FreeSpace ,
AvailableSpace.
, .
FreeSpace:

Function ShowFreeSpace(drvPath)
Dim fso, d, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(fso.GetDriveName(drvPath))
s = " " & UCase(drvPath) & " - "
s = s & d.VolumeName & vbCrLf
s = s & " : " & FormatNumber(d.FreeSpace/1024, 0)
s = s & " "
ShowFreeSpace = s
End Function

IsReady
TRUE, , FALSE.
:

.IsReady
:

. Drive.

CD-ROM- IsReady TRUE


.
IsReady:

Function ShowDriveInfo(drvpath)
Dim fso, d, s, t
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(drvpath)
Select Case d.DriveType
Case 0: t = ""
Case 1: t = ""
Case 2: t = ""
Case 3: t = ""
Case 4: t = "CD-ROM"
Case 5: t = "RAM-"
End Select
s = " " & d.DriveLetter & ": - " & t
If d.IsReady Then
s = s & vbCrLf & " ."
Else
s = s & vbCrLf & " ."
End If
ShowDriveInfo = s
End Function

VBScript 5.6

112

Path
, . :

.Path
:

. File, Folder Drive.

. , :,
:\. Path File:

Function ShowFileAccessInfo(filespec)
Dim fso, d, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
s = UCase(f.Path) & vbCrLf
s = s & ": " & f.DateCreated & vbCrLf
s = s & " : " & f.DateLastAccessed & vbCrLf
s = s & " : " & f.DateLastModified
ShowFileAccessInfo = s
End Function

RootFolder
Folder, . :

.RootFolder
:

. Drive.

, , ,
Folder, .
RootFolder:

Function ShowRootFolder(drvspec)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetDrive(drvspec)
ShowRootFolder = f.RootFolder
End Function

VBScript 5.6

113

SerialNumber
,
. :

.SerialNumber
:

. Drive.

SerialNumber , ,
.
SerialNumber:

Function ShowSerialNum(drvpath)
Dim fso, d, s, t
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(fso.GetDriveName(fso.GetAbsolutePathName(drvpath)))
Select Case d.DriveType
Case 0: t = ""
Case 1: t = ""
Case 2: t = ""
Case 3: t = ""
Case 4: t = "CD-ROM"
Case 5: t = "RAM-"
End Select
s = " " & d.DriveLetter & ": - " & t
s = s & vbCrLf & " : " & d.SerialNumber
ShowSerialNum = s
End Function
ShareName
. :

.ShareName
:

. Drive.

, ShareName
(). ShareName:

Function ShowShareName(drvpath)
Dim fso, d
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(fso.GetDriveName(fso.GetAbsolutePathName(drvpath)))
ShowShareName = " " & d.DriveLetter & ": - " & d.ShareName
End Function

VBScript 5.6

114

TotalSize
. :

.TotalSize
:

. Drive.

TotalSize:

Function ShowSpaceInfo(drvpath)
Dim fso, d, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(fso.GetDriveName(fso.GetAbsolutePathName(drvpath)))
s = " " & d.DriveLetter & ":"
s = s & vbCrLf
s = s & " : " & FormatNumber(d.TotalSize/1024, 0) & " "
s = s & vbCrLf
s = s & ": " & FormatNumber(d.AvailableSpace/1024, 0) & " "
ShowSpaceInfo = s
End Function
VolumeName
. :

.VolumeName [= ]
:

. Drive.

. , .

VolumeName:

Function ShowVolumeInfo(drvpath)
Dim fso, d, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(fso.GetDriveName(fso.GetAbsolutePathName(drvpath)))
s = " " & d.DriveLetter & ": - " & d.VolumeName
ShowVolumeInfo = s
End Function

VBScript 5.6

115

5.7.6. File
.
File :

Function ShowFreeSpace(drvPath)
Dim fso, d, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(fso.GetDriveName(drvPath))
s = " " & UCase(drvPath) & " - "
s = s & d.VolumeName & vbCrLf
s = s & " : " & FormatNumber(d.FreeSpace/1024, 0)
s = s & " "
ShowFreeSpace = s
End Function
MsgBox ShowFreeSpace("C:\")

5.7.6.1. File
Attributes
. :
/. . :

.Attributes [= ]
:

. File Folder.

. ,
( ).


,
.

Normal
ReadOnly
Hidden
System
Volume
Directory
Archive
Alias
Compressed

0
1
2
4
8
16
32
64
128

. .
. /.
. /.
. /.
. .
. .
. /.
. .
. .

VBScript 5.6

116

Attributes:

Function ToggleArchiveBit(filespec)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
If f.attributes and 32 Then
f.attributes = f.attributes - 32
ToggleArchiveBit = " ."
Else
f.attributes = f.attributes + 32
ToggleArchiveBit = " ."
End If
End Function

DateCreated
, . :

.DateCreated
:

. File Folder.

DateCreated:

Function ShowFileInfo(filespec)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
ShowFileInfo = ": " & f.DateCreated
End Function

DateLastAccessed
,
. :

.DateLastAccessed
:

. File Folder.

VBScript 5.6

117

DateLastAccessed:

Function ShowFileAccessInfo(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
s = UCase(filespec) & vbCrLf
s = s & ": " & f.DateCreated & vbCrLf
s = s & " : " & f.DateLastAccessed & vbCrLf
s = s & " : " & f.DateLastModified
ShowFileAccessInfo = s
End Function
!
.
, .

DateLastModified
, .
:

.DateLastModified
:

. File Folder.

DateLastModified:

Function ShowFileAccessInfo(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
s = UCase(filespec) & vbCrLf
s = s & ": " & f.DateCreated & vbCrLf
s = s & " : " & f.DateLastAccessed & vbCrLf
s = s & " : " & f.DateLastModified
ShowFileAccessInfo = s
End Function

Drive
, . .
:

.Drive

VBScript 5.6

118

. File Folder.

Drive:

Function ShowFileDriveInfo(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
s = f.Name & " " & UCase(f.Drive) & vbCrLf
ShowFileDriveInfo = s
End Function

Name
. /. :

.Name [= ]
:

. File Folder.

. , .

Name:

Function ShowFileDriveInfo(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
s = f.Name & " " & UCase(f.Drive) & vbCrLf
ShowFileDriveInfo = s
End Function

ParentFolder
Folder .
. :

.ParentFolder
:

. File Folder.

VBScript 5.6

119

ParentFolder:

Function ShowParentFolder(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
s = " " & UCase(f.Name) & " " &
UCase(f.ParentFolder)
ShowParentFolder = s
End Function

Path
, . :

.Path
:

. File, Folder Drive.

Drive .
, :, :\.
Path:

Function ShowPath(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
s = " " & UCase(f.Path)
ShowPath = s
End Function

ShortName
() .
,
8.3 (8 , 3 - ). :

.ShortName
:

. File Folder.

VBScript 5.6

120

ShortName:

Function ShowShortName(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
s = " " & UCase(f.Name) & vbCrLf
s = s & " - " & f.ShortName
ShowShortName = s
End Function

ShortPath
. ,
8.3 (8
, 3 - ). :

.ShortPath
:

. File Folder.

ShortPath:

Function ShowShortPath(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
s = " " & UCase(f.Name) & vbCrLf
s = s & " - " & f.ShortPath
ShowShortPath = s
End Function

Size
.
, . :

.Size
:

. File Folder.

VBScript 5.6

121

Size Folder:

Function ShowFolderSize(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(filespec)
s = UCase(f.Name) & " " & f.size & " ."
ShowFolderSize = s
End Function

/ ,
.

Type
. , .
Text Document (
Windows , , ). :

.Type
:

. File Folder.

Type Folder.

Function ShowFolderType(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(filespec)
s = UCase(f.Name) & " - " & f.Type
ShowFolderType = s
End Function

5.7.6.2. File
Copy
. :

.Copy [, ]

VBScript 5.6

122

. File Folder.

. , .
() .

. , , ,
, . True, ,
False, . True.

, Copy File Folder,


,
FileSystemObject.CopyFile FileSystemObject.CopyFolder,
, .
,
.
Copy.

Dim fso, MyFile


Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("testfile-1.txt", True)
MyFile.WriteLine(" .")
MyFile.Close
Set MyFile = fso.GetFile("testfile-1.txt")
MyFile.Copy ("testfile-2.txt")

Delete
. :

.Delete
:

. File Folder.

. , .
, , ,
, True. .
False.

, . Delete
- . ,
, - .
, Delete File Folder,
,
FileSystemObject. DeleteFile FileSystemObject. DeleteFolder.

VBScript 5.6

123

Delete.

Dim fso, MyFile


Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("testfile-1.txt", True)
MyFile.WriteLine(" .")
MyFile.Close
Set MyFile = fso.GetFile("testfile-1.txt")
MyFile.Delete

Move
. :

.Move
:

. File Folder.

. , .
() .

, Move File Folder,


,
FileSystemObject.MoveFile FileSystemObject.MoveFolder. ,
.
Move.

Dim fso, MyFile


Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("testfile-1.txt", True)
MyFile.WriteLine(" .")
MyFile.Close
Set MyFile = fso.GetFile("testfile-1.txt")
MyFile.Move "c:\"

OpenAsTextStream
TextStream,
, . :

.OpenAsTextStream ([, []])

VBScript 5.6

124

. File.

. /. :
ForReading, ForWriting ForAppending.

. Tristate, .
, ASCII-.

ForReading

. .

ForWriting

. ,
, .

ForAppending

TristateUseDefault

-2

TristateTrue

-1

Unicode.

TristateFalse

ASCII-.

OpenAsTextStream , OpenTextFile
FileSystemObject. OpenAsTextStream
.
OpenAsTextStream.

Function TextStreamTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, f, ts
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "test-1.txt"
' .
Set f = fso.GetFile("test-1.txt")
Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
ts.Write ", !"
ts.Close
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
TextStreamTest = ts.ReadLine
ts.Close
End Function

VBScript 5.6

125

5.7.7. TextStream
.

TextStream, CreateTextFile
FileSystemObject:
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile= fso.CreateTextFile("testfile.txt", True)
MyFile.WriteLine(" .")
MyFile.Close
WriteLine Close TextStream.

5.7.7.1. TextStream
AtEndOfLine
TRUE,
, FALSE. :

.AtEndOfLine
:

. TextStream.

AtEndOfLine , ,
. AtEndOfLine.

Function ReadEntireFile(filespec)
Const ForReading = 1
Dim fso, theFile, retstring
Set fso = CreateObject("Scripting.FileSystemObject")
Set theFile = fso.OpenTextFile(filespec, ForReading, False)
Do While theFile.AtEndOfLine <> True
retstring = theFile.Read(1)
Loop
theFile.Close
ReadEntireFile = retstring
End Function

AtEndOfStream
TRUE, ,
FALSE. :

.AtEndOfStream

VBScript 5.6

126

. TextStream.

AtEndOfStream , ,
.
AtEndOfStream.

Function ReadEntireFileEOS(filespec)
Const ForReading = 1
Dim fso, theFile, retstring
Set fso = CreateObject("Scripting.FileSystemObject")
Set theFile = fso.OpenTextFile(filespec, ForReading, False)
Do While theFile.AtEndOfStream <> True
retstring = theFile.ReadLine
Loop
theFile.Close
ReadEntireFileEOS = retstring
End Function

Column
() . .
:

.Column
:

. TextStream.

,
Column 1.
Column.

Function GetColumn
Const ForReading = 1, ForWriting = 2
Dim fso, f, m
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
f.Close
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
m = f.ReadLine
GetColumn = f.Column
End Function

VBScript 5.6

127

Line
. . :

.Line
:

. TextStream.


Line 1.
Line.

Function GetLine
Const ForReading = 1, ForWriting = 2
Dim fso, f, ra
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write ", !" & vbCrLf & "VB Script - !" & vbCrLf
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
ra = f.ReadAll
GetLine = f.Line
End Function

5.7.7.2. TextStream
Close
TextStream. :

.Close
:

. TextStream.

Close.

Sub CreateAFile
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("testfile.txt", True)
MyFile.WriteLine(" .")
MyFile.Close
End Sub

VBScript 5.6

128

Read
TextStream
. :

.Read()
:

. TextStream.

. , .

Read.

Function ReadTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f, Msg
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("testfile.txt", ForWriting, True)
f.Write ", !"
Set f = fso.OpenTextFile("testfile.txt", ForReading)
ReadTextFileTest = f.Read(6)
End Function

ReadAll
TextStream . :

.ReadAll
:

. TextStream.

ReadAll .
,
. ReadAll.

Function ReadAllTextFile
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f, Msg
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("testfile.txt", ForWriting, True)
f.Write ", !"
Set f = fso.OpenTextFile("testfile.txt", ForReading)
ReadAllTextFile = f.ReadAll
End Function

VBScript 5.6

129

ReadLine
( ) TextStream
. :

.ReadLine
:

. TextStream.

ReadLine.

Function ReadLineTextFile
Const ForReading = 1, ForWriting = 2
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.OpenTextFile("testfile.txt", ForWriting, True)
MyFile.WriteLine ", !"
MyFile.WriteLine " "
MyFile.Close
Set MyFile = fso.OpenTextFile("testfile.txt", ForReading)
ReadLineTextFile = MyFile.ReadLine ' ", !"
End Function

Skip
TextStream. :

.Skip()
:

. TextStream.

. , .

.
Skip. 8 (
!).

Function SkipTextFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("testfile.txt", ForWriting, True)
f.Write ", !"
Set f = fso.OpenTextFile("testfile.txt", ForReading)
f.Skip(8)
SkipTextFile = f.ReadLine
End Function

VBScript 5.6

130

SkipLine
TextStream. :

.SkipLine
:

. TextStream.

, ,
. , .
SkipLine.

Function SkipLineInFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("testfile.txt", ForWriting, True)
f.Write ", !" & vbCrLf & "VB Script - !" & vbCrLf
Set f = fso.OpenTextFile("testfile.txt", ForReading)
f.SkipLine
SkipLineInFile = f.ReadLine
End Function

Write
TextStream. :

.Write()
:

. TextStream.

. , .


. WriteLine ,
.
Write.

Function WriteToFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("testfile.txt", ForWriting, True)
f.Write ", !"
Set f = fso.OpenTextFile("testfile.txt", ForReading)
WriteToFile = f.ReadLine
End Function

VBScript 5.6

131

WriteLine
TextStream. :

.WriteLine([])
:

. TextStream.

. , . ,
.

WriteLine.

Function WriteLineToFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("testfile.txt", ForWriting, True)
f.WriteLine ", !"
f.WriteLine "VB Script - !"
Set f = fso.OpenTextFile("testfile.txt", ForReading)
WriteLineToFile = f.ReadAll
End Function

WriteBlankLines
TextStream. :

.WriteBlankLines()
:

. TextStream.

. , .

WriteBlankLines.

Function WriteBlankLinesToFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("testfile.txt", ForWriting, True)
f.WriteBlankLines 2
f.WriteLine ", !"
Set f = fso.OpenTextFile("testfile.txt", ForReading)
WriteBlankLinesToFile = f.ReadAll
End Function

VBScript 5.6

132

5.7.8. Match
.
.

Match Execute RegExp,


Match. Match
.
,
Match. Match ,
, , .
Match:
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches
' .
Set regEx = New RegExp
' .
regEx.Pattern = patrn
' .
regEx.IgnoreCase = True
' .
regEx.Global = True
' .
Set Matches = regEx.Execute(strng)
' .
For Each Match in Matches
' Match.
RetStr = RetStr & "Match " & I & " "
RetStr = RetStr & Match.FirstIndex & ". Match = "'
RetStr = RetStr & Match.Value & "'." & vbCRLF
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

5.7.8.1. Match
FirstIndex
. :

.FirstIndex
:

. Match.

FirstIndex ( ) .
(0).
FirstIndex.

VBScript 5.6

133

Function RegExpTest(patrn, strng)


Dim regEx, Match, Matches
' .
Set regEx = New RegExp
' .
regEx.Pattern = patrn
' .
regEx.IgnoreCase = True
' .
regEx.Global = True
' .
Set Matches = regEx.Execute(strng)
' .
For Each Match in Matches
' Match.
RetStr = RetStr & "Match " & I & " "
RetStr = RetStr & Match.FirstIndex & ". Match = "
RetStr = RetStr & Match.Value & "'." & vbCRLF
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

RegExpTest () is. IS1 is2 IS3


is4, .
.
Match, is
4 ( ).
Length
. :

.Length
:

. Match.

Length.
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches
' .
Set regEx = New RegExp
' .
regEx.Pattern = patrn
' .
regEx.IgnoreCase = True
' .
regEx.Global = True
' .
Set Matches = regEx.Execute(strng)
' .
For Each Match in Matches
' Match.
RetStr = RetStr & "Match " & I & " "
RetStr = RetStr & Match.FirstIndex & ". Match = "'
RetStr = RetStr & Match.Length
RetStr = RetStr & " ." & vbCRLF
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

VBScript 5.6

134

Value
, . :

.Value
:

. Match.

Value.
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches
' .
Set regEx = New RegExp
' .
regEx.Pattern = patrn
' .
regEx.IgnoreCase = True
' .
regEx.Global = True
' .
Set Matches = regEx.Execute(strng)
' .
For Each Match in Matches
' Match.
RetStr = RetStr & "Match " & I & " "
RetStr = RetStr & Match.FirstIndex & ". Match = "
RetStr = RetStr & Match.Value & "'." & vbCRLF
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

5.7.9. RegExp
.
RegExp.
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches
' .
Set regEx = New RegExp
' .
regEx.Pattern = patrn
' .
regEx.IgnoreCase = True
' .
regEx.Global = True
' .
Set Matches = regEx.Execute(strng)
' .
For Each Match in Matches
' Match.
RetStr = RetStr & "Match " & I & " "
RetStr = RetStr & Match.FirstIndex & ". Match = "
RetStr = RetStr & Match.Value & "'." & vbCRLF
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

VBScript 5.6

135

5.7.9.1. RegExp
Global
, ,
. :

.Global [= True | False ]


:

. RegExp. Global TRUE,


, FALSE. TRUE.

Global.
, .
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches
'
Set regEx = New RegExp
'
regEx.Pattern = patrn
'
regEx.IgnoreCase = True
'
regEx.Global = True
'
Set Matches = regEx.Execute(strng)
RegExpTest = RetStr
End Function

.
.
.
.
.
' .

MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))


IgnoreCase
, ,
. :

.IgnoreCase [= True | False ]


:

. RegExp. IgnoreCase TRUE,


, FALSE. TRUE.

IgnoreCase.
, .
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches
'
Set regEx = New RegExp
'
regEx.Pattern = patrn
'
regEx.IgnoreCase = True
'
Set Matches = regEx.Execute(strng)
RegExpTest = RetStr
End Function

.
.
.
.
' .

MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

VBScript 5.6

136

Pattern
() ,
. :

.Pattern [= ]
:

. RegExp.

. .
, .


.
, .

. , n
n, \n . \\
\, /( (.

. , zo*
z, zoo.

. , zo+
zoo, z.

. , a?ve?
ve never.

, .

(pattern) pattern .
Matches, Item [0]...[n].
\( \).

x|y

y. , z|food z food. (z|f)ood


z food.

{n}

n . n . , o{2}
Bob, foooood.

{n,}

n . n . , o{2,}
Bob, foooood. o{1,}
o+. o{0,} o*.

{n,m}

n m . n m .
, o{1,3} foooood. o{0,1} o?.

[xyz]

. . , [abc]
plain.

[^xyz]

. .
, [^abc] p plain.

[a-z]

. . , [a-z]
z.

VBScript 5.6

[^m-z]

137

. , .
, [^m-z] (, ),
m z.

\b

, .
, er\b er never, er verb.

\B

. , e*r\ er never early.

\d

-. [0-9].

\D

. [^0-9].

\f

\n

\r

\s

: , , ..
[ \f\n\r\t\v].

\S

. [^ \f\n\r\t\v].

\t

\v

\w

, . [A-Zaz0-9_].

\W

, . [^AZa-z0-9_].

\num

num, num .
. , (.)\1
.

\n

n, n . 1, 2
3 . , \11 \011 . \0011
\001 & 1. 256. ,
.
ASCII-.

\n

n, n .
2 . , \41 . \041 \04 & 1.
ASCII-.

Pattern.
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches
'
Set regEx = New RegExp
'
regEx.Pattern = patrn
'
regEx.IgnoreCase = True
'
Set Matches = regEx.Execute(strng)
RegExpTest = RetStr
End Function

.
.
.
.
' .

MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

VBScript 5.6

138

5.7.9.2. RegExp
Execute
. :

.Execute()
:

. RegExp.

. , .

() Pattern
RegExp.
Execute Matches, Match
, . ,
Execute Matches.
Execute.
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches
'
Set regEx = New RegExp
'
regEx.Pattern = patrn
'
regEx.IgnoreCase = True
'
Set Matches = regEx.Execute(strng)
RegExpTest = RetStr
End Function

.
.
.
.
' .

MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

Replace
, . :

.Replace(1, 2)
:

. RegExp.

. , ,
.

. , ,
.

VBScript 5.6

139

() Pattern
RegExp.
Replace 1 RegExp.Pattern,
2. , 1 .
Replace.
Function ReplaceTest(patrn, replStr)
Dim regEx, str1
' .
str1 = " ."
Set regEx = New RegExp
' .
regEx.Pattern = patrn
' .
regEx.Global = True
' .
regEx.IgnoreCase = True
' .
ReplaceTest = regEx.Replace(str1, replStr)
' .
End Function
MsgBox(ReplaceTest("", ""))

' .

Replace .
,
.
MsgBox(ReplaceTest("(\S+)(\s+)(\S+)", "$3$2$1"))
:
.

Test

, , . :

.Test()
:

. RegExp.

. , .

() Pattern
RegExp. RegExp.Global .
Test TRUE, .
, FALSE.

VBScript 5.6

Test.
Function RegExpMetTest(patrn, strng)
Dim regEx, retVal
' .
Set regEx = New RegExp
' .
regEx.Pattern = patrn
' .
regEx.IgnoreCase = False
' .
retVal = regEx.Test(strng)
' .
If retVal Then
RegExpMetTest = " ."
Else
RegExpMetTest = " ."
End If
End Function
MsgBox(RegExpMetTest("is.", "IS1 is2 IS3 is4"))

140

Оценить