Manage multiple databases services using batch script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
suadhm
Posts: 13
Joined: 05 Feb 2019 05:22

Manage multiple databases services using batch script

#1 Post by suadhm » 05 Feb 2019 05:45

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!!!
Last edited by suadhm on 08 Feb 2019 08:36, edited 1 time in total.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Load multiple parameters from file

#2 Post by elzooilogico » 05 Feb 2019 06:32

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

suadhm
Posts: 13
Joined: 05 Feb 2019 05:22

Re: Load multiple parameters from file

#3 Post by suadhm » 05 Feb 2019 06:39

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.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Load multiple parameters from file

#4 Post by Squashman » 05 Feb 2019 07:32

Why did you feel it was not necessary to provide an example of the input file?

suadhm
Posts: 13
Joined: 05 Feb 2019 05:22

Re: Load multiple parameters from file

#5 Post by suadhm » 05 Feb 2019 07:40

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.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Load multiple parameters from file

#6 Post by elzooilogico » 05 Feb 2019 07:53

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

suadhm
Posts: 13
Joined: 05 Feb 2019 05:22

Re: Load multiple parameters from file

#7 Post by suadhm » 05 Feb 2019 08:14

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.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Load multiple parameters from file

#8 Post by Squashman » 05 Feb 2019 10:57

Are you trying to echo the values of the variables from the command prompt after the batch file has run?

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Load multiple parameters from file

#9 Post by Aacini » 05 Feb 2019 11:20

.... in other words:

Try to insert this line:

Code: Select all

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

Antonio

suadhm
Posts: 13
Joined: 05 Feb 2019 05:22

Re: Load multiple parameters from file

#10 Post by suadhm » 06 Feb 2019 02:27

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.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Load multiple parameters from file

#11 Post by Aacini » 06 Feb 2019 05:38

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

suadhm
Posts: 13
Joined: 05 Feb 2019 05:22

Re: Load multiple parameters from file

#12 Post by suadhm » 06 Feb 2019 05:54

Great!

That code works!!

Thank you!!


Best regards.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Load multiple parameters from file

#13 Post by elzooilogico » 06 Feb 2019 06:19

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

suadhm
Posts: 13
Joined: 05 Feb 2019 05:22

Re: Load multiple parameters from file

#14 Post by suadhm » 06 Feb 2019 06:31

Great!

With this code I don't need previous one.

Thank you!!!


Best regards.

suadhm
Posts: 13
Joined: 05 Feb 2019 05:22

Re: Load multiple parameters from file

#15 Post by suadhm » 07 Feb 2019 06:08

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.

Post Reply