Search code examples
batch-filenssm

Can't use nssm set command in batch script


When using this command in my batch script the set command is interpreted as the default windows batch "set an environment command":

@echo off
echo "Always run this script as Administrator. If you don't the service will be installed but not correctly configured!"

set serviceName=MyApp
set serviceLabel=MyApp
set serviceExe=MyApp.exe

set maindir=C:\\Users\\MyUser\\Desktop\\MyApp\\
set nssm=%maindir%\\lib\\nssm-2.24\\win64\\nssm.exe

%nssm% install %serviceName% "%maindir%\\%serviceExe%"

How do I prevent this?


Solution

  • An example, based upon my comment:

    @Echo Off
    Set "serviceName=MyApp"
    Set "serviceExe=MyApp.exe"
    Set "maindir=C:\\Users\\MyUser\\Desktop\\MyApp"
    Set "nssm=%maindir%\\lib\\nssm-2.24\\win64\\nssm.exe"
    
    "%nssm:\\=\%" install %serviceName% "%maindir%\\%serviceExe%"