|
黄金长老
 
- UID
- 146500
- 帖子
- 36
- 精华
- 0
- 积分
- 643
- 菊花元
- 643 元
- 威望
- 0 点
- 阅读权限
- 40
- 在线时间
- 26 小时
- 注册时间
- 2007-3-13
- 最后登录
- 2008-7-23
|
顶楼
大 中
小 发表于 2007-8-13 08:37 只看该作者
   
开始学系统管理脚本(四)之循环语句
不知道为什么,排版老是会乱,每次都要重新排,请版主帮忙,看得辛苦的可以先到我的blog看http://blog.absee.net/post/307.html
, e' @) B4 A# q6 ^9 t ! l. J m6 V4 i7 ^
" [$ `5 t/ u; r4 |* ZChapter 10. Controlling the Flow of Execution简单的If…Then语句嵌套的If…Then…Else…ElseIfDim iMyNumber iMyNumber = InputBox("Enter a number from 1-100") If iMyNumber = 1 Then MsgBox "1 is a good number." ElseIf iMyNumber > 1 And iMyNumber < 50 MsgBox "2 to 49: Numbers of indecision" ElseIf iMyNumber = 50 MsgBox "Heading right for the middle, huh?" ElseIf iMyNumber > 50 And iMyNumber < 99 MsgBox "51 to 99: You like the upper half" ElseIf iMyNumber = 99 MsgBox "99 is just short of 100" ElseIf iMyNumber = 100 MsgBox "You went all the way!" Else MsgBox "You didn't enter 1 to 100!" End If
( `7 |% ?3 t. e) v' h8 SSelect…Case语句Dim iMyNumber iMyNumber = InputBox("Enter a number from 1-100") Select Case iMyNumber Case 1 9 [5 x; K( {/ x
MsgBox "1 is a good number." Case 2 To 49
: s% i. r5 X( H4 GMsgBox "2 to 49: Numbers of indecision" Case 50
' i% j& f9 T: u% t/ d7 |8 @MsgBox "Heading right for the middle, huh?" Case 51 To 98 ' P3 Z5 g7 z: g
MsgBox "51 to 99: You like the upper half" Case 99 ( Y- H' P8 w( J( I) `: o
MsgBox "99 is just short of 100" Case 100 . W% K8 \, c! r2 s0 K# n. L Z
MsgBox "You went all the way!" Case Else 9 ?6 O& P3 L+ E# u* `
MsgBox "You didn't enter 1 to 100!" End Select 7 O, }$ M0 z$ ~
# v7 R2 W' }- [( ~: E
3 \) `2 g8 c8 z9 m6 I5 ?LoopsDo While…Loop and Do…Loop While
' e3 H Z, q! Z, k4 l; n; _6 W! P' BIsNumeric(expression)
# L: q$ E8 J4 ^7 R t8 H* G如果整个 expression
0 h4 |! f9 {, D+ H. k+ M被识别为数字,IsNumeric, y \; T% f! |6 d8 F) p2 G
函数返回 True;否则函数返回 False。, \8 R- l' x1 u, ]% L
9 ]1 L$ ]9 x9 V# y& I3 L
Do…Loop While例子,假如输入不是数字的话,就继续循环,遇到输入为纯数字时才跳出:! P% f+ x9 B0 e7 W
Dim iNumber Do iNumber = InputBox(" lease enter a number.") Loop While Not IsNumeric(iNumber) MsgBox "Thank you!". g6 s0 _' z/ v9 m. o
* z& h1 h y7 \% Y- ?% m
Do为单独一行,do与loop while之间为循环语句部分;Loop While语句当条件为true时就循环,为假时跳出循环;Loop While Not语句,当条件为False的时候就循环,当条件为True时就跳出循环。% |8 Y% e0 O2 U, ?/ J$ `8 j
. @8 a7 _; a3 ~5 P! @, n. x% y
# q( s$ `$ H( |$ ^2 r7 f' T4 ODo While…Loop例子:当没有读取到文件内容结束时继续循环:8 l$ q" o$ D! M0 o, m: w
' assumes oFile is some kind of file object ' that is opened for reading Dim sData Do While Not oFile.EndOfFile ‘可换为Do While oFile.EndOfFile = False sData = oFile.Read MsgBox sData Loop
3 O" M% t" v0 o- nDo Until…Loop and Do…Loop Until' assumes oFile is some kind of file object ' that is opened for reading Dim sData Do Until oFile.EndOfFile sData = oFile.Read MsgBox sData Loop
9 W1 J* o; Q& w5 V: E9 LFor…Next当你需要执行一个脚本固定多少次的时候,可以使用For…Next语句。4 [; s! K/ v7 q, n) _
9 u4 N0 [5 H# v* _, Y让电脑.哔哔声响8次的脚本:; k" [# g. d7 g8 i
For iCount = 1 To 8 Beep Next & T# I9 f. J* C* c
For语句默认为每次增加一,如果你想要改变它的步长,如每次增加2的话,可以在后面加step 2,如以下例子: Dim iCount For iCount = 2 To 10 Step 2 MsgBox iCount Next
3 d/ K d& ]- L c: S. k& N+ ~0 B另外,你也可以采用负数来做一个由大到小的循环,如:7 H4 t A a! m; e
- U' e5 J" B* X) e I8 B5 j% `$ kDim iCount For iCount = 10 to 1 Step –1 MsgBox iCount Next MsgBox "Blast off!"
* s+ Y' B0 }% ~6 m$ x& ^它将从10往1倒数,并在最后显示"Blast off!"
+ W. x3 H/ v+ E( q ; p4 W0 M/ m0 e; l/ S8 y
For Each…Next当你不确定你想要处理的循环语句的次数或想遍历某些对象或内容时,可以使用For Each…Next语句。4 N! q( l8 m8 v! x
, A% E9 F3 ?$ u) Q例子,假设oRoot代表C盘根目录,现在想查找有无一个叫WINDOWS的文件夹:
% M" Q: s0 p* F* A+ {
- I9 ^. \9 N" M2 h V' Assume oRoot represents the root folder of C ' and has a Subfolders property that is a ' collection of folder objects that represent ' the subfolders of C Dim oSubfolder For Each oSubfolder In oRoot.Subfolders If oFolder.Name = "WINDOWS" Then 8 j+ H Q- S3 C* |3 j- A
MsgBox "Found the Windows folder!" End If Next + ~5 K- p% _2 i6 w5 J- T
Exiting Loops (直接退出循环,一般与if连用)' that is opened for reading ' assumes oFile is some kind of file object Dim iData, iSum Do Until oFile.EndOfFile iData = oFile.Read iSum = iSum + iData If iSum > 1000 Then # P9 }& x# q6 K: O6 C, |; }& _
Exit Loop End If Loop 3 Q- t P0 W8 ?; a/ t( f
当iSum>1000时,直接退出循环。# H$ t i$ V* N7 D4 Y
3 h1 Z7 m1 u* _: e
0 I9 }( a) K& i( S% w9 Y- _/ U' m7 @$ x1 r9 l0 c4 R2 y5 h1 P
; f7 G+ u0 P/ M
5 d/ W( p9 C9 m3 e( O
9 g) ~( o7 ?: [$ h) g& M3 N$ p[ 本帖最后由 jacketlee 于 2007-8-13 08:52 编辑 ]
搜索更多相关主题的帖子:
系统管理 脚本 语句
|