Some VBS functions
来源:互联网 发布:南方科技大学知乎 编辑:程序博客网 时间:2024/05/17 00:41
好久没有写VBS代码了,很多最基本的我都忘掉了,今天借助于复习QTP的机会把VBS脚本重新复习了一遍,为了方便理解,我把所有的代码都添加了注释。在为 Active Server Pages、Windows Scripting Host、或其他可以使用 scripting 的应用程序写脚本时,在 Web 服务器上添加、移动、改变、创建或删除文件夹(目录)和文件通常是很重要的。获得有关信息和操作连接在 Web 服务器上的驱动器也可能是必要的。这里我就使用了windows scripting和FilesystemObject两个对象,其余的还包括很多,可以参考VBS用户手册。
'使用CreateObject对象来创建FilesystemObject对象,其中Script是类型库的名字,FilesystemObject是想要创建的对象的名字
Dim shell
set shell=CreateObject("WScript.shell")
'shell.run "cmd " '打开命令提示符
'shell.run "C:\test.VBS" ’打开VBS脚本程序
'shell.run "TheWorld.exe http://blog.sina.com.cn/u/1896911462" '用世界之窗浏览器打开我的新浪博客主页
'shell.run "c:\windows\system32\notepad" '打开系统的记事本程序
打开系统组件比较复杂一点,需要找到组件的ID才能打开,这个也可以在网上搜到一箩筐的,我只列举几个:
'shell.run "explorer.exe::{21EC2020-3AEA-1069-A2DD-08002B30309D}"
'我的电脑:'shell.run "explorer.exe ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
'我的文档: 'shell.run "explorer.exe ::{450D8FBA-AD25-11D0-98A8-0800361B1103}"
'控制面板: 'shell.run "explorer.exe::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
'回收站: 'shell.run "explorer.exe::{645FF040-5081-101B-9F08-00AA002F954E}"
接下来这个才是一个小的VBS程序,没啥用处,只是好玩而已。
'显示系统盘的信息
dim panfu
panfu = inputbox("Please input the Driver Name",1)
'显示一个输入对话框,输入驱动器的名称,可以是c或C:或C:\格式的,但是不能出现空格,因为里面没有验证机制,程序会崩溃的
MsgBox ("输入: " & panfu) ‘显示出所输入的信息
Set fso = CreateObject("Scripting.FileSystemObject") ’创建一个FilesystemObject对象
fso.GetDrive(panfu) ‘得到盘符信息
Dim fso, drv, s,p
' Set drv = fso.GetDrive(fso.GetDriveName(panfu)) ’这是另外一种方式
Set drv = fso.GetDrive(panfu) ‘得到盘符信息
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True) ‘在C盘下写入记事本文件中
p = "Drive " & UCase(panfu) & " - " '输入盘符的名称,方便在文本文件中显示出,故专门定义了一个新的变量
s = p & drv.VolumeName
s = s & "Total Space: " & FormatNumber(drv.TotalSize / 1024, 0) '盘的容量,这个可以调用磁盘对象方法实现,参看手册。
s = s & " Kb" ‘添加单位,上一部单位已经实现了K的换算,如果你感兴趣,可以继续换算成M或者G或者T等等
MyFile.WriteLine(s) ‘写入文件中
myfile.WriteblankLines(0) '换行,0表示没有空行,1表示空一行,以此类推
s = p&"Free Space: " & FormatNumber(drv.FreeSpace / 1024, 0) ’盘的空余容量,同上
s = s & " Kb"
MyFile.WriteLine(s)
MyFile.Close ‘关闭文件
MsgBox("done") ’消息提示,完成
shell.run "c:\windows\system32\notepad c:\testfile.txt" '用记事本打开文件看看结果。
需要注意的是在这里使用了Wscript对象,就是刚开始定义的Shell,而不是文件对象。
- Some VBS functions
- some functions
- some functions in assembly
- Some basic socket functions
- Some functions about time
- Some Hashing Functions
- Some Useful VBA Functions
- some useful functions of c++
- [Javascript] Some functions for loading related scripts
- Some functions and codes about date
- Taylor series expansion of some functions
- some functions about judge the String type
- Unix Network Programming(8)---Some Important Functions
- OpenCV中文文档——Some Functions
- [Javascript] Some very simple functions for Word and Excel handling
- Some tips about Abstract base classes and pure virtual functions
- Some useful functions from Oracle database 11g
- Some R functions while playing with the table
- Android虚拟机(DVM)内存分配——内存溢出问题
- VBS病毒感染和搜索的原理
- LinkedList API 受用手册
- Android 退出动画下广告不出错详解
- VBS与正则表达式
- Some VBS functions
- 如何判断是否安装了VC RUNTIME
- Android 程序真退出与假退出后台运行详解
- VC++Redistributable的安装判断
- Oracle EBS Instance冷备份的方法(Cold Backup)
- 浏览器的兼容性问题
- 标准Windows控件的常规测试方法及VBS脚本代码
- javascript的本地操作
- F#互操作中的一招