1

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 enter image description here

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

Manvinder
  • 4,495
  • 16
  • 53
  • 100
  • `for /F "delims=" %%a in ('nssm.exe get MyWindowsService AppParameters') do nssm.exe set MyWindowsService AppParameters = %%a --baseUrl helloworld.txt` – Aacini Mar 11 '17 at 19:15
  • @Aacini It is saying %%a was unexpected at this time. – Manvinder Mar 11 '17 at 19:21
  • 3
    You said: "I want to do all this in a batch file..." Did you put this line _in a Batch file_? – Aacini Mar 11 '17 at 19:23
  • @Aacini My bad, but what it did is replace everything with = - --baseUrl helloworld.txt, it did not append. – Manvinder Mar 11 '17 at 19:34
  • I am afraid I don't follow you... The executed command should be exactly the same you requested accordingly to your data. You may test this point addig an `echo` command after `do` word. – Aacini Mar 11 '17 at 19:39
  • @Aacini I've tested it and I believe your command returns only the first character of the output from the previous command – Manvinder Mar 11 '17 at 20:19
  • Well, there is not need to "belive" in nothing. Just paste the output of the command (after added the `echo` part) and copy it here enclosed in "backtits". Click on "help" word after starting the comment and choose the "code" example. – Aacini Mar 11 '17 at 20:31
  • @Aacini Current output is `echo -`, My exact command is `for /F "delims=" %%a in ('nssm.exe get MyWindowsService AppParameters') do echo %%a` and the expected result of this command is `--client-version 6.1.0` – Manvinder Mar 11 '17 at 20:40
  • Well, that is the _expected output_, but what is the _real output_? At the command prompt execute: `nssm.exe get MyWindowsService AppParameters > output.txt` and then `type output.txt` or open it with a text editor. – Aacini Mar 11 '17 at 20:51
  • @Aacini --client-version 6.1.0 – Manvinder Mar 11 '17 at 20:56
  • @Aacini and this is the thing that got appended with my new string `= - ` instead of above output – Manvinder Mar 11 '17 at 20:59
  • I hope you understand that there is no way that a hypen be created "from nowhere"! Try: `for /F "delims=" %%a in (output.txt) do echo [%%a]` – Aacini Mar 11 '17 at 21:06
  • See? The real output is formed by different characters! Try: `for /F "delims=" %%a in ('type output.txt') do echo [%%a]` – Aacini Mar 11 '17 at 21:14
  • The last time I saw a `for` loop do weird stuff to a text file, it's because the text file wasn't saved as ANSI. – SomethingDark Mar 15 '17 at 22:50
  • You are asking us to _guess_ what the problem may be with no chance to review the problematic data. You should post the `output.txt` file on a public site and post here the link. I reviewed the [nssm documentation](https://nssm.cc/), but not found a clue about this problem... – Aacini Mar 16 '17 at 05:07
  • @Aacini I have added the link for the output file, please let me know if there is something else you need – Manvinder Mar 16 '17 at 11:04
  • The problem is that the output data is in Unicode format, but MC ND user have posted the solution already... – Aacini Mar 16 '17 at 12:32

1 Answers1

2

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
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Hi, it almost works except for one minor thing. While getting the parameters output contain something with double quotes and slashes like this --baseUrl="C:\ProgramData\\". Our solution removes double quotes and last double slash, it returns only one slash – Manvinder Mar 17 '17 at 04:43
  • @MegaMind, this is not related to the problem in the question but to the way [arguments are handled](https://msdn.microsoft.com/en-us/library/a1y7w461.aspx) by `nssm` (in this case). The double ending backslash is included to avoid the quote escaping, not to keep it into the saved value, so, on execution it is discarded. If you have problems with this, please, start a new question stating this problem, input parameters and what should be finally stored in the service configuration. – MC ND Mar 17 '17 at 06:56
  • @MegaMind, answer updated. Anyway, the ending backslash will be a problem in the future, better remove it and ensure the service program handle/build paths correctly. – MC ND Mar 17 '17 at 09:15