VBS脚本中常用的自定义函数


On Error Resume Next
'---------------------------------------------------
'对象定义
Dim pCmd,pFso
Set pCmd=CreateObject("Wscript.Shell")
Set pFso = CreateObject("Scripting.FileSystemObject")

'---------------------------------------------------
'配置脚本执行引擎
RunScriptEngine = "WScript"
IF Instr(1, RunScriptEngine, "WScript", 1) > 0 Then
RunScriptEngine = "wscript.exe"
Else
RunScriptEngine = "cscript.exe"
End IF

'---------------------------------------------------
'引擎判断
If Lcase(Right(Wscript.FullName,11))<>RunScriptEngine Then
pCmd.Run("%ComSpec% /c " & RunScriptEngine & " //Nologo //B " & Wscript.ScriptFullName)
Wscript.Quit
End IF

'---------------------------------------------------
'函数定义


Sub LRApp(pAppPath, pSleepTime)
'Run An Application
PushDir = LGetPath(pAppPath)
RunFile = LGetFile(pAppPath)
pCmd.Run("%ComSpec% /c PUSHD "&Chr(34)&PushDir&Chr(34)&Chr(38)&"Start "&RunFile)
If pSleepTime <> 0 Then WScript.Sleep pSleepTime
End Sub

Sub LRVbs(pVbsPath, pSleepTime)
'Run Vbs
pCmd.Run("%ComSpec% /c " & RunScriptEngine & " //Nologo //B " & pVbsPath)
If pSleepTime <> 0 Then WScript.Sleep pSleepTime
End Sub

Sub LKill(pProcess)
'tskill
pCmd.Run("%ComSpec% /c tskill " & pProcess)
End Sub

Sub LAApp(AppTitle, pSleepTime)
'AppActivate
pCmd.AppActivate AppTitle
If pSleepTime <> 0 Then WScript.Sleep pSleepTime
End Sub

Sub LKey(pKey, pSleepTime)
'SendKeys
pCmd.SendKeys pKey
If pSleepTime <> 0 Then WScript.Sleep pSleepTime
End Sub

Sub LSLP(pTime)
'Sleep
WScript.Sleep pTime
End Sub

'文件、文件夹相关
Function LRFS(pFile)
'ReadFileStream
Set pFs = pFso.GetFile(pFile)
Set pTs = pFs.OpenAsTextStream(1)
LRFS = pTs.ReadAll
pTs.Close
End Function

Function LWFS(pFile, pStr, pType)
'WriteStringToFile
Set pFs = pFso.OpenTextFile(pFile, pType, True)
pFs.Write pStr
pFs.Close
End Function


Function LGetPath(pFile)
iLastSepPos = InstrRev(pFile, "", -1, 1)
If iLastSepPos = -1 Then
  LGetPath=""
  Exit Function
Else
  LGetPath = Left(pFile, iLastSepPos-1)
End If
End Function

Function LGetFile(pFile)
iLastSepPos = InstrRev(pFile, "", -1, 1)
If iLastSepPos = -1 Then
  LGetFile=""
  Exit Function
Else
  LGetFile = Right(pFile, Len(pFile)-iLastSepPos)
End If
End Function

'选择提示框相关函数
Function LYesOrNo(pMsg)
iYesOrNo = MsgBox(pMsg, 4, "温馨提示!")
IF iYesOrNo = 6 Then
  bYesOrNo = True
Else
  bYesOrNo = False
End IF
LYesOrNo = bYesOrNo
End Function

Function LSureOrCancel(pMsg)
iSureOrCancel = MsgBox(pMsg, 4, "温馨提示!")
IF iSureOrCancel = 6 Then
  bSureOrCancel = True
Else
  bSureOrCancel = False
End IF
LSureOrCancel = bSureOrCancel
End Function

'---------------------------------------------------

文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: VBS
相关日志:
评论: 0 | 引用: 0 | 查看次数: 790
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 20 字 | UBB代码 关闭 | [img]标签 关闭