VBS学习日记(三) 运行外部程序

利用 Vbs 运行外部程序

Vbs 只提供了编程的一个基本框架,用户可以使用 Vbs 来定义变量、过程和函数, vbs 也提供了一些内部函数和对象,但是 Vbs 没有提供任何命令来访问 Windows 系统内部的部件,但是值得庆幸的是,Vbs 虽然不能自己完成这些任务,但是它提供了一条极为方便、功能也相当强的命令——CreateObject ,这条命令可以访问 windows 系统内安装的所有 com 对象,并且可以调用这些部件中存放的命令。

WSH 也就是用来解析 Vbs 的宿主,本身包含了几个个常用对象:
1、Scripting.FileSystemObject —> 提供一整套文件系统操作函数
2、Scripting.Dictionary —> 用来返回存放键值对的字典对象
3、Wscript.Shell —> 提供一套读取系统信息的函数,如读写注册表、查找指定文件的路径、读取 DOS 环境变量,读取链接中的设置
4、Wscript.NetWork —> 提供网络连接和远程打印机管理的函数。(其中,所有 Scripting 对象都存放在 SCRRUN.DLL 文件中,所有的 Wscript 对象都存放在 WSHOM.ocx 文件中。)

Set objShell = CreateObject("Wscript.Shell")
objShell.Run"notepad"
objShell.Run"calc"
objShell.Run"winword"
objShell.Run"""C:\Program Files (x86)\Foxit Software\Foxit Reader\Foxit Reader.exe"""
objShell.Run"gpedit.msc"
<pre name="code" class="plain">objShell.Run"taskmgr.exe"

 
说明之一:Set 是 Vbs 指令,凡是将一对象引用赋给变量,就需要使用 set 关键字。那么什么是对象引用呢?凡是字符串、数值、布尔值之外的变量都是对象引用。 Objshell 是变量名,可以随意修改。 

说明之二:凡是正确引用的对象,其本身内置有函数和变量,其引用方法为在变量后加“ . ”,后紧跟其实现功能的函数就可以了。 Objshell.run 的意思就是调用 Wscript.shell 中的运行外部程序的函数——run,notepad 是记事本程序的文件名。当然你也可以改成“ calc ”,这是计算器的文件名, winword 是 word 的文件名,等等吧,所有可执行文件的文件名都可以。但是需要注意的是 ,如果你要执行的可执行文件存放的地方不是程序安装的常用路径,一般情况下,需要提供合法的路径名,但要注意使用 8.3 格式输入 ,比如“"D:\Progra~1\Tencent\QQ.exe"” 。但是 run 在运行解析时,遇到空格会停止,解决的方法是使用双引号

网上也有使用"chr(34)"的,就在这里把所有的都保存下,以便往后要用到

chr(32)是空格;
chr(8、9、10 和 13)可以分别转换为退格符、制表符、换行符和回车符。这些字符都没有图形表示,但是对于不同的应用程序,这些字符可能会影响文本的显示效果。

ASCII码表在线查询(1.0 beta 1)

ASCII码表_全_完整版

例如:在我的机器运行 qq,代码为:
objshell.run """C:\Program Files\QQ2006\QQ.exe""" ‘注:三个引号

Set objShell = CreateObject("wscript.shell")
objShell.Run"notepad",,True
objShell.Run"calc",,true
objshell.Run"taskmgr.exe"

 在不加入",,true"时两个程序基本上同时启动了。 如果我们需要先启动 notepad 再启动 calc 将如何呢?很简单在需要顺序执行的代码后加 , , True 参数就可以了。结果是,在关闭前一个程序后,第二个程序才被打开,如此继续。 

run 函数有三个参数

object

WshShell object.

strCommand

String value indicating the command line you want to run. You must include any parameters you want to pass to the executable file.

intWindowStyle

Optional. Integer value indicating the appearance of the program's window. Note that not all programs make use of this information.

bWaitOnReturn

Optional. Boolean value indicating whether the script should wait for the program to finish executing before continuing to the next statement in your script. If set totrue, script execution halts until the program finishes, and Run returns any error code returned by the program. If set tofalse (the default), theRunmethod returns immediately after starting the program, automatically returning 0 (not to be interpreted as an error code).

第一个参数是你要执行的程序的路径;

第二个参数是窗口的形式,0 是在后台运行;1 表示正常运行;2 表示激活程序并且显示为最小化;3 表示激活程序并且显示为最大化;一共有 10 个这样的参数我只列出了 4 个最常用的;

intWindowStyleDescription
0

Hides the window and activates another window.

1

Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.

2

Activates the window and displays it as a minimized window.

3

Activates the window and displays it as a maximized window.

4

Displays a window in its most recent size and position. The active window remains active.

5

Activates the window and displays it in its current size and position.

6

Minimizes the specified window and activates the next top-level window in the Z order.

7

Displays the window as a minimized window. The active window remains active.

8

Displays the window in its current state. The active window remains active.

9

Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.

10

Sets the show-state based on the state of the program that started the application.

0 隐藏一个窗口并激活另一个窗口。
1 激活并显示窗口。如果窗口处于最小化或最大化状态,则系统将其还原到原始大小和位置。第一次显示该窗口时,应用程序应指定此标志。
2 激活窗口并将其显示为最小化窗口。
3 激活窗口并将其显示为最大化窗口。
4 按最近的窗口大小和位置显示窗口。活动窗口保持活动状态。
5 激活窗口并按当前的大小和位置显示它。
6 最小化指定的窗口,并按照 Z 顺序激活下一个顶部窗口。
7 将窗口显示为最小化窗口。活动窗口保持活动状态。
8 将窗口显示为当前状态。活动窗口保持活动状态。
9 激活并显示窗口。如果窗口处于最小化或最大化状态,则系统将其还原到原始大小和位置。还原最小化窗口时,应用程序应指定此标志。
10 根据启动应用程序的程序状态来设置显示状态。

第三个参数是表示这个脚本是等待还是继续执行,如果设为了 true, 脚本就会等待调用的程序退出后再向后执行。其实, run 做为函数,前面还有一个接受返回值的变量,一般来说如果返回为 0,表示成功执行 ,如果不为 0,则这个返回值就是错误代码,可以通过这个代码找出相应的错误。

vbs中run方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值