VBScript入门篇

                           VBScript入门篇

                                                      作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

一.定义一个过程

 

 1 定义一个过程:可以将相同的操作的代码提取出来,方便其他人来调用这段代码,可以减少你的代码的重复性
 2 
 3 
 4 Option Explicit
 5 '@author :yinzhengjie
 6 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 7 'EMAIL:y1053419035@qq.com
 8 
 9 
10 '定义一个数组
11 Dim ary_1(2)
12 ary_1(0) = 100
13 ary_1(1) = 200
14 ary_1(2) = 300
15 
16 
17 '定义一个过程的方法,Sub表示一个过程的开始,End Sub表示一个过程的结束标识符
18 Sub max_number(num_1,num_2,num_3)
19 Dim max
20 If num_1 > num_2 Then
21 max = num_1
22 Else
23 max = num_2
24 End If 
25 If num_3 > max Then max = num_3
26 MsgBox "最大值是:" & max
27 End Sub
28 
29 
30 '调用一个过程的方法,用call方法实现,或者是不用,两种区别不大,就是前者有括号,后者无括号
31 Call max_number(1,2,3) '需要用括号将参数传递进去
32 Call max_number(ary_1(0),ary_1(1),ary_1(1))
33 max_number 4,5,6 '如果不用call调用过程的话,后面传参不需要用括号包裹起来。

 

 

二.定义一个函数

 1 定义一个函数:
 2 
 3 Option Explicit
 4 '@author :yinzhengjie
 5 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 6 'EMAIL:y1053419035@qq.com
 7 
 8 '定义一个数组
 9 Dim ary_1(2),max
10 ary_1(0) = 100
11 ary_1(1) = 200
12 ary_1(2) = 300
13 
14 '定义一个函数,Function表示函数的开始标识符,End Function 表示函数的结束标识符.sub(过程)能实现的Function(函数)都能实现,而且Function比sub功能更加强大,因为它有返回值,请看案例:
15 Function  max_number(num_1,num_2,num_3)
16     If num_1 > num_2 Then
17         max_number = num_1
18     Else
19         max_number = num_2
20     End If 
21     If num_3 > max Then  max_number = num_3
22     'Exit Function表示退出函数
23 End Function 
24 
25 '调用函数的方法
26 max = max_number(ary_1(0),ary_1(1),ary_1(2))
27 MsgBox "最大值是:" & max

 

三.ByRef 和 ByVal 语句区别:

 1 ByRefByVal 语句区别:
 2 
 3 Option Explicit
 4 '@author :yinzhengjie
 5 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 6 'EMAIL:y1053419035@qq.com
 7 
 8 '定义一个数组
 9 Dim ary_1(2),max
10 ary_1(0) = 100
11 ary_1(1) = 200
12 ary_1(2) = 300
13 
14 Function max_number(byval num_1,byref num_2,num_3)
15 MsgBox num_1 & "#"  & num_2
16     num_1 = 666666
17     num_2 = 888888
18     
19     MsgBox num_1 & "#" & num_2
20 End Function
21 
22 max_number ary_1(0),ary_1(1),ary_1(2)
23 MsgBox ary_1(0) & "#" & ary_1(1)
24 
25 '通过以上的这个案例,我们可以总结如下:
26 'ByVal :表示该参数是按值方式传递的。这个只能让局部生效
27 'ByRef :表示该参数按引用方式传递。表示在局部变量如果改动了某个变量的值,那么会全局生效!
28 '所以不建议用ByRef,因为你只要稍微不注意,就会在全局改变这个变量,我们一般使用的是ByVal

 

五.变量的作用域与存活期

  变量的作用域由声明它的位置决定。如果在过程中声明变量,则只有该过程中的代码可以访问或更改变量值,此时变量具有局部作用域并且是过程级变量。如果在过程之外声明变量,则该变量可以被脚本中所有过程所识别,称为 Script 级变量,具有脚本级作用域。我们可以通过一个脚本来判断作用域,方便我们理解

 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim  num
 7 
 8 num = 100
 9 
10 getmax
11 
12 MsgBox num '全局变量
13 
14 Function getmax()
15     Dim num
16     num = 666  '局部变量,而且局部变量的名称和全局变量名称重复
17     MsgBox num  
18     num = 888   '修改局部变量所对应的值
19     MsgBox num
20 End Function 
21 
22 '建议实际生产环节中不要在局部变量和全局变量的变量名定义相同的哟,我这里是为了测试,方便大家理解。

  

  变量存在的时间称为存活期。Script 级变量的存活期从被声明的一刻起,直到脚本运行结束。对于过程级变量,其存活期仅是该过程运行的时间,该过程结束后,变量随之消失。在执行过程时,局部变量是理想的临时存储空间。可以在不同过程中使用同名的局部变量,这是因为每个局部变量只被声明它的过程识别。

 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim ary_1(2),max
 7 ary_1(0) = 100
 8 ary_1(1) = 200
 9 ary_1(2) = 300
10 
11 '获取两个值中的最大值
12 Public Function get_num(num_1,num_2)
13     pass '我是为了举列子,此处就用pass代替代码了
14 End Function
15 
16 
17 Public Function get_max(num_1,num_2)
18      get_num 100,200 '调用Public类的函数,不会报错!
19 End Function 
Public 公有的属性可以被其他函数调用
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim ary_1(2),max
 7 ary_1(0) = 100
 8 ary_1(1) = 200
 9 ary_1(2) = 300
10 
11 '获取两个值中的最大值
12 private Function get_num(num_1,num_2) '表示定义私有属性,不能被调用!
13     pass 
14 End Function
15 
16 
17 Public Function get_max(num_1,num_2)
18      get_num 100,200 '调用private类的函数会报错!不要这么干哟!
19 End Function 
private 私有的属性不能被其他函数调用!

 

六.对象的介绍

  对象:严格地说,对象是复杂数据和程序结构在内存中的表现,只有在程序运行时才存在。包含有方法和属性。其实就是一种封装的概念,它把事物封装成一个类,然后提供类的接口,而具体的过程人们是看不到的。

 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 'vbs创建对象概念
 7 '要使用一个对象,必须先用专门的命令创建一个对象。在一个对象实例化时,会在内存中生成一份那个对象的拷贝,同时会将指向该对象的指针(也被称为引用)存放到您申明该对象的变量中。使用Set和CreateObject()函数实例化对象,并将引用存放到申明的变量中。
 8 Dim objdict,objFSO,objFolder
 9 Set objdict = createobject("Scripting.Dictionary") '创建一个对象,您也可以理解成实例化一个对象
10 
11 'Set的唯一用途是实例化对象变量(存放指向该对象的引用)。
12 'CreateObject()完成的是创建对象的工作。使用该函数,都必须将您想要实例化的对象的标识符(prog id)传达给它,对象的库和类的名称一起构成可prog id.
13 
14 'Scripting是脚本运行时库(一些单独的组件)。有些库中的某些对象只能通过库中其他对象创建,FileSystemObject就是这样一个中介类。
15 'Folder对象就是这样一个对象。
16 
17 Set objFSO = CreateObject("Scripting.FileSystemObject")
18 Set objFolder = objFSO.GetFolder("C:\Users\yinzhengjie")  '注意:此处输入的路径必须是系统中存在的哟,不然你随便输入一个不存在的路径打印的时候回报错的哟!!
19 MsgBox objFolder 
vbs创建对象用法展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim my_phone,
 7 Set my_phone = CreateObject("phone_number") 'set其实就是实例化一个类,如果不存在“phone_number”这个类的话就会报错,所以我们在实例化的时候要看你是否已经自定义过这个类哟
 8 my_phone.email = "y1053419035@qq.com"  '表示对“my_phone”定义一个“email”方法。
 9 my_phone.number = "153****5200"
10 
11 MsgBox my_phone.email
如何实例化自定义的类
1 Option Explicit
2 '@author :yinzhengjie
3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
4 'EMAIL:y1053419035@qq.com
5 
6 Dim myphone 
7 Set myphone = Nothing  '释放内存,不写也是可以的,因为在脚本运行结束时会自动释放掉内存,不过建议还是写上哟~
释放内存展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 '1.属性:
 7 '        . 的作用
 8 '        只读 /可写/只读+可写
 9 
10 '2.方法:
11 '        . 的作用
12 '        与函数的使用方法类似
对象的用法

 

七.WScript.Shell对象

  可以用于获取系统环境变量的访问、创建快捷方式、访问Windows的特殊文件夹,如Windows Desktop,以及添加或删除注册表条目。还可以使用Shell对象的功能创建更多的定制对话框以进行用户交互。 

WshShell对象有三个属性介绍:
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 WshShell对象有三个属性:
 7     1>● CurrentDirectory; '当前目录路径,是绝对路径
 8 
 9     2>● Environment; '获取系统环境变量
10 
11     3>● SpecialFolders; '获取特殊文件夹路径,如:桌面,system32等等

WshShell对象有11个方法介绍:
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 
 7 WshShell对象有11个方法: 
 8AppActivate 
 9     '激活窗口
10 ● CreateShortcut 
11     ' 创建快捷方式用的
12 ● ExpandEnvironmentStrings 
13     '扩充的环境变量的字符串
14 ● LogEvent
15     ' 记录日志事件
16 ● Popup
17     ' 与msgbox功能类似,只不过msgbox需要用户点击才能消失对话框,而这个有自动消失的功能
18 ● RegDelete
19     ' 删除注册表的某个键值
20 ● RegRead
21     ' 读取注册表的某个键值
22 ● RegWrite
23     ' 写入注册表的某个键值
24 ● Run 
25     '可以运行一个cmd的字符串
26 ● SendKeys 
27     '模拟键盘输入,功能相当强大
28 ● Exec
29     ' 与run很类似,可以参考vbscript程序员参考手册

 

 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 
 7 Dim windows_shell,desktop,object_link
 8 
 9 Set windows_shell = CreateObject("WScript.Shell") '创建一个对象(实例化)
10 desktop = windows_shell.SpecialFolders("Desktop") '使用SpecialFolders方法获取桌面的路径
11 'MsgBox desktop
12 Set object_link = windows_shell.CreateShortcut(desktop & "\yinzhengjie_cmd.lnk") '定义链接文件路径并且给他起了一个名字叫“yinzhengjie_cmd”,注意以“.lnk”结尾标志着链接文件哟
13 
14 object_link.TargetPath = "C:\Windows\System32\cmd.exe" '源文件链接的绝对路径
15 
16 object_link.WorkingDirectory = "C:\Windows\System32" '源文件的工作路径,也就是他的所在目录(父目录)
17 
18 object_link.save '保存以上的配置,因为以上代码只是将数据加入到缓存中,并没有将数据写入磁盘中去,所以这个步骤是必须的!
19 
20 Set windows_shell = Nothing '以上代码执行完毕后,需要释放掉内存地址。
21 Set desktop = Nothing
22 Set object_link = Nothing 
利用WScript.Shell对象创建一个cmd快捷方式
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 
 7 Dim windows_shell
 8 
 9 Set windows_shell = CreateObject("WScript.Shell") '创建一个对象(实例化)
10 
11 MsgBox "我是msgbox"  '会打印"我是msgbox" ,直到你点击“确定”按钮才能走以下的代码
12 
13 windows_shell.Popup  "我是popup",2  '会打印"我是popup",并在2s后退出程序。
14 
15 Set windows_shell = Nothing 
MsgBox 与Popup 用法展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 
 7 Dim windows_shell
 8 
 9 Set windows_shell = CreateObject("WScript.Shell") '创建一个对象(实例化)
10 
11 
12 windows_shell.Run "cmd" '运行"cmd"命令,会弹出一个cmd对话框,
13 
14 WScript.Sleep  100 '表示延时100ms(毫秒)后再执行以下的代码,就是让程序睡一会,一般电脑卡可以这么干,因为同事打开多个文件会卡顿呢!
15 
16 windows_shell.Run "notepad" '打开一个记事本
17 
18 Set windows_shell = Nothing 
Run发放的应用展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 '程序员如何表白?嘻嘻~装逼的时刻来了,拿起键盘跟着哥的节奏撸起来!
 7 
 8 Dim windows_shell
 9 Set windows_shell = CreateObject("WScript.Shell") '创建一个对象(实例化)
10 windows_shell.Run "notepad" '打开一个记事本
11 
12 WScript.Sleep  100 '表示延时100ms(毫秒)后再执行以下的代码,就是让程序睡一会,一般电脑卡可以这么干,因为同事打开多个文件会卡顿呢!
13 
14 windows_shell.SendKeys "I" '打开笔记本程序后,会自动输入一个"I"的字符串进去,下面原理一样的,最终会呈现一个打开记事本呈现自动输入一些字符串进去。
15 
16 WScript.Sleep  1000 '让程序睡1000ms,也就是睡1s
17 
18 windows_shell.SendKeys "  "
19 WScript.Sleep  100
20 windows_shell.SendKeys "L"
21 WScript.Sleep  800
22 windows_shell.SendKeys "O"
23 WScript.Sleep  800
24 windows_shell.SendKeys "V"
25 WScript.Sleep  800
26 windows_shell.SendKeys "E"
27 WScript.Sleep  800
28 windows_shell.SendKeys "  "
29 WScript.Sleep  100
30 windows_shell.SendKeys "Y"
31 WScript.Sleep  800
32 windows_shell.SendKeys "O"
33 WScript.Sleep  800
34 windows_shell.SendKeys "U"
35 WScript.Sleep  800
36 windows_shell.SendKeys "  "
37 WScript.Sleep  100
38 
39 Set windows_shell = Nothing 
表白记事本案例展示

 

八.FileSystemObject对象

  FileSystemObject (FSO) 对象模型,允许对大量的属性、方法和事件,使用较熟悉的 object.method 语法,来处理文件夹和文件。

  作用: FSO 对象模型使应用程序能创建、改变、移动和删除文件夹,或探测特定的文件夹是否存在,若存在,还可以找出有关文件夹的信息,如名称、被创建或最后一次修改的日期,等等。

1.文件/文件夹复制、移动、重命名

 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 
 7 复制:Copy()、CopyFile () 、CopyFolder ()
 8 
 9 移动:Move()、MoveFile ()、MoveFolder ()
10 
11 重命名:object.Name [= newname] 

 

 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim file
 7 
 8 Set file = CreateObject("Scripting.filesystemobject") '实例化
 9 
10 file.CopyFile "E:\装饰器.txt","E:\python文档\" '这个步骤是将“E:\装饰器.txt”这个文件移动到"E:\python文档\",一定要注意最后的“\”,不然会报错的哟
11 
12 Set file = Nothing 
CopyFile用法展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim file
 7 
 8 Set file = CreateObject("Scripting.filesystemobject") '实例化
 9 
10 file.MoveFile "E:\装饰器.txt","E:\python文档\" '这个步骤是将“E:\装饰器.txt”这个文件移动到"E:\python文档\",一定要注意最后的“\”,不然会报错的哟
11 
12 Set file = Nothing 
MoveFile用法展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim file,filepath
 7 
 8 Set file = CreateObject("Scripting.filesystemobject") '实例化
 9 
10 filepath = "E:\装饰器.txt"
11 If file.FileExists("E:\python文档\装饰器.txt") Then  '判断文件"E:\python文档\装饰器.txt"是否存在,如果存在执行以下代码
12     MsgBox "文件已存在"
13 Else 
14     file.MoveFile filepath,"E:\python文档\" '如果直接移动或者复制一个文件的话,当目标目录已经存在该文件就会报错,
15 
16 End If 
17 Set file = Nothing 
FileExists用法展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim file,filepath,f
 7 
 8 Set file = CreateObject("Scripting.filesystemobject") '实例化
 9 
10 filepath = "E:\装饰器.txt"
11 
12 Set f = file.GetFile(filepath) '获取文件的源文件绝对路径
13 
14 f.Copy("E:\python文档\decorator.txt")  '方法一,拷贝文件的时候更改文件名称
15 
16 f.Name = "decorator_note" '方法二,不用copy文件,而是直接给上面定义好的文件绝对路径找到那个文件对象f,并给那个文件改名字。如果要改名的建议用方法二。
17 
18 Set file = Nothing 
19 
20 Set f = Nothing 
21 
22 Name用法展示
GetFile修改文件名用法展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim file,folderpath,f
 7 
 8 Set file = CreateObject("Scripting.filesystemobject") '实例化
 9 
10 folderpath = "E:\python文档"
11 
12 Set f = file.GetFolder(folderpath)
13 
14 f.Name = "python自动换运维之路" '重命名文件夹。
15 MsgBox "已经成功修改文件夹名称"
16 
17 Set file = Nothing 
18 Set f = Nothing 
19 Set folderpath = Nothing 
GetFolder修改文件夹名称用法展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim file,filepath,f
 7 
 8 Set file = CreateObject("Scripting.filesystemobject") '实例化
 9 
10 filepath = "E:\python自动换运维之路\decorator.txt"
11 
12 Set f = file.GetFile(filepath) '获取文件的源文件绝对路径
13 
14 MsgBox "文件大小:" & f.Size  '注意:size返回的是字节哟
15 
16 Set file = Nothing 
17 
18 Set f = Nothing 
Size方法用法展示

 

2.文件读取/写入

 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 第一步:OpenTextFile()
 7         或者OpenAsTextStream()
 8 第二步:Read 方法
 9         或者ReadAll 方法
10         或者ReadLine 方法
11 
12         Write 方法
13         WriteBlankLines 方法
14         WriteLine 方法

 

写入数据到文件方法展示如下:

 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim file,filepath,f
 7 
 8 Set file = CreateObject("Scripting.filesystemobject") '实例化
 9 
10 filepath = "E:\python自动换运维之路\生成器.exe" '定义一个文件的绝对路径和名称,注意:“.txt”表示文本文件,”.exe“表示被打包的二进制文件.
11 
12 Set f = file.CreateTextFile(filepath) '创建一个文件
13 
14 MsgBox "文件创建成功"
15 
16 Set file = Nothing 
17 
18 Set f = Nothing 
CreateTextFile创建文件用法展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim file,filepath,f,i
 7 
 8 Set file = CreateObject("Scripting.filesystemobject") '实例化
 9 
10 filepath = "E:\python自动换运维之路\生成器.txt" '定义一个文件的绝对路径和名称,注意:“.txt”表示文本文件,”.exe“表示被打包的二进制文件.
11 
12 Set f = file.CreateTextFile(filepath) '创建一个文件
13 MsgBox "文件创建成功"
14 
15 For i = 1 To 10
16     f.Write i '会把数字连续写进去,不会自动换行。
17 Next
18 
19 f.Close '关闭文件,可以将写入的东西存入硬盘中。
20 MsgBox "文件写入成功"
21 
22 
23 Set file = Nothing 
24 
25 Set f = Nothing 
Write用法展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim file,filepath,f,i,str
 7 
 8 Set file = CreateObject("Scripting.filesystemobject") '实例化
 9 
10 filepath = "E:\python自动换运维之路\生成器.txt" '定义一个文件的绝对路径和名称,注意:“.txt”表示文本文件,”.exe“表示被打包的二进制文件.
11 
12 Set f = file.CreateTextFile(filepath) '创建一个文件,注意:如果存在该文件会直接覆盖源文件,用的时候要注意哟!
13 MsgBox "文件创建成功"
14 
15 For i = 1 To 10
16     str = str & i & vbCrLf '其中vbCrLf是vbs内部的换行符
17 Next
18 f.Write str '将整个字符串一次性写入.
19 
20 f.Close '关闭文件,可以将写入的东西存入硬盘中。
21 MsgBox "文件写入成功"
22 
23 
24 Set file = Nothing 
25 
26 Set f = Nothing 
Write 配合vbCrLf 用法展示,推荐这种写法,可以提高文件的写入效率
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim file,filepath,f,i,str
 7 
 8 Set file = CreateObject("Scripting.filesystemobject") '实例化
 9 
10 filepath = "E:\python自动换运维之路\生成器.txt" '定义一个文件的绝对路径和名称,注意:“.txt”表示文本文件,”.exe“表示被打包的二进制文件.
11 
12 Set f = file.CreateTextFile(filepath) '创建一个文件,注意:如果存在该文件会直接覆盖源文件,用的时候要注意哟!
13 MsgBox "文件创建成功"
14 
15 For i = 1 To 10
16     f.Write i 
17     f.WriteBlankLines 2 '表示写入2个回车符号,所以他的功能就是一个换行功能而已
18 Next
19 f.Write str '将整个字符串一次性写入.
20 
21 f.Close '关闭文件,可以将写入的东西存入硬盘中。
22 MsgBox "文件写入成功"
23 
24 
25 Set file = Nothing 
26 
27 Set f = Nothing 
WriteBlankLines用法展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim file,filepath,f,i
 7 
 8 Set file = CreateObject("Scripting.filesystemobject") '实例化
 9 
10 filepath = "E:\python自动换运维之路\生成器.txt" '定义一个文件的绝对路径和名称,注意:“.txt”表示文本文件,”.exe“表示被打包的二进制文件.
11 
12 Set f = file.CreateTextFile(filepath) '创建一个文件
13 MsgBox "文件创建成功"
14 
15 For i = 1 To 10
16     f.WriteLine i
17 Next
18 
19 f.Close '关闭文件,可以将写入的东西存入硬盘中。
20 MsgBox "文件写入成功"
21 
22 
23 Set file = Nothing 
24 
25 Set f = Nothing 
WriteLine用法展示

 

读取文件内容的方法展示如下:

 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Const forreading = 1 '这个是openTextFile定义权限的方法,具体对应关系请看下面介绍
 7 'ForReading 1 以只读方式打开文件。不能写这个文件。 
 8 'ForWriting 2 以写方式打开文件 
 9 'ForAppending 8 打开文件并从文件末尾开始写。 
10 
11 
12 
13 Dim file,filepath,f,i,str
14 
15 Set file = CreateObject("Scripting.filesystemobject") '实例化
16 
17 filepath = "E:\python自动换运维之路\生成器.txt" '定义需要打开的文件,这个文件必须存在,不然会报错哟。
18 
19 Set f = file.openTextFile(filepath,forreading) '直接打开这个文件,根据上面的变量可知应该是只读的方式打开。当然我们也可以判断文件是否存在,如果存在再打开,否则提示文件不存在即可。
20 MsgBox "打开文件成功" 
21 str = f.ReadAll
22 
23 f.Close '关闭文件,可以将写入的东西存入硬盘中。
24 MsgBox "点击确定按钮显示文件内容"
25 MsgBox str
26 
27 
28 Set file = Nothing 
29 
30 Set f = Nothing 
openTextFile以及ReadAll用法展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Const forreading = 1 '这个是openTextFile定义权限的方法,具体对应关系请看下面介绍
 7 'ForReading 1 以只读方式打开文件。不能写这个文件。 
 8 'ForWriting 2 以写方式打开文件 
 9 'ForAppending 8 打开文件并从文件末尾开始写。 
10 
11 
12 
13 Dim file,filepath,f,i,str
14 
15 Set file = CreateObject("Scripting.filesystemobject") '实例化
16 
17 filepath = "E:\python自动换运维之路\装饰器.txt" '定义需要打开的文件,这个文件必须存在,不然会报错哟。
18 
19 Set f = file.openTextFile(filepath,forreading) '直接打开这个文件,根据上面的变量可知应该是只读的方式打开。当然我们也可以判断文件是否存在,如果存在再打开,否则提示文件不存在即可。
20 MsgBox "打开文件成功" 
21 
22 MsgBox f.Read(2) '表示从开始移动光标2位,即读取2个字符。
23 MsgBox f.ReadLine '表示从上次读取的光标位置开始读取一整行。
24 str = f.ReadAll '表示从文件开头开始读取数据。
25 
26 f.Close '关闭文件,可以将写入的东西存入硬盘中。
27 MsgBox "点击确定按钮显示文件内容"
28 MsgBox str
29 
30 
31 Set file = Nothing 
32 
33 Set f = Nothing 
Read与ReadLine还有ReadAll 的区别

 

3.文件夹、驱动器集合

 

 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim file,fc,f1,f
 7 
 8 Set file = CreateObject("Scripting.filesystemobject") '实例化
 9 
10 Set f = file.GetFolder("E:\python自动换运维之路\") '定义一个路径
11 Set fc = f.Files  '获取该路径下的所有文件,但是不包含目录
12 
13 For Each f1 In fc  '表示遍历这个fc变量
14     MsgBox f1.Name  '打印fc这个变量中存放的每一个文件名称,包括隐藏文件哟!
15 Next
16 
17 
18 Set file = Nothing 
19 
20 Set f = Nothing 
21 Set fc =Nothing 
Files以及For Each...Next用法展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim file,fc,f1,f
 7 
 8 Set file = CreateObject("Scripting.filesystemobject") '实例化
 9 
10 Set f = file.GetFolder("E:\python自动换运维之路\") '定义一个路径
11 Set fc = f.SubFolders  '获取该路径下的所有文件夹,但是不包含目录
12 
13 For Each f1 In fc  '表示遍历这个fc变量
14     MsgBox f1.Name  '打印fc这个变量中存放的每一个文件夹名称,包括隐藏文件夹哟!
15 Next
16 
17 
18 Set file = Nothing 
19 
20 Set f = Nothing 
21 Set fc =Nothing 
SubFolders用法展示

 

 4.FSO实例

 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Const forreading =  1
 7 Const Forwriting =  2
 8 Dim file,f,filepath,i,str
 9 Set file = CreateObject("Scripting.filesystemobject") '实例化
10 
11 filepath = "E:\python自动化运维之路\函数\decorator.txt" '定义需要问去的文件
12 Set f = file.OpenTextFile(filepath,forreading) '由于OpenTextFile只支持三种种方式打开,我们先以只读的方式打开,相当于python3中的“r”
13 
14 While f.AtEndOfStream <> True  '这里是利用“AtEndOfStream”属性判断文件是否到末尾,如果到末尾就会返回True.没有到末尾就一直循环读取每一行。
15     i = i + 1 '给变量赋初值为1,不赋值的话默认为0
16     str = str & "" & i & "行内容是>>>:" &  _
17         f.ReadLine & vbCrLf  '这里是一个换行符和一个回车符的应用,将文件的内容拼接起来。
18 Wend
19 
20 f.Close '读取文件完毕后要关闭源文件
21 MsgBox str 
22 Set f = file.OpenTextFile(filepath,Forwriting) '这是是写入的方式打开,注意打开的时候回把已经存在的文件给清空掉,相当于python3中的"w"
23 f.Write str '将读取的数据重写写入文件中
24 f.Close
25 
26 MsgBox "写入成功"
27 
28 
29 Set file = Nothing 
30 Set f = Nothing
31 Set filepath = Nothing   
32 Set str = Nothing 
Forwriting以及AtEndOfStream 的用法展示(添加行号)
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Const forreading =  1
 7 Const Forwriting =  2
 8 Const forappending = 8
 9 Dim file,f,filepath,i,str
10 Set file = CreateObject("Scripting.filesystemobject") '实例化
11 
12 filepath = "E:\python自动化运维之路\函数\decorator.txt" '定义需要问去的文件
13 Set f = file.OpenTextFile(filepath,forreading) '由于OpenTextFile只支持三种种方式打开,我们先以只读的方式打开,相当于python3中的“r”
14 
15 While f.AtEndOfStream <> True  '这里是利用“AtEndOfStream”属性判断文件是否到末尾,如果到末尾就会返回True.没有到末尾就一直循环读取每一行。
16     i = i + 1 '给变量赋初值为1,不赋值的话默认为0
17     str = str & "" & i & "行内容是>>>:" &  _
18         f.ReadLine & vbCrLf  '这里是一个换行符和一个回车符的应用,将文件的内容拼接起来。
19 Wend
20 
21 f.Close '读取文件完毕后要关闭源文件
22 MsgBox str 
23 Set f = file.OpenTextFile(filepath,forappending) '这是以追加的方式打开文件,相当于python3中的“a”
24 f.Write str '将读取的数据重写写入文件中
25 f.Close
26 
27 MsgBox "写入成功"
28 
29 
30 Set file = Nothing 
31 Set f = Nothing
32 Set filepath = Nothing   
33 Set str = Nothing 
forappending用法展示
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Const hidden = 2 'const是设置属性的一个参数,hidden=2 表示隐藏属性,具体的可以参考以下解释哟
 7 '常数             值                 描述 
 8 'Normal         0             普通文件。不设置属性。 
 9 'ReadOnly         1             只读文件。属性为读/写。 
10 'Hidden         2             隐藏文件。属性为读/写。 
11 'System         4             系统文件。属性为读/写。 
12 'Volume         8             磁盘驱动器卷标。属性为只读。 
13 'Directory         16             文件夹或目录。属性为只读。 
14 'Archive         32             文件在上次备份后已经修改。属性为读/写。 
15 'Alias             64             链接或者快捷方式。属性为只读。 
16 'Compressed     128         压缩文件。属性为只读。 
17 
18 
19 Dim file,f,filepath,i,str
20 Set file = CreateObject("Scripting.filesystemobject") '实例化
21 
22 filepath = "E:\python自动化运维之路\函数\decorator.txt" '定义需要问去的文件
23 
24 Set f = file.GetFile(filepath) '获取目标文件,然后方便对其进行属性设置操作
25 
26 f.Attributes = hidden
27 
28 MsgBox "成功隐藏" & filepath
29 
30 Set file = Nothing 
31 Set f = Nothing
32 Set filepath = Nothing   
33 Set str = Nothing 
Attributes隐藏一个文件案例展示

 

 

九.小试牛刀

  目前刚刚入门,勉强写了一下2个备份脚本,可惜没有查到用vbs判断ftp目录下是否存在某个路径,不然就可以将以下2个脚本的特色合二为一了。如果有哪位大神指导,欢迎在我的博客留下珍贵的脚印,感激不尽啊~哈哈~

 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 Dim file,f1,i,j,f,soucepath,backpath,flag
 6 Set file = CreateObject("Scripting.filesystemobject") 
 7 soucepath = "E:\python自动化运维之路\"  '定义源文件目录
 8 backpath = "E:\bakcup\" '定义将要非分的目录
 9 Set f = file.GetFolder(soucepath) 
10 Set f1 = file.GetFolder(backpath)
11 For Each i In f.Files  '遍历f这个目录
12    flag = 0 '定义一个标志位,用于判断是match到相应的文件.
13    For Each j In f1.Files '遍历f1这个目录
14       If i.Name = j.Name Then 
15        'MsgBox j.Name & "文件已存在"
16        flag = 1'如果在备份的牧流中存在这个变量,就讲标志位改为1
17        Exit For 'match到后退出循环
18       End If 
19    Next 
20    If flag = 0 Then '如果没有匹配到,则flag = 0,就可以执行以下的copy
21     'MsgBox i.Name & "文件不存在"
22     file.CopyFile soucepath & i.Name,backpath '没有文件就拷贝文件到目标目录中去
23    End If 
24 Next
25 
26 Set file = Nothing '释放掉内存
27 Set f = Nothing 
28 Set f1 = Nothing 
29 Set flag = Nothing 
30 Set soucepath = Nothing 
31 Set backpath = Nothing 
本地文件备份脚本
 1 Option Explicit
 2 '@author :yinzhengjie
 3 'blog:http://www.cnblogs.com/yinzhengjie/tag/VBScript/
 4 'EMAIL:y1053419035@qq.com
 5 
 6 Dim file, WSHshell,configFile,backupFiles,objFile
 7 Set file = CreateObject("Scripting.FileSystemObject") 
 8 set WSHshell=CreateObject("WScript.Shell") 
 9 Set configFile = file.opentextfile("E:\bakcup\yinzhengjie.txt",2,True) '以写的权限打开一个文件,如果文件不存在就创建(因为后面指定了“True”) 
10 configFile.WriteLine("OPEN 172.16.96.205") '需要输入ftp服务器的地址
11 configFile.WriteLine("yzj") 'ftp服务器的用户
12 configFile.WriteLine("1")  'ftp服务器的密码
13 configFile.WriteLine("binary") '表示传输文件用二进制传输。
14 configFile.WriteLine("prompt") '表示关闭交互模式,因为默认是开启交互模式的,
15 Set backupFiles = file.GetFolder("E:\bakcup\").Files   '定义一个需要非
16 For Each objFile in backupFiles '遍历objFiles目录下的所有文件,包括隐藏文件。
17 configFile.WriteLine("mput " & objFile.Path) '上传文件到服务器上去。
18 Next
19 configFile.WriteLine("quit") '与服务器断开链接的指令。
20 configFile.Close '关闭文件。
21 WSHshell.Run "ftp -s:" & "E:\bakcup\yinzhengjie.txt", 1, true  '这个是调用WSHshell.Run方法去执行cmd的命令,以此来启动链接ftp的文件。
22 Set configFile = Nothing  '释放configFile掉内存,以下同理
23 Set WSHshell = Nothing 
24 Set file = Nothing
25 
26 
27 
28 
29 '''
30 '友情提示:
31 '    1.根据以上备注信息修改相应参数;
32 '    2.需要在windows周期计划任务中添加自动执行时间。
33 '    3.由于是vbscript新手,欢迎大神指点程序需要修改的地方。百度了很久没有找到判断ftp目录是否存在某个文件的方法,后期如果哪位大神找到了请在我的博客告知我,那样的话我可以再优化一下这个脚本。
34 '    4.原链接地址:http://www.cnblogs.com/yinzhengjie/p/6597685.html
35 '''
本地文件备份到ftp服务器上的脚本

 

 

 

 

 

 

 

'    你已经看到了这里,那么VBScript你就算是入门了,其实,我这里举例子仅仅都是冰山一角,FileSystemObject的用法还有很多,其功能特别强大,感兴趣的朋友可以研究一下。想要了解更多vsscript用法请参考:http://www.cnblogs.com/yinzhengjie/p/6604253.html

 

 

 

 

 

posted @ 2017-03-22 00:27  尹正杰  阅读(2253)  评论(0编辑  收藏  举报