-2

I need to receive a string with processes whose name is equal to "MSSQLSERVER" or starts with "MSSQL$". The names should be separated by forward slashes (/). I am trying to do it with such script:

@echo off
set mssqlDependenties=
set defaultSqlServerServiceName="MSSQLSERVER"
set namedSqlServerServiceNameBeginning="MSSQL$"

setlocal enabledelayedexpansion
for /f "tokens=2" %%s in ('sc query state^= all ^| find "SERVICE_NAME: MSSQL"') do (
    set serviceName=%%s
    if "%%s" equ %defaultSqlServerServiceName% (
        set mssqlDependenties=!mssqlDependenties!/%%s
    )   
    if "%serviceName:~0,7%" equ %namedSqlServerServiceNameBeginning% (
        set mssqlDependenties=!mssqlDependenties!/%%s
    )
)

echo %mssqlDependenties%

But I receive an error:

The syntax of the command is incorrect.

mssqlDependenties should contain MSSQLSERVER/MSSQL$1/MSSQL$2/MSSQL$3, etc.

Sheinar
  • 362
  • 3
  • 14
  • Quotes need to be around both of the things you're comparing. `%serviceName:~0,7%" equ %namedSqlServerServiceNameBeginning%` should be `"%serviceName:~0,7%" equ "%namedSqlServerServiceNameBeginning%"`. – SomethingDark Jul 23 '20 at 19:02
  • You need to confirm whether you want `%mssqlDependenties%` to contain `/MSSQLSERVER` or `/MSSQLRANDOM1/MSSQLRANDOM2/MSSQLRANDOM3`, or potentially `/MSSQLSERVER/MSSQLRANDOM1/MSSQLRANDOM2/MSSQLRANDOM3`, or `/MSSQLRANDOM1/MSSQLRANDOM2/MSSQLRANDOM3/MSSQLSERVER`. If `MSSQLSERVER` exists, is that all you want, or do you need them all listing regardless of which or how many are found? Also your question asks about processes, but these are services. What exactly are you trying to achieve? Are these sevices/processes supposed to be all? only those which are running? etc. – Compo Jul 23 '20 at 19:15
  • 2
    Your are inside a parenthesized code block. Any variables set or changed need to be referenced with delayed expansion. – Squashman Jul 23 '20 at 19:17
  • @SomethingDark Unless the post was edited, the variable `namedsqlserverservicenamebeginning` already has double quotes. The `if` statement should only be missing a leading double quote. AND it also needs delayed expansion `if "!serviceName:~0,7!" equ %namedSqlServerServiceNameBeginning%` – avery_larry Jul 23 '20 at 20:35
  • with 'if "%serviceName:~0,7%" equ %namedSqlServerServiceNameBeginning% (' I receive "The syntax of the command is incorrect." error. And with 'if "%serviceName:~0,7%" equ "%namedSqlServerServiceNameBeginning%" I got "( was unexpected at this time." error. – Sheinar Jul 23 '20 at 20:39
  • avery_larry, thanks, it works with if "!serviceName:~0,7!" equ %namedSqlServerServiceNameBeginning% – Sheinar Jul 23 '20 at 21:16
  • @Sheinar, your posted solution does not answer the question, because your question requires clarification. Leaving it so that nobody can understand the task, except for you, means that only yourself can answer. The code you've posted is improvable, do you not want better, or improved code? Please answer the questions I asked, in order that we can provide something more appropriate, useful, robust, efficient etc. – Compo Jul 24 '20 at 11:56

1 Answers1

-1

The solution from avery_larry works perfectly: I changed

 if "%serviceName:~0,7%" equ %namedSqlServerServiceNameBeginning% (

to

if "!serviceName:~0,7!" equ %namedSqlServerServiceNameBeginning% (
Sheinar
  • 362
  • 3
  • 14