常用VBS代码有哪些
常用VBS代码有哪些
Visual Basic Scripting(简称VBS)是一种基于Visual Basic的脚本语言,通常用来创建Windows平台下的自动化任务和脚本程序。在这篇文章中,我们将介绍一些常用的VBS代码,涵盖文件操作、系统管理、网络操作等方面。
文件操作
1. 创建文件:
Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.CreateTextFile("C:\path\to\file.txt", True) objFile.Close
2. 读取文件内容:
Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("C:\path\to\file.txt", 1) strContent = objFile.ReadAll objFile.Close
3. 写入文件内容:
Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("C:\path\to\file.txt", 2, True) objFile.Write "Hello, World!" objFile.Close
系统管理
1. 运行命令行:
Set objShell = WScript.CreateObject("WScript.Shell") objShell.Run "cmd.exe /c echo Hello, World!"
2. 获取当前用户名:
Set objNetwork = CreateObject("WScript.Network") strUserName = objNetwork.UserName WScript.Echo "当前用户名:" & strUserName
3. 获取计算机名称:
Set objNetwork = CreateObject("WScript.Network") strComputerName = objNetwork.ComputerName WScript.Echo "计算机名称:" & strComputerName
网络操作
1. 发送HTTP请求:
Set objHTTP = CreateObject("Microsoft.XMLHTTP") objHTTP.Open "GET", "http://www.example.com/api/data", False objHTTP.Send strResponse = objHTTP.responseText WScript.Echo strResponse
2. 解析URL:
Set objURL = CreateObject("Scripting.Dictionary") strURL = "http://www.example.com/pageparam1=value1¶m2=value2" objURL.CompareMode = vbTextCompare arrParams = Split(Mid(strURL, InStr(strURL, "") + 1), "&") For Each param In arrParams arrParam = Split(param, "=") If Not objURL.Exists(arrParam(0)) Then objURL.Add arrParam(0), arrParam(1) End If Next WScript.Echo "参数1的值:" & objURL("param1")
以上是一些常用的VBS代码示例,涵盖了文件操作、系统管理和网络操作等方面。希望本文能对您有所帮助。
信息由用户投稿以及用户自行发布,真实性、合法性由发布人负责,涉及到汇款等个人财产或隐私内容时请仔细甄别,注意防骗!如有侵权,请联系:wwwlaoyuwang#126.com(#=@)!我们会第一时间核实处理!