---
title: "VBScript 常用语法一览"
author: "TimerBin"
date: "2014-12-26T16:59:38Z"
tags: []
categories: ["编程语言"]
slug: "vbscript"
draft: false
lastmod: "2014-12-26T16:59:38Z"
id: 2170422
url: "https://timerbin.iteye.com/blog/2170422"
---

VBScript  (Microsoft Visual Basic Scripting Edition--VBS) ，[微软公司](http://baike.baidu.com/view/39784.htm)可视化BASIC[脚本](http://baike.baidu.com/view/54.htm)版，在window中随处可见，文件后缀名为.vbs。下面来看下它的常用语法：

运行文件demo.vbs文件如下所示：

```
'定义脚本类型
set sh=WScript.CreateObject("WScript.Shell")
'调用子脚本信息信息 后面跟上所传输参数的数组
sh.Run "demoSun.vbs  1234"
```

 运行子文件demoSun.vbs请与demo.vbs放于同一目录下面，内容入下所示：

 

```
'定义脚本类型
set sh=WScript.CreateObject("WScript.Shell")
'定义参数
Dim password
'接收上一级传输过来的参数 数组
set password = WScript.Arguments
Dim inputPsd
'定义弹出框来接收你输入的参数,提示,title,默认值
inputPsd = inputbox("Please input your passwrod","Input passwrod","1234")
'弹出输入内容
msgbox(inputPsd)
'判断内容inputPsd是否输入为空
if inputPsd<>"" then
	if inputPsd=password(0) then
		 '等待3秒 
		 WScript.Sleep 3000
		 '相当于indexOf 判断inputPsd中是否包含1字符串 获取1字符串的下标
		 if Instr(inputPsd,"1") >= 1 then
			 msgbox("your passwrod have 1")
		 end if
		 '弹框提示
		 msgbox("your passwrod is success")
		 '将内容输入到光标处
		 sh.SendKeys "OK"
	 end if
else 
	'等待3秒 
	 WScript.Sleep 3000
	 '弹框提示
	 msgbox("Please input your passwrod")
end if
```

 其中弹框的样子如下图所示：

![](http://dl2.iteye.com/upload/attachment/0104/6696/9dd33b8b-21de-348c-b76a-de51d8a9a33a.jpg)  
 

在编写过程中经常被空格问题被坑，希望大家注意。其中的注释符号为'。

其中的循环代码for.vbs例子文本如下所示：

```
'定义脚本类型
set sh=WScript.CreateObject("WScript.Shell")
Dim num
'定义弹出框来接收你输入的参数,提示,title,默认值
num = inputbox("Please input max NUM","Input NUM","3")
'结果 1 2 3
for i=1 to num
 msgbox i
next
'step i参数每次增加的值 结果1 3
for i=1 to num Step 2
 msgbox i
next
```

 如果想进一步学习的可详见：[http://www.cnblogs.com/sirrah/articles/2349029.html](http://www.cnblogs.com/sirrah/articles/2349029.html)