常用vbs代码[通俗易懂]

2022-08-31 17:14:45 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

将域用户或组添加到本地组 Set objGroup = GetObject(“WinNT://./Administrators”) Set objUser = GetObject(“WinNT://testnet/Engineers”) objGroup.Add(objUser.ADsPath)

修改本地管理员密码 Set objcnlar = GetObject(“WinNT://./administrator, user”) objcnla.SetPassword “P@ssW0rd” objcnla.SetInfo

弹出 YES or NO 的对话框,不同的选择执行不同的代码 intAnswer = Msgbox(“Do you want to delete these files?”, vbYesNo, “Delete Files”) If intAnswer = vbYes Then Msgbox “You answered yes.” Else Msgbox “You answered no.” End If

运行CMD命令行命令 set obshell=wscript.createobject(“wscript.shell”) obshell.run (“ipconfig”),,true 如果要运行的命令中包含双引号,可使用&chr(34)&代替

忽略代码错误继续执行 On Error Resume Next 放置于代码的最开头,当代码运行出错后并不停止跳出而是继续执行下一条。适当应用会很有效果。

注册表的修改,读取,删除,创建 Set wso = CreateObject(“WScript.Shell”) ‘声明 wso.RegWrite “%Path%”‘创建子键 wso.RegWrite “%Path%”,”%Value%”‘修改”默认”键值 wso.RegWrite “%Path%”,%Value%,%RegType% ‘修改特定类型的键值 ‘(字符串值 REG_SZ 可扩充字符串值 REG_EXPAND_SZ DWORD值 REG_DWORD 二进制值 REG_BINARY)

Set WSHShell= Wscript.CreateObject(“Wscript.Shell”) WSHShell.RegRead (%Path%) ‘读取注册表子键或键值(一般用于判断某一事件是否执行)

Set wso = CreateObject(“WScript.Shell”) wso.RegDelete “%Path%” ‘删除子键或键值 ‘(根键缩写HKEY_CLASSES_ROOT HKCR HKEY_CURRENT_USER HKCU HKEY_LOCAL_MACHINE HKLM,其余无)

eg: Set wso = CreateObject(“Wscript.Shell”) wso.RegWrite “HKLM/SOFTWARE/Microsft/Windows NT/#1” wso.RegWrite “HKLM/SOFTWARE/Microsft/Windows NT/#1″,”0” wso.RegWrite “HKLM/SOFTWARE/Microsft/Windows NT/#1/#2”,0,REG_BINARY wso.RegDelete “HKLM/SOFTWARE/Microsft/Windows NT/#1” Wscript.quit

文件的复制,删除,创建,简单的写入 Set fso = Wscript.CreateObject(“Scripting.FileSystemObject”) ‘声明 Set f = fso.CreateTextFile(“%PATH%”) ‘创建文件,其中f可任意,包含缩略名 f.WriteLine(“VBS”) ‘写文件内容,该命令功能太简单,目前看来只能用于TXT文件 f.Close set c=fso.getfile(“%path%”) ’拷贝某文件 c.copy(“%PATH2%”) ‘拷贝文件到指定地点 fso.deletefile(“%PATH%”) ‘删除文件 Wscript.quit

eg. Set fso = Wscript.CreateObject(“Scripting.FileSystemObject”) Set f=fso.CreateTextFile(“C:/Sample.txt”) WriteLine(“VBS”) f.close set e=fso.getfile(C:/Sample.txt) e.copy(“D:/Sample.txt”) fso.deletefile(C:/Sample.txt) Wscript.quit

向应用程序输出简单的连串指令 dim program1 ‘声明变量program1 program1= “%Path%” ‘应用程序路径 set wshshell=createobject(“wscript.shell”) ‘声明饮用函数 set oexec=wshshell.exec(program1) ‘运行程序 wscript.sleep 2000 ‘(该行命令未知作用.估计是设定延迟,请高手指点) wshshell.appactivate “%WindowsName%” ‘激活运用程序窗口 wshshell.sendkeys “ {%KeyBoardName%}” ‘第一次输出键盘按键指令前要加 wshshell.sendkeys “555555” ‘在程序输入栏中输入运用该系列命令须首先确定程序可以实施连串的键盘操作,这在QQ登录中最适用,如下例。

eg. dim program1 program1=”D:/Program Files/Tencent/coralQQ.exe” set wshshell=CreateObject(“wscript.shell”) set oexec=wshshell.exec(program1) wscript.sleep 2000 wshshell.appactivate “QQ登录” wshshell.sendkeys “ {TAB}” wshshell.sendkeys “250481892” wscript.sleep 2000 wshshell.sendkeys “{TAB}” wshshell.sendkeys “****************” wscript.sleep 2000 wshshell.sendkeys “{ENTER}” Wscript.quit

文件夹的简单操作 Set fso = Wscript.CreateObject(“Scripting.FileSystemObject”) ‘声明 Set f = fso.CreateFolder(“%PATH%”) 创建文件夹 Set e = getFolder(%PATH%) 类似于“绑定目标” e.copy(“%PATH2%”) 复制文件夹 fso.deletefolder(%PATH%) 删除文件夹

eg. Set fso = Wscript.CreateObject(“Scripting.FileSystemObject”) Set f = fso.CreateObject(“C:/sample”) f.copy(“D:/sample”) fso.deletefolder(“C:/sample”)

‘(由上例可以看出,文件夹的操作很多是和文件的操作相通的,因此VBS文件具有很多命令的统一性)

将某一指定文件夹的所有只读文件转为可读文件 Const ReadOnly = 1 ‘设只读属性对应值为1

Set FSO = CreateObject(“Scripting.FileSystemObject”) ‘声明 Set Folder = FSO.GetFolder(“%PATH%”) ’绑定文件夹 Set colFiles = Folder.Files ‘文件夹所有文件

For Each objFile in colFiles ’下列语句应用于文件夹所有文件 If File.Attributes AND ReadOnly Then ‘这是关键之处,这里应用了If判断语句,来检测文件属性是否为只读 File.Attributes = File.Attributes XOR ReadOnly ‘对判断结果为Ture(默认为True)’执行XOR逻辑运算,将其改为可读 End If ‘结束判断 Next

将Word文件另存为文本文件 Const wdFormatText = 2 ’设置常数值 (当该值为8时另存为HTML文档,为11时另存为XML文档) Set objWord = CreateObject(“Word.Application”) ‘申明调用函数 Set objDoc = objWord.Documents.Open(“%Path%”) ‘打开某DOC文件 objDoc.SaveAs “%PATH2%”, wdFormatText 另存为…… objWord.Quit

eg: Const wdFormatText = 2 Set objWord = CreateObject(“Word.Application”) Set objDoc = objWord.Documents.Open(“d:/doc1.doc”) objDoc.SaveAs “g:/doc1.txt”, wdFormatText objWord.Quit

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/143015.html原文链接:https://javaforall.cn

0 人点赞