WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DOS Console UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Run Console Command UDF



; UDF Name: RunCmd
; Compatibility: Windows XP (probably also works on NT and 2K)
; Author: Les Ferch
; Date: November 15, 2006
; Syntax: RunCmd('console-command with parameters',ReturnResult,HideCmd,Wait4Cmd,StayOnScreen)
; Parameters: (i) ReturnResult  1 = Return result of command    0 = Do not return result
;             (i) HideCmd       1 = Hide console window         0 = Do not hide
;             (i) Wait4Cmd      1 = Wait for command to finish  0 = Do not wait
;             (i) StayOnScreen  1 = Console window stays open   0 = Console window closes
;
; Note: HideCmd and Wait4Cmd are always set to 1 when ReturnResult = 1
;       StayOnScreen is set to 0 if HideCmd = 1 or ReturnResult = 1

#DefineFunction RunCmd(Cmd,ReturnResult,HideCmd,Wait4Cmd,StayOnScreen)
   ComSpec = Environment("ComSpec")
   Temp = FileNameShort(Environment("Temp"))
   Wait4Me = StrCat(Temp,"\CMD",GetTickCount())
   TempFile = StrCat(Temp,"\TmpOut.txt")
   DirRemove(Wait4Me)
   FileDelete(TempFile)
   If ReturnResult || HideCmd Then StayOnScreen = 0 ; Nothing to see
   If ReturnResult
      Cmd = StrCat(Cmd," >",TempFile," 2>&1")
      Wait4Cmd = 1 ; Must wait to get a result returned
      HideCmd = 1 ; Nothing to see if ReturnResult is enabled
   EndIf
   xHide = ""
   xWait = ""
   If HideCmd Then xHide = "Hide"
   If Wait4Cmd Then xWait = "Wait"
   If StayOnScreen
      Cmd = StrCat('/k "',Cmd,'"')
      If Wait4Cmd
         DirMake(Wait4Me)
         Cmd = StrCat(Cmd," & RD ",Wait4Me)
         xWait = "" ; Can't use RunWait with StayOnScreen
      EndIf
   Else
      Cmd = StrCat('/c "',Cmd,'"')
   EndIf
   Run%xHide%%xWait%(ComSpec,Cmd)
   ;Pause("Run%xHide%%xWait%",StrCat(ComSpec," ",Cmd)) ; Enable to debug commands
   TimeDelay(0.25)
   Result = ""
   If ReturnResult Then Result = StrReplace(FileGet(TempFile),StrCat(@CR,@CR),@CR)
   While StayOnScreen && Wait4Cmd && DirExist(Wait4Me)
      TimeDelay(0.25)
   EndWhile
   Return(Result)
#EndFunction

;Here are some examples:

; Wait for command, Keep result on screen
Result = RunCmd('ipconfig /all',0,0,1,1)
Message("Done",Result)

; Wait for command, Keep result on screen (with quotes and environment variables)
Result = RunCmd('xcopy "%%WinDir%%\Driver Cache" "%%Temp%%\Test\" /crisy',0,0,1,1)
Message("Done",Result)

; Don't wait for command, Keep result on screen
Result = RunCmd('xcopy "%%WinDir%%\Driver Cache" "%%Temp%%\Test\" /crisy',0,0,0,1)
Message("Done",Result)

; Wait for command, Don't keep result on screen
Result = RunCmd('xcopy "%%WinDir%%\Driver Cache" "%%Temp%%\Test\" /crisy',0,0,1,0)
Message("Done",Result)

; Return result to a variable, Hide while running, Wait for command
Result = RunCmd('ipconfig /all',1,1,1,0)
Message("Done",Result)

; Return result to a variable, Hide while running, Wait for command
Result = RunCmd('ping.exe 127.0.0.1',1,1,1,0)
Message("Done",Result)


Article ID:   W17282
File Created: 2007:07:03:14:29:16
Last Updated: 2007:07:03:14:29:16