用VBS来代替BAT或CMD文件进行命令第1/2页

2019-01-16 06:43:14刘景俊

Set Files=objFolder.Files
For Each File In Files
If Not InArray(LIstArr,LCase(File.name)) Then
On Error Resume Next
File.Delete(True)
If Err Then
err.Clear
Msgbox "不能删除文件,请检查 "&File,16,"错误"
Else 
flnum = flnum + 1
End If
On Error GoTo 0
End If
Next
End Sub

Function CheckLine(strLine)
Dim LineRegExp,Matches
Set LineRegExp = New RegExp
LineRegExp.Pattern = ".=."
LineRegExp.Global = True
Set Matches = LineRegExp.Execute(strLine)
CheckLine = Matches.count
End Function

Function InArray(Myarray,StrIn)
Dim StrTemp
InArray = True
For Each StrTemp In Myarray
If StrIn = StrTemp Then
Exit Function
Exit For
End If
Next
InArray = False
End Function

!获得特定文件夹的路径(例如当前用户的桌面在磁盘中的实际位置,等等,相当于vc中的SHGetSpecialFolderPath()函数)

Set wsShell = CreateObject("WScript.Shell")
DesktopPath = wsShell.SpecialFolders("Desktop")


!获取当前用户名称
Set WshNetwork = WScript.CreateObject("WScript.Network")

UserName= WshNetwork.UserName


!获取系统变量%SystemRoot%(当然其他的系统变量可以类推,只是不只是不是都要通过PROCESS中转一下)

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshSysEnv = wsShell.Environment("PROCESS") 
SystemRoot = WshSysEnv("WINDIR") 
!将域用户或租添加到本地组

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
放置于代码的最开头,当代码运行出错后并不停止跳出而是继续执行下一条。适当应用会很有效果。