Execute Command-Line Command from NSIS
Asked Answered
C

3

22

I'm creating my first NSI script and I'm just wondering if I can execute a command-line command from NSIS or should I just execute a batch file? I don't really know where to begin and other similar topics have gone a little over my head.

Calzada answered 16/7, 2012 at 18:54 Comment(1)
The answer here might help. It works for a command-line app just fine.Haldas
U
13

I would recommend taking a look at the nsExec plugin. I just recently had a situation where I needed to ping a server from inside an NSIS script, and the following code worked perfectly for me.

nsExec::Exec '"C:\Windows\System32\PING.EXE" $URL'

The benefit of using nsExec is that it executes the command without making a dos box pop up on your screen. The return value is pushed onto the stack, and there are a couple different ways that you can access the output of the program as well (if any exists).

There isn't a whole lot of information about the plugin on the NSIS website that I could find, but the following link should get you started in the right direction:

http://nsis.sourceforge.net/Docs/nsExec/nsExec.txt

Edit:

I noticed you asked specifically about a COPY command which is an internal DOS command, meaning that you won't be able to execute it like I did with ping. I may be mistaken but you shouldn't need to use any outside programs to carry out basic commands like this. You should be able to replicate most of the internal commands using NSIS commands.

For Example to copy a file (or multiple files) use the NSIS command: CopyFiles

The NSIS Scripting Reference is your friend :) (So is ctrl+f)

Upbringing answered 18/7, 2012 at 14:15 Comment(2)
Just the right option to execute an application without displaying the command promptTravis
CopyFiles would be great, but there appears to be a ~264 character limit on it.Philippe
I
11

Try using exec command http://nsis.sourceforge.net/Docs/Chapter4.html:

4.9.1.2 Exec

command

Execute the specified program and continue immediately. Note that the file specified must exist on the target system, not the compiling system. $OUTDIR is used for the working directory. The error flag is set if the process could not be launched. Note, if the command could have spaces, you should put it in quotes to delimit it from parameters. e.g.: Exec '"$INSTDIR\command.exe" parameters'. If you don't put it in quotes it will not work on Windows 9x with or without parameters.

Exec '"$INSTDIR\someprogram.exe"'
Exec '"$INSTDIR\someprogram.exe" some parameters'
Ibbie answered 16/7, 2012 at 21:47 Comment(2)
ok, I was hoping to be able to control the command line from the .nsi and do something like this: "copy /b someprogram.exe +,," Sounds like I'll need to do it from a batch file.Calzada
did you try this Exec 'cmd /k copy /b ...' ?Ibbie
V
5

We can launch a command-line command from NSIS, get the returned value and further develop the installation logic based on that.

Example: Let's say we need to get the installed clang compiler version. To get the version we have to launch:

clang --version 

In NSIS we do this using ExecToStack:

     nsExec::ExecToStack  'cmd /c "clang --version"'
     Pop $0
     Pop $0
     ;now we have the version in $0

Warning: Only the second Pop $0 gets the response that we want, in this case the clang version. The first Pop $0 grabs the exit code.

Valenza answered 13/7, 2018 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.