VBScript (vbs) script example

Welcome friends' comments✨✨


foreword

This article will bring you an introduction to VBScript and code examples.


1. What is vbs?

VBS is a scripting language based on Visual Basic. The full name of VBS is: Microsoft Visual Basic Script Edition. (Microsoft Visual BASIC Script Edition).
VBS is built-in in the system, and scripts written with it do not need to be compiled into binary files, and the source code is directly interpreted and executed by the Windows system host host.

2. Simple use of VBScript examples

2.1, msgbox example

Open your "Notepad" or directly create a "text document" on the desktop, enter the following code in it, press Ctrl+S shortcut key to save, and then modify the suffix to ".vbs".

msgbox "Hello vbs"

Effect icon
insert image description here

2.2. VBScript variables

Variable declarations in VBScript are not actually necessary.
This is fine, you can use the new variable everywhere without prior declaration. There is no requirement that you declare variables beforehand. Whenever VBScript encounters a new undeclared variable in the script, it allocates memory for it.
Examples are as follows:

lngFirst = 1
lngSecond = 2
lngThird = lngFirst + lngSecond
MsgBox lngThird

Effect icon
insert image description here

The Option Explicit statement is needed when variables are used too much and it is not easy to check the variables.
All you have to do is put the Option Explicit statement at the beginning of the script, before all other statements. This tells VBScript that your code requires all variables to be explicitly declared before they can be used. Now VBScript will no longer allow you to introduce new variables without declaring them in your code.
Examples of not using the Option Explicit statement are as follows:

lngFirst = 1
lngSecond = 2
lngThird = lngFirst + cond
MsgBox lngThird

Effect diagram
insert image description here
At this time, since cond has not been assigned and initialized, the value is 0, so 1 + 0 = 1. Therefore, in order to avoid unreasonable usage of variables, we add the Option Explicit statement to reasonably require all variables to be cleared before use. Must be explicitly declared first.
An example of using the Option Explicit statement is as follows:

Option Explicit
lngFirst = 1
lngSecond = 2
lngThird = lngFirst + cond
MsgBox lngThird

Effect diagram
insert image description here
When writing a new script file, the first thing to do is to enter Option Explicit at the beginning of the file and press Enter. This avoids annoying code typing errors.

3. VBScript output text and keyboard keys to cmd

3.1, cmd's FOR loop

In the cmd window: for %I in (command1) do command2
Examples are as follows:

for /L %a in (0,1,4) do echo Hello cmd!

Instruction analysis :
specific to the previous piece of code, if it is (0,1,4), it starts from 0, increases by 1 each time, and ends at 4.

Effect icon
insert image description here

In a batch file: for %%I in (command1) do command2

@echo off
for %%I in (A ,B ,C) do echo %%I
pause

Create a new document xx.txt, copy the above code into it and change the suffix name to bat, double-click to run, as shown in the figure
insert image description here
below
insert image description here

3.2. The ping command of cmd

Commonly used loop ping packet commands are as follows

ping 127.0.0.1 -t -l 65500

The instructions are explained in detail as shown in the figure below
insert image description here

3.1, VBScript batch output to cmd command code example

Option Explicit  'vbs注释格式,Option Explicit 语句 使所有的变量在使用之前都必须先显式地声明
Dim objShell     '声明一个变量存储对象
Dim fs           '声明一个变量存储对象
Dim ts           '声明一个变量存储对象
Dim line         '声明一个变量存储文本
Set objShell = CreateObject("Wscript.Shell") '得到对象objShell ,即Wscript 是一个对象
'我们首先了解下对象Wscript : Windows Script Host object model 
'简单的说,Wscript就是Windows脚本宿主对象模型(WSHCOM),而Wscript就是Windows内对象存放的一个根对象
'利用它可以创造两种COM对象:WshShell与WshNetwork 。
objShell.Run "cmd.exe",1,False   '运行cmd,窗口 false为可见窗口,true为隐藏窗口
WScript.Sleep 50 '延时50毫秒
set fs = CreateObject("scripting.filesystemobject") '打开并读文件
set ts = fs.opentextfile("loop_ip.txt",1,true) '第二个参数 1 表示只读打开,第三个参数表示目标文件不存在时是否创建
WScript.Sleep 50  '延时50毫秒
objShell.SendKeys "for /l " '输出文本
objShell.SendKeys "{%}"     '输出按键
objShell.SendKeys "a in "   '输出文本
objShell.SendKeys "{(}"     '输出按键
objShell.SendKeys "1,1,"    '输出文本
line=ts.readline            '读取loop_ip.txt中的第一行,这一行的数据表示循环次数
objShell.SendKeys line      '输出文本
objShell.SendKeys "{)}"     '输出按键
objShell.SendKeys " do start cmd /k "  '输出文本
objShell.SendKeys """"       '输出文本
objShell.SendKeys " ping "   '输出文本
line=ts.readline             '读取loop_ip.txt中的第二行,这一行的数据表示ping包的IP地址
objShell.SendKeys line       '输出文本
objShell.SendKeys " -t -l 65500"  '输出文本
objShell.SendKeys """"       '输出文本
objShell.SendKeys "{ENTER}"  '输出按键
'最终输出到指令到cmd : for /l %a in (1,1,10) do start cmd /k " ping 127.0.0.1 -t -l 65500"

Create a new document xx.txt, copy the above code into it and change the suffix name to vbs, and create a new loop_ip.txt, write the number of loops (10) in the first line, write the IP (127.0.0.1) in the second line, and double-click to run ,As shown below

insert image description here

loop_ip.txt icon
insert image description here
start_up.vbs icon
insert image description here

Effect icon
insert image description here

Four. Summary

VBScript as a tool can be used in many ways and in many different applications.
VBS is a scripting language based on Visual Basic. The full name of VBS is: Microsoft Visual Basic Script Edition. Its interpreter is Windows Script Host, referred to as VBScript, and the suffix is ​​.vbs.

Guess you like

Origin blog.csdn.net/weixin_44759598/article/details/128507328