VBScript 函数

http://www.jb51.net/shouce/vbs/vsstmclass-1.htm

VB用户手册

------------------------------------------------------------------------

函数:

类别

关键字

数组处理

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
  函数, Sub
  属性 Get, 属性 Let, 属性 Set

错误处理

On Error
  Err

表达式

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 函数
  Execute 语句
  RGB 函数

对象

Create对象
  Err 对象
  Get对象
  RegExp

运算符

加法 (+),减法 (-)

(^)
  求余算术运算 (Mod)
  乘法 (*)除法(/)
  整除()
  求补(-)
  字符串连接(&)
  Is
  And, Or, Xor
  Eqv, Imp

选项

Option Explicit

过程

Call
  函数, Sub
  属性 Get, 属性 Let, 属性 Set

四舍五入

Abs
  Int, Fix, Round
  Sgn

Script 引擎 ID

ScriptEngine
  ScriptEngineBuildVersion
  ScriptEngineMajorVersion
  ScriptEngineMinorVersion

字符串

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
  Is对象
  TypeName
  VarType

Script 引擎标识

ScriptEngine
  ScriptEngineBuildVersion
  ScriptEngineMajorVersion
  ScriptEngineMinorVersion

VBScript 对象

1, 类—声明

Class name

Statements----定义类的变量,属性和方法

End Class

********类的缺省属性和方法是在它们的声明部分用 Default 关键字指定的。

2,创建对象:供了对的各种事件的访问。

Dim X

Set X = New classname

Set 语句

将对象引用赋给一个variable或property,或者将对象引用与事件关联。

Set objectvar ={objectexpression | New classname | Nothing}

-或者-

Set object.eventname =GetRef(procname)

例如:

Set fso =CreateObject("Scripting.FileSystemObject")

Set d =fso.GetDrive(fso.GetDriveName(drvPath))

 

With 语句

对一个对象执行一系列的语句

With 语句可以用来对指定的对象执行一系列的语句,但不需要重复地说明对象的名称。例如,如果要修改一个对象的多个属性,可以将所有属性赋值语句放在 With 控制结构中,这样对对象的引用就只需要一次,而不是在每个赋值语句中都引用。

With MyLabel 

.Height =2000 

.Width =2000 

.Caption ="这是MyLabel"

End With

 

过程:

定义

Sub.....End Sub, 执行操作但不返回值。

   可以使用参数,如果无参数,则SubName后面必须带空括号 ()。

    Sub  SubName

               ............

           Exit Sub

               ............

               End Sub

调用

SubName 参数1,参数2            

Call SubName(参数1,参数2) 

使用 Exit Sub 语句可以立即从 Sub 过程中退出。

函数

Function.....End Function ,通过函数名返回一个值,这个值是在过程的语句中赋给函数名的。

可以使用参数,如果无参数,则FunctionName后面必须带空括号 ()。

1,  定义一个Function的时候,可以直接给函数名(如IsEnvExist)赋值,然后用函数名(IsEnvExist)返回结果。

如上面的例子中

Function IsEnvExist(Env_Variable)

...........

IsEnvExist=False 

..........

End Function

If IsEnvExist("XXX")=false Then

使用 Exit Function 语句可以从 Function 过程中立即退出。

调用SubFunction 过程。

[Call] name [argumentlist]

Call name (argumentlist)

或者

name argumentlist

call和参数两边的括号必须一起出现

Exit 语句

退出 Do...Loop、For...Next、Function 或 Sub 代码块。

Exit 语句的语法有以下几种形式:

语句

描述

Exit Do

退出 Do...Loop 语句

Exit For

退出 For 循环的方法。只能在 For...NextFor Each...Next 循环中使用。

Exit Function

退出 Function 过程。

Exit Property

从所在的 Property 过程中退出。

Exit Sub

退出 Sub 过程

 

Execute语句:执行一个或者多个语句,多条语句之间用分号隔开.

 Execute "Sub Proc2: Print X: End Sub" 

FileSystemObject(FSO) 对象模式包含下面的对象和集合。

对象/集合

描述

FileSystemObject  

主对象。包含用来创建、删除和获得有关信息,以及通常用来操作驱动器、文件夹和文件的方法和属性。和该对象相关联的许多方法,与其他 FSO 对象中的方法完全相似;它们是为了方便才被提供的。

Drive

对象。包含用来收集信息的方法和属性,这些信息是关于连接在系统上的驱动器的,如驱动器的共享名和它有多少可用空间。请注意,"drive" 并非必须是硬盘,也可以是 CD-ROM 驱动器,RAM 磁盘等等。并非必须把驱动器实物地连接到系统上;它也可以通过网络在逻辑上被连接起来。

Drives  

集合。提供驱动器的列表,这些驱动器实物地或在逻辑上与系统相连接。Drives 集合包括所有驱动器,与类型无关。要可移动的媒体驱动器在该集合中显现,不必把媒体插入到驱动器中。

File

对象。包含用来创建、删除或移动文件的方法和属性。也用来向系统询问文件名、路径和多种其他属性。

Files

集合。提供包含在文件夹内的所有文件的列表。

Folder  

对象。包含用来创建、删除或移动文件夹的方法和属性。也用来向系统询问文件夹名、路径和多种其他属性。

Folders  

集合。提供在 Folder 内的所有文件夹的列表。

TextStream  

对象。用来读写文本文件。

CreateTextFile

FileSystemObject 或Folder 对象调用。

返回 TextStream 对象

OpenTextFile 方法

FileSystemObject 对象调用

打开指定的文件并返回一个 TextStream 对象

GetFile 方法

FileSystemObject对象调用

返回File 对象。

OpenAsTextStream 方法

File对象调用

打开指定的文件并返回一个 TextStream 对象,

Microsoft Scripting 运行时库参考

Scripting.FileSystemObject

FileSystem对象

主对象

Set fso =CreateObject("Scripting.FileSystemObject")
 

方法:

                                                                                                                                                       
   

Fso的File方法

   
   

Fso的Folder方法

   
   

Fso的Drive方法

   
   

Fso的Path方法

   
   

GetFile

   
   

GetFolder

   
   

GetDrive

   
   

BuildPath

   

GetBaseName

   

GetDriveName

   

GetExtensionName

   

GetFileName

   

GetParentFolderName

   

GetSpecialFolder

   

GetTempName

   
   

CreateTextFile

   
   

CreateFolder

   
       
   

OpenTextFile

   
              
   

DeleteFile

   
   

DeleteFolder

   
       
   

CopyFile

   
   

CopyFolder

   
       
   

MoveFile

   
   

MoveFolder

   
       
   

FileExists

   
   

FolderExists

   
   

DriveExists

   

*******************创建/打开文本文件***************

FileSystemObject 对象的 CreateTextFile/OpenTextFile 方法,返回TextStream 对象

FileSystemObject 对象的GetFile 方法,返回File对象;

File对象的方法,必须通过fso.GetFile 对象调用。

属性:

Drives

 

Drives

集合

所有可用驱动器的集合

Set   fso =CreateObject("Scripting.FileSystemObject")

Set dc =fso.Drives

Function ShowDriveList
   Dim fso, d, dc, s, n 
   Set fso =CreateObject("Scripting.FileSystemObject") 
   Set dc =fso.Drives 
   For Each d in dc '---枚举集合的成员,d实际是Drive对象,所以可以调它的属性
   n ="" 
   s =s & d.DriveLetter & " - "  
   If d.DriveType =Remote  Then 
   n =d.ShareName 
   ElseIf d.IsReady  Then 
   n =d.VolumeName 
   End If 
   s =s & n &vbCrLf  
   Next 
   ShowDriveList =s
    msgbox ShowDriveList
   End Function

Count/Item

无方法

Drive

对象

提供对磁盘驱动器或网络共享的属性的访问

Set fso =CreateObject("Scripting.FileSystemObject")
  Set d =fso.GetDrive(fso.GetDriveName(drvPath))

msgbox "AvailableSpace:" & d.AvailableSpace'----可用空间大小
  msgbox "FreeSpace:" & d.FreeSpace'----与AvailableSpace基本相同
  msgbox "DriveLetter:" & d.DriveLetter'---驱动器号
  msgbox "DriveType:" & d.DriveType'------指定驱动器的类型的值。
  msgbox "FileSystem:" & d.FileSystem'-----文件系统的类型
  msgbox "IsReady:" & d.IsReady'-----指定的驱动器是否就绪
  msgbox "Path:" & d.Path'-----路径
  msgbox "RootFolder:" & d.RootFolder'-----根文件夹
  msgbox "SerialNumber   :" & d.SerialNumber'-----返回十进制序列号,用于唯一标识一个磁盘卷。
  msgbox "ShareName:" & d.ShareName'-----驱动器的网络共享名
  msgbox "TotalSize:" & d.TotalSize'-----总字节数
  msgbox "VolumeName:" & d.VolumeName'-----驱动器的卷标

AvailableSpace

FreeSpace

DriveLetter

DriveType

FileSystem

IsReady

Path

RootFolder

SerialNumber  

ShareName

TotalSize

VolumeName

无方法

Folders   集合

包含在一个 Folder 对象中的所有 Folder 对象的集合

Set fso =CreateObject("Scripting.FileSystemObject")
  Set f =fso.GetFolder(folderspec)’----

Set fc =f.SubFolders

Function ShowFolderList(folderspec)
  Dim fso, f, f1, fc, s
  Set fso =CreateObject("Scripting.FileSystemObject")
  Set f =fso.GetFolder(folderspec) '----没有单独针对folders集合的属性,所以,folders与 folder用同样的GetFolder()属性,
  Set fc =f.SubFolders '----   SubFolders属性相当于fso.Drives
  For Each f1 in fc'---枚举集合的成员,f1实际是Folder对象,所以可以调它的属性
  s =s & f1.name
  s =s & vbCrlf
  Next
  ShowFolderList =s
  End Function

Count

一个方法:

Add

Folder 对象

对文件夹所有属性的访问

Set fso =CreateObject("Scripting.FileSystemObject")
Set f =fso.GetFolder(folderspec)

Folder的属性比File属性多了三个:Files, IsRootRolder

SubFolders

 

前三个方法跟File方法相同:

Copy

Delete

Move

CreateTextFile

Attributes

DateCreated

DateLastAccessed

DateLastModified

Drive

Name

ParentFolder

Path

ShortName

ShortPath

Size

Type

Files

IsRootFolder

SubFolders

Files

文件夹中所有 File 对象的集合.

Set fso =CreateObject("Scripting.FileSystemObject")
  Set f =fso.GetFolder(folderspec)
  Set fc =f.Files

Function ShowFolderList(folderspec)
  Dim fso, f, f1, fc, s
  Set fso =CreateObject("Scripting.FileSystemObject")
  Set f =fso.GetFolder(folderspec)'---没有直接访问Files的属性
  Set fc =f.Files'---Files必须通过folder对象来访问
  For Each f1 in fc'---f1实际上是file对象,所以可以调用file的属性
  s =s & f1.name
  s =s & vbCrlf
  Next
  ShowFolderList =s
  End Function

Count/Item

 

无方法

File 对象

对文件的所有属性的访问

Set fso   =CreateObject("Scripting.FileSystemObject")

Set f   =fso.GetFile(filespec)

右键---property里的所有的属性,对应右边的属性。

前三个方法跟Folder方法相同:

Copy

Delete

Move

OpenAsTextStream

 

                                                                     
   

Folder对象的方法

   
   

File对象的方法

   
   

Copy

   
   

Copy

   
   

Delete

   
   

Delete

   
   

Move

   
   

Move

   
   

CreateTextFile

   
   

OpenAsTextStream

   

***CreateTextFile方法可以被FileSystemObject 或 Folder 对象调用

****File的方法,必须通过fso.GetFile 对象调用。

Attributes
  DateCreated,

DateLastAccessed

DateLastModified
  Drive

Name,
  ParentFolder

Path
  ShortName,

ShortPath
  Size

Type

TextStream对象

 

Close

Skip

SkipLine

Read

ReadAll

ReadLine

Write

WriteLine

WriteBlankLines

AtEndOfLine

AtEndOfStream

Column

Line

 

 

 

 

 

分类

功能/关键字

属性

集合

Drives
  Files
  Folders

Count

数据存储

Dictionary

字典

 Add
  Exists
  Items, Keys
  Remove, RemoveAll
  Count
  Item, Key

文件系统

Drive

File
  FileSystem对象
  Folder
  TextStream

FileSystem对象

BuildPath
  CopyFile, CopyFolder
  CreateFolder,CreateTextFile
  DeleteFile,DeleteFolder
  DriveExists, FileExists, FolderExists
  GetAbsolutePathName, GetBaseName
  GetDrive, GetDriveName
  GetFile, GetExtensionNameGetFileName
  GetFolder, GetParentFolderName
  GetSpecialFolder
  GetTempName
  MoveFile, MoveFolder
  OpenTextFile
  Drives

驱动器

AvailableSpace
  Count
  DriveLetter
  DriveType
  FileSystem
  FreeSpace
  IsReady
  Item
  RootFolder
  SerialNumber
  ShareName
  TotalSize
  VolumeName

文件
  文件夹

Add
  Attributes
  Copy, Delete, Move
  Count
  OpenAsTextStream
  DateCreated,

DateLastAccessed,

DateLastModified
  Drive
  Item
  ParentFolder
  Name,

Path
  ShortName,

ShortPath
  Size

TextStream  

  Close
  Read, ReadAll, ReadLine
  Skip, SkipLine
  Write, WriteBlankLines, WriteLine
  AtEndOfLine, AtEndOfStream
  Column, Line

原文地址:https://www.cnblogs.com/xiaoxiapier/p/4530997.html