Simon M.
Simon M.

Reputation: 2482

Capture full output of command in batch file (only capture first character)

I have to create a batch file where I check the status of my service thanks to nssm. The command that I use is nssm status MyService and it returns SERVICE_STOPPED OR SERVICE_RUNNING. I do not have a lot of experience with batch file so I followed some SO answers to achieve what I want, here is my file :

FOR /F "tokens=*" %%F IN ('nssm status MyService') DO (
SET var=%%F
)
ECHO %var%

But when I echo the output I only get the first character of the expected output.

var is equal to S

I tried to change tokens and delims parameters but without success it always returns S

What did I missed ? Is it the FOR /F parameters ? Or how I set my variable ?

Upvotes: 1

Views: 796

Answers (2)

Simon M.
Simon M.

Reputation: 2482

Here is how I solved that, the problem might come with NSSM command line, instead I used SC command to query my service as mentionned @Squashman

set list=service1 service2
for %%a in (%list%) do (
  FOR /F "tokens=4" %%F IN ('sc query %%a ^| find "STATE     "') DO (
    IF %%F NEQ RUNNING (
      nssm start %%a
    )
  )
)

Upvotes: 2

CatCat
CatCat

Reputation: 491

for /f "usebackq tokens=2 delims=," %A in (`wmic service where "name='rpcss'" get state /format:csv`) Do echo %A

Upvotes: 1

Related Questions