Page 1 of 2

Manage multiple databases services using batch script

Posted: 05 Feb 2019 05:45
by suadhm
Hi,

I'm trying to load multiple lines from file and set each line as parameter.

Here is my problem:

1. I have text file that contains several lines with one word
2. I count lines in file with:

Code: Select all

findstr /R /N "^" %temp%\_list.log | find /C ":" > %temp%\_count.log
set /p C1=<"%temp%\_count.log"
3. I need to set each word as parameter, I tried that with:

Code: Select all

for /f "delims== tokens=1,2" %%G in (%temp%\_list.log) do set /p %%H=%%G
And that works good but I need to set parameter %%H. Because I get this:

Code: Select all

set /p =word1
set /p =word2
...
set /p =wordN

I need to get this:

Code: Select all

set /p param1=word1
set /p param2=word2
...
set /p paramN=wordN
How can I set %%H for each line in file (each word)?

I tried with:

Code: Select all

FOR /L %%H IN (1,1,%C1%) DO (
for /f "delims== tokens=1,2" %%G in (%temp%\_list.log) do set /p %%H=%%G
)
But results are not good. :cry:



In advance, thanks for the help!!!

Re: Load multiple parameters from file

Posted: 05 Feb 2019 06:32
by elzooilogico
give this a try

Code: Select all

setlocal enabledelayedexpansion
set /a cnt=0
for /f "delims== tokens=1" %%G in (%temp%\_list.log) do (set /a cnt+=1 & set "param!cnt!=%%G")
You need delayed expansion as cnt is incremented within a code block
to know how many params have been loaded just see what's inside cnt

Re: Load multiple parameters from file

Posted: 05 Feb 2019 06:39
by suadhm
Hi,
Thank you for the replay.

I got this:

Code: Select all

set param!cnt!=word1 & set /a cnt+=1
2
set param!cnt!=word2 & set /a cnt+=1
3
set param!cnt!=word3 & set /a cnt+=1
4
set param!cnt!=word4 & set /a cnt+=1
5
For four words in the file. Number of words in the file is variable.

Regards.

Re: Load multiple parameters from file

Posted: 05 Feb 2019 07:32
by Squashman
Why did you feel it was not necessary to provide an example of the input file?

Re: Load multiple parameters from file

Posted: 05 Feb 2019 07:40
by suadhm
Input file contains just some words:

Code: Select all

word1
word2
word3
File is created from:

Code: Select all

sc query state= all | find "SERVICE_NAME" | find "SOME_SERVICE_NAME" > %temp%\_list.log
Regards.

Re: Load multiple parameters from file

Posted: 05 Feb 2019 07:53
by elzooilogico

Code: Select all

@echo off
setlocal enabledelayedexpansion
set /a cnt=0
for /f "delims== tokens=1" %%G in (%temp%\_list.log) do (set /a cnt+=1 & set "param!cnt!=%%G")
set param
please provide output for this
more also, you don't need an intermediate file, you can run the sc command in a for loop an fetch it's output

Re: Load multiple parameters from file

Posted: 05 Feb 2019 08:14
by suadhm
Thank you.

When I execute your code I get:

Code: Select all

cmd> par.bat
param!cnt!=word4
param1=word1
param2=word2
param3=word3
param4=word4
When I test parameters I get:

Code: Select all

cmd> echo %param1%
%param1%

But I need this:

Code: Select all

cmd> echo %param1%
word1
How can I achieve this?
you don't need an intermediate file, you can run the sc command in a for loop an fetch it's output
Regards.

Re: Load multiple parameters from file

Posted: 05 Feb 2019 10:57
by Squashman
Are you trying to echo the values of the variables from the command prompt after the batch file has run?

Re: Load multiple parameters from file

Posted: 05 Feb 2019 11:20
by Aacini
.... in other words:

Try to insert this line:

Code: Select all

echo %param1%
at end of par.bat :roll:

Antonio

Re: Load multiple parameters from file

Posted: 06 Feb 2019 02:27
by suadhm
Hi,
I tried both, after the batch and in batch, but output is not as expected.

I get this:

Code: Select all

CMD >par.bat
word1
word2
word3
word4
param!cnt!=word4
ECHO is off.

Re: Load multiple parameters from file

Posted: 06 Feb 2019 05:38
by Aacini
I tried this. It works.

This is the _list.log file:

Code: Select all

word1
word2
word3
This is the par.bat Batch file:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set /a cnt=0
for /f %%G in (_list.log) do (set /a cnt+=1 & set "param!cnt!=%%G")
set param
echo %param1%
This is the output:

Code: Select all

C:\Users\Antonio\Documents\test> par.bat
param1=word1
param2=word2
param3=word3
word1
If you not list the code you used, we have no idea of what the problem could be...

Antonio

Re: Load multiple parameters from file

Posted: 06 Feb 2019 05:54
by suadhm
Great!

That code works!!

Thank you!!


Best regards.

Re: Load multiple parameters from file

Posted: 06 Feb 2019 06:19
by elzooilogico
Also, to avoid the intermediate file

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set /a cnt=0
set "MyServiceName=Enter_Name_Here"
for /f "tokens=1,* delims=: " %%G in ('sc query state^= all ^| find /I "SERVICE_NAME" ^| find /I "%MyServiceName%"') do (set /a cnt+=1 & set "param!cnt!=%%H")
set param
echo %param1%
NOTE, equal = and pipe | symbols must be escaped in the for clause

Re: Load multiple parameters from file

Posted: 06 Feb 2019 06:31
by suadhm
Great!

With this code I don't need previous one.

Thank you!!!


Best regards.

Re: Load multiple parameters from file

Posted: 07 Feb 2019 06:08
by suadhm
Hi,

I have another question.

I got service names as parameters and now I need to check service state for all selected services.
Like this:

Code: Select all

set "_stat="
set "%ERRORLEVEL%="
sc query "%param1%" | findstr RUNNING > nul
if %ERRORLEVEL% == 1 set _stat=STOPPED
if %ERRORLEVEL% == 0 set _stat=RUNNING
echo %_stat%
How to do this for all selected services?

Regards.