您的位置:首页 > 运维架构 > Shell

【VBScript Shell】使用vbs shell调用命令行exe的示例代码

2013-11-15 23:52 471 查看
有时候我们需要用.bat或其他脚本快速灵活的实现对其他exe的调用,实现一些数据转换、搬迁等批处理工作,在windows上vbs shell是一种不错的选择,不仅灵活而且功能强大。

本文给出了一些自己封装的用于调用exe的vbs函数。

 

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The function used to print log									            '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function PrintLog(strLog)
strMsg = Date() & " " & Time() & " " & strLog
logFile.WriteLine strMsg
End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The function used to check whether the given file exists.			            '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function CheckFileExists(strFilePath)
set oFSO = createObject("Scripting.FileSystemObject")
boolFileExists = oFSO.fileExists(strFilePath)
set oFSO = Nothing

CheckFileExists = boolFileExists
End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The function used to delete file.									            '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function DeleteTempFile(strFilePath)
set oFSO = createObject("Scripting.FileSystemObject")

If oFSO.FileExists(strFilePath) Then
oFSO.DeleteFile strFilePath, True
End If

set oFSO = Nothing
End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' To read the input parameters, return an array						            '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ReadInputParam(strFilePath)
Dim retArray
retArray = Array(50, 3)

set oFSO = createObject("Scripting.FileSystemObject")

If oFSO.FileExists(strFilePath) Then
Set textStream = oFSO.OpenTextFile(strFilePath)

Dim i
i = 0
Do While Not textStream.AtEndOfStream
inputLine = ucase(trim(textStream.readline))

tempArray = Split(inputLine, " ", -1, 1)
If UBound(tempArray) = 2 Then
retArray(i) = tempArray
i = i + 1
End If
Loop

Set textStream = Nothing

End If

set oFSO = Nothing

ReadInputParam = retArray
End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The GetSortedFiles function get a filename list of a specified directory  	'
' ordered by ascending filename             									'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GetSortedFiles(strPath)
Dim rs
Dim folder
Dim File
Const adVarChar = 200
Set fso = CreateObject("Scripting.FileSystemObject")
Set rs = CreateObject("ADODB.Recordset")
Set folder = fso.GetFolder(strPath)
Set FSO = nothing

rs.fields.append "FileName", adVarChar, 255
rs.fields.append "OrderBy", adVarChar, 255
rs.Open

For Each File In folder.Files
If InStr(1, File.Name, ".edb", 1) <> 0 And InStr(1, UCase(File.Name), "TIC", 1) = 1 Then
rs.AddNew
rs("FileName") = File.Name
strNameWithoutExt = Left(UCase(File.Name), Len(File.Name) - 4)
strArray = Split(strNameWithoutExt, "_", -1, 1)
strYMD = strArray(0)

If UBound(strArray) = 0 Then
strNO = "00"
Else
strNO = strArray(1)
If Len(strNO) = 1 Then
strNO = "0" & strNO
End If
End If

strOrderBy = strYMD & "_" & strNO

rs("OrderBy") = strOrderBy

rs.Update
End If
Next

'Sort by acending file name
rs.Sort = "OrderBy ASC"

rs.MoveFirst
Set folder = Nothing
Set GetSortedFiles = rs
End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The RunCommandlineTool function execute the specified command-line exe,   	'
' and redirect the command-line print messages to the given log file,  			'
' then analyze the log file to determine whether there are some errors.			'
'																				'
' PARAMETERS:																	'
'	@strCommand: Specify the command string,									'
' 	@strRedirectLogFilenamePath: Specify the log file,							'
' RETURN:																		'
'	True : process succeed without any error,     								'
'	False: process complete with some errors     								'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function RunCommandlineTool(strCommand, strRedirectLogFilenamePath)
PrintLog """" & strCommand & """" & " Start!"

Set WsShell = CreateObject("Wscript.Shell")
Dim strShellCommand
Dim processNoError

strShellCommand = "%comspec% /c " & Chr(34) & strCommand & " >> " & strRedirectLogFilenamePath & Chr(34)
WsShell.Run strShellCommand, 0, True

'Parse the log to determine whether there are some errors
processNoError = True
WScript.Sleep 200

Set fso1 = createobject("scripting.filesystemobject")
Set textStream = fso1.OpenTextFile(strRedirectLogFilenamePath, 1)

Do While Not textStream.AtEndOfStream
logLine = ucase(trim(textStream.readline))
i=i + 1

If InStr(logLine, "FAILED") Then
PrintLog(logLine)
processNoError = false
Exit Do
End If
Loop

Set textStream = Nothing
Set fso1 = Nothing

Dim strProcEndMsg
If processNoError = True Then
strProcEndMsg = " Done Successfully"
Else
strProcEndMsg = "Complete with Some Errors!"
End If

strProcEndMsg = """" + strCommand + """ " + strProcEndMsg

PrintLog strProcEndMsg

RunCommandlineTool = processNoError
End Function


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐