Execute Command-Line Command from NSIS

前端 未结 3 1220
梦如初夏
梦如初夏 2021-02-07 08:28

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

相关标签:
3条回答
  • 2021-02-07 08:56

    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)

    0 讨论(0)
  • 2021-02-07 09:04

    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'
    
    0 讨论(0)
  • 2021-02-07 09:17

    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.

    0 讨论(0)
提交回复
热议问题