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

有时候我们需要用.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 



 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值