CmdExec: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (added stay mode)
 
(8 intermediate revisions by 2 users not shown)
Line 2: Line 2:


== Description ==
== Description ==
Execute a command with cmd.exe, with optional dospause and execwait.
Macro that uses the windows default command line interpreter (usually $sysdir\cmd.exe) to execute a command and its parameters, with working directory, optional pause, stay-in-prompt and execwait.


== Example ==
== Example ==
Line 14: Line 14:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  
!define Cmd "!insertmacro _CmdExec 0"        ; nopause/nowait
!define Cmd "!insertmacro _CmdExec 0"        ; nopause/nowait
!define CmdPause "!insertmacro _CmdExec 1"     ; dospause
!define CmdWait "!insertmacro _CmdExec 1"     ; wait for end of execution
!define CmdWait "!insertmacro _CmdExec 2"     ; execwait
!define CmdPause "!insertmacro _CmdExec 2"     ; pause before exiting
!define CmdPauseWait "!insertmacro _CmdExec 3" ; dospause+execwait
!define CmdPauseWait "!insertmacro _CmdExec 3" ; pause + execwait
!define CmdStay "!insertmacro _CmdExec 4"      ; stay in command box after execution
!define CmdStay "!insertmacro _CmdExec 4"      ; stay in command prompt
!define CmdStayWait  "!insertmacro _CmdExec 5"  ; stay in command box after execution, and wait for exit
!define CmdStayWait  "!insertmacro _CmdExec 5"  ; stay + execwait
;;
!macro _CmdExec _Mode_ _WorkDir_ _CommandAndParams_
!macro _CmdExec _Mode_ _WorkDir_ _CommandAndParams_
   StrCmp "${_WorkDir_}" "" +3  
  Push $R0
       Push $OutDir  
  ExpandEnvStrings $R0 '%COMSPEC%'
       SetOutPath "${_WorkDir_}"  
   StrCmp "${_WorkDir_}" "" +3
   !if '${_Mode_}' == '1'  
       Push $OutDir
       Exec `"$SYSDIR\cmd.exe" /c "${_CommandAndParams_}" & echo. & echo. & pause`  
       SetOutPath "${_WorkDir_}"
   !else if '${_Mode_}' == '2'  
   !if '${_Mode_}' == '1'
       ExecWait `"$SYSDIR\cmd.exe" /c "${_CommandAndParams_}"`  
       Exec `"$R0" /c "${_CommandAndParams_}" & echo. & echo. & pause`
   !else if '${_Mode_}' == '3'  
   !else if '${_Mode_}' == '2'
       ExecWait `"$SYSDIR\cmd.exe" /c "${_CommandAndParams_}" & echo. & echo. & pause`  
       ExecWait `"$R0" /c "${_CommandAndParams_}"`
   !else if '${_Mode_}' == '3'
       ExecWait `"$R0" /c "${_CommandAndParams_}" & echo. & echo. & pause`
   !else if '${_Mode_}' == '4'
   !else if '${_Mode_}' == '4'
       Exec `"$SYSDIR\cmd.exe" /k "${_CommandAndParams_}"`
       Exec `"$R0" /k "${_CommandAndParams_}"`
   !else if '${_Mode_}' == '5'
   !else if '${_Mode_}' == '5'
       ExecWait `"$SYSDIR\cmd.exe" /k "${_CommandAndParams_}"`
       ExecWait `"$R0" /k "${_CommandAndParams_}"`
   !else  
   !else
       Exec `"$SYSDIR\cmd.exe" /c "${_CommandAndParams_}"`  
       Exec `"$R0" /c "${_CommandAndParams_}"`
   !endif  
   !endif
   StrCmp "${_WorkDir_}" "" +4
   StrCmp "${_WorkDir_}" "" +3
       Exch $R0  
       Pop $R0
       SetOutPath "$R0"  
       SetOutPath "$R0"
      Pop $R0  
  Pop $R0
!macroend</highlight-nsis>
!macroend</highlight-nsis>


[[Category:Functions & Macros]]
[[Category:Functions & Macros]]

Latest revision as of 04:05, 6 February 2012

Author: Lloigor (talk, contrib)


Description

Macro that uses the windows default command line interpreter (usually $sysdir\cmd.exe) to execute a command and its parameters, with working directory, optional pause, stay-in-prompt and execwait.

Example

${CmdPause} `$ParentFolder` `"d:\tools\free_upx\upx.exe" -v --brute $FileNames`

Macro

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
;;  Execute a command with cmd.exe 
;;  P1 :in: Working directory (""=$outdir) 
;;  P2 :in: Command and parameters 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
!define Cmd "!insertmacro _CmdExec 0"         ; nopause/nowait
!define CmdWait "!insertmacro _CmdExec 1"      ; wait for end of execution
!define CmdPause "!insertmacro _CmdExec 2"     ; pause before exiting
!define CmdPauseWait "!insertmacro _CmdExec 3" ; pause + execwait
!define CmdStay "!insertmacro _CmdExec 4"       ; stay in command prompt
!define CmdStayWait  "!insertmacro _CmdExec 5"  ; stay + execwait
;;
!macro _CmdExec _Mode_ _WorkDir_ _CommandAndParams_
   Push $R0
   ExpandEnvStrings $R0 '%COMSPEC%'
   StrCmp "${_WorkDir_}" "" +3
      Push $OutDir
      SetOutPath "${_WorkDir_}"
   !if '${_Mode_}' == '1'
      Exec `"$R0" /c "${_CommandAndParams_}" & echo. & echo. & pause`
   !else if '${_Mode_}' == '2'
      ExecWait `"$R0" /c "${_CommandAndParams_}"`
   !else if '${_Mode_}' == '3'
      ExecWait `"$R0" /c "${_CommandAndParams_}" & echo. & echo. & pause`
   !else if '${_Mode_}' == '4'
      Exec `"$R0" /k "${_CommandAndParams_}"`
   !else if '${_Mode_}' == '5'
      ExecWait `"$R0" /k "${_CommandAndParams_}"`
   !else
      Exec `"$R0" /c "${_CommandAndParams_}"`
   !endif
   StrCmp "${_WorkDir_}" "" +3
      Pop $R0
      SetOutPath "$R0"
   Pop $R0
!macroend