fShellRun

Author Message
DiGiTAL.SkReAM
  • Total Posts : 1291
  • Scores: 8
  • Reward points : 0
  • Joined: 9/7/2005
  • Location: Clearwater, FL, USA
fShellRun - Tuesday, February 06, 2007 3:03 AM
0
I use this function a lot in my scripts, to capture the output/results of a dos command.  It captures the output of a dos command and returns it in a string.
 
 wscript.echo fShellRun("%comspec% /c dir c:\")
  
  
  
 Function fShellRun(sCommandStringToExecute)
 ' This function will accept a string as a DOS command to execute.
 ' It will then execute the command in a shell, and capture the output into a file.
 ' That file is then read in and its contents are returned as the value the function returns.
  Dim oShellObject, oFileSystemObject, sShellRndTmpFile
  Dim oShellOutputFileToRead, iErr
  Set oShellObject = CreateObject("Wscript.Shell")
  Set oFileSystemObject = CreateObject("Scripting.FileSystemObject")
  sShellRndTmpFile = oShellObject.ExpandEnvironmentStrings("%temp%") & oFileSystemObject.GetTempName
  On Error Resume Next 
   oShellObject.Run sCommandStringToExecute & " > " & sShellRndTmpFile, 0, True
   iErr = Err.Number
  On Error GoTo 0
   If iErr <> 0 Then 
    fShellRun = ""
    Exit Function 
   End If 
  fShellRun = oFileSystemObject.OpenTextFile(sShellRndTmpFile,1).ReadAll
  oFileSystemObject.DeleteFile sShellRndTmpFile, True 
 End Function
 

 
"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury