I am running a command like
nssm.exe get MyWindowsService AppParameters
and its output is
--client-version 6.1.0
Now I want to append some text to this output like this
--client-version 6.1.0 --baseUrl helloworld.txt
And then run a new command with this new output
nssm.exe set MyWindowsService AppParameters = --client-version 6.1.0 --baseUrl helloworld.txt
I want to do all this in a batch file so I just need to add "--baseUrl helloworld.txt" and it should handle concatenation with the output of first command and execution on its own with second command.
I've been using {for /f} to hold the value of the output returned by a command and then use that output. But things are not going as expected as depicted in the attached image
I've used this approach(to read text from file) few times and it seems to work fine but sometimes when I tried to read the text from a file which is being "output(ted)" by command line, it won't work. Please guide me on how to handle this situation and what is causing it
You can find the output file here https://www.dropbox.com/s/yr6s7eo8d8uwhey/output.txt?dl=0
Your problem is the unicode output when retrieving the AppParameters. You can deal with it as
nssm get MyWindowsService AppParameters > tempFile
for /f "delims=" %%a in ('
^< tempFile find /v ""
') do (
nssm.exe set MyWindowsService AppParameters "%%a --baseUrl helloworld.txt"
)
or
nssm get MyWindowsService AppParameters > tempFile
for /f "delims=" %%a in ('
more tempFile
') do (
nssm.exe set MyWindowsService AppParameters "%%a --baseUrl helloworld.txt"
)
Or, without a temp file
for /f "delims=" %%a in ('
nssm get MyWindowsService AppParameters ^| sort
') do (
nssm.exe set MyWindowsService AppParameters "%%a --baseUrl helloworld.txt"
)
edited included sample code of how to deal with quotes.
@echo off
setlocal enableextensions disabledelayedexpansion
set "svc=MyWindowsService"
nssm install "%svc%" "c:\windows\system32\cmd.exe"
nssm set "%svc%" Start SERVICE_DEMAND_START
nssm set "%svc%" AppParameters "--client-version ""6.1.0"""
echo ---------------------------------------------------------------
nssm get "%svc%" AppParameters
echo ---------------------------------------------------------------
for /f "delims=" %%a in ('
nssm get "%svc%" AppParameters ^| sort
') do (
set "appParams=%%a"
setlocal enabledelayedexpansion
nssm.exe set "%svc%" AppParameters "!appParams:"=""!" "--baseUrl=""C:\ProgramData\\"""
endlocal
)
echo ---------------------------------------------------------------
nssm get "%svc%" AppParameters
echo ---------------------------------------------------------------
nssm remove "%svc%" confirm