TO DISPLAY ALL RUNNING AUTOMATIC SERVICES PAGE BY PAGE

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jopijose
Posts: 3
Joined: 17 Sep 2017 22:55

TO DISPLAY ALL RUNNING AUTOMATIC SERVICES PAGE BY PAGE

#1 Post by jopijose » 17 Sep 2017 23:07

Hello Team,

Greetings for the day !!

I'm a newbie to this forum, just trying to get help from experts to fix my below code.

I'm trying to get all running automatic services and display 25 lines per window, my if loop not actually not going to next even after the counter %plcnt% has a -ve value, could any one help to fix this, it will be a great help.

Code: Select all

:: CODE 
::
@ECHO OFF
set slcnt=1
set elcnt=25
set file=c:\tmp\tmp_file.txt
:loop
ECHO ________________________________________________________________________
ECHO HOST NAME: %computername%
ECHO DATE STAMP: %time:~-11,2%:%time:~-8,2% %date%
ECHO ========================================================================
ECHO SYSTEM RUNNING AUTO SERVICES STATUS
ECHO ========================================================================
WMIC SERVICE GET name,StartMode,State,Status | FINDSTR "Auto" | FINDSTR /V "Manual Stopped" > %file% && for /l %%l in (%slcnt%,1,%elcnt%) do @for /f "tokens=1,2* delims=:" %%a in ('findstr /n /r "^" %file% ^| findstr /r "^%%l:"') do @echo %%b
ECHO __________________________LINE %slcnt% TO %elcnt%________________________________
TIMEOUT 3 > NUL
CLS
:: FIND TOTAL AND PENDING LINE COUNT
set /a tlcnt=0
for /f %%a in ('type "%file%"^|find "" /v /c') do set /a tlcnt=%%a
::
set /a plcnt= %tlcnt% - %elcnt%
::
:: IF PENDING LINE COUNT GREATER THAN OR EQUAL TO 0 THEN LOOP
::
IF /i %plcnt% GEQ "0" (
set slcnt=%elcnt%
set /a elcnt=25 + %elcnt%
   GOTO :loop
) ELSE (
::
   GOTO :next
)   
::
:next
ECHO NEXT COMMAND
::
:EOF

Thanks in Advance to all Batch Guru's
Last edited by aGerman on 17 Sep 2017 23:57, edited 1 time in total.
Reason: code tags added

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: TO DISPLAY ALL RUNNING AUTOMATIC SERVICES PAGE BY PAGE

#2 Post by Compo » 18 Sep 2017 03:42

I'm not sure if I understand exactly what you're trying to do, but this would be my best guess.

Code: Select all

@Echo Off
Mode 80,26
SetLocal EnableDelayedExpansion
Set "i=0"
For /F "Skip=1 Delims=" %%A In ('WMIC Service Where^
 "StartMode='Auto' And State='Running'" Get Name^,StartMode^,State^,Status'
) Do For /F "Delims=" %%B In ("%%A") Do (Set/A i=i%%25+1
   Echo %%B
   If !i! Equ 25 Timeout 7 /NoBreak>Nul&ClS)
Timeout -1
<Edit />
The script will currently pause for seven seconds before each new screen of data, change 7 in the second last line to adjust it.
Last edited by Compo on 18 Sep 2017 03:50, edited 2 times in total.

penpen
Expert
Posts: 1999
Joined: 23 Jun 2013 06:15
Location: Germany

Re: TO DISPLAY ALL RUNNING AUTOMATIC SERVICES PAGE BY PAGE

#3 Post by penpen » 18 Sep 2017 03:45

The reason why your code fails is the following line:

Code: Select all

IF /i %plcnt% GEQ "0" (
This if command compares the content of the environment variable plcnt with "0" as a text string.

You better should compare the number value only which could be done by removing the option "/i" and the doublequotes:

Code: Select all

IF %plcnt% GEQ 0 (


You should also note that there is another issue with your code:
You are taking a new snapshot in every loop pass.

This is a bad idea because of multiple side effects. For example if a service you have displayed terminates,
then you will miss a service in the next loop iteration.

You better take one snapshot only before the loop starts, and you are using much too many findstr calls.


Sidenote:
I just noticed this is an interesting find (at least for me).

I never noticed before that the if command using the "/i" option ignores minus characters ('-') in comparisons:

Code: Select all

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. Alle Rechte vorbehalten.

Z:\>if /i "-1" GEQ "0" echo ok
ok

Z:\>if /i "--1" GEQ "0" echo ok
ok

Z:\>if /i "----------1----------" GEQ "-0-" echo ok
ok

Z:\>if /i "----------1--------0--" GEQ "10" echo ok
ok

Z:\>if /i "----------1--------0--" GEQ "12" echo ok

Z:\>

I currently have no access to any windows versions other than Windows 10.0.14393 (32 bit).
So i wonder if all Windows versions behave this way.


penpen

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: TO DISPLAY ALL RUNNING AUTOMATIC SERVICES PAGE BY PAGE

#4 Post by Compo » 18 Sep 2017 03:55

penpen wrote:I never noticed before that the if command using the "/i" option ignores minus characters ('-') in comparisons:

Code: Select all

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. Alle Rechte vorbehalten.

Z:\>if /i "-1" GEQ "0" echo ok
ok

Z:\>if /i "--1" GEQ "0" echo ok
ok

Z:\>if /i "----------1----------" GEQ "-0-" echo ok
ok

Z:\>if /i "----------1--------0--" GEQ "10" echo ok
ok

Z:\>if /i "----------1--------0--" GEQ "12" echo ok

Z:\>


That's because you're doing a non numeric comparison!

Code: Select all

C:\Users\Compo>If /I -1 GEq 0 @Echo OK

C:\Users\Compo>If /I "-1" GEq "0" @Echo OK
OK

jopijose
Posts: 3
Joined: 17 Sep 2017 22:55

Re: TO DISPLAY ALL RUNNING AUTOMATIC SERVICES PAGE BY PAGE

#5 Post by jopijose » 18 Sep 2017 04:07

@Compo
I'm trying to generate a script to take snapshot of running automatic services with 25 lines per screen
Thanks for your assistance and guidance which enabled me some other options.

@penpen
Thanks for your time in analysing the code and guiding on right direction, your valued time has saved my day

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: TO DISPLAY ALL RUNNING AUTOMATIC SERVICES PAGE BY PAGE

#6 Post by Compo » 18 Sep 2017 04:20

The script I posted displays all running automatic services page by page, which is exactly as your subject title says.

The only difference is that yours also outputs the data, (with some additional bloat), to a file.

Here is a version which saves the data to an output file as did yours:

Code: Select all

@Echo Off
SetLocal EnableDelayedExpansion

Rem Creating output file
Set "file=C:\tmp\tmp_file.txt"
(Echo _________________________________________________________________________
 Echo HOST NAME: %COMPUTERNAME%
 Echo DATE STAMP: %TIME:~,5% %DATE:~-10%
 Echo =========================================================================
 Echo SYSTEM RUNNING AUTO SERVICES STATUS
 Echo =========================================================================
)>"%file%"
WMIC /Append:"%file%" Service Where "StartMode='Auto' And State='Running'"^
 Get Name,StartMode,State,Status

Rem Counting lines and setting variables
For /F %%A In ('Find /C /V ""^<"%file%"') Do Set/A l=10+%%A-16,r=n=p=#=0

Rem Setting window parameters
Mode 80,27

Rem Displaying output file on screen
For /F "UseBackQ Skip=7 Delims=" %%A In ("%file%"
) Do (Set/A r=n%%25+1,n+=1,p=n/25*25+1,#=24+p
   If !#! Gtr %l% Set "#=%l%"
   If !r! Equ 1 Echo ----- LINES !p! TO !#! -----
   Echo %%A
   If !r! Equ 25 Timeout 7 /NoBreak>Nul&ClS)

Rem Ending script
Echo=&Echo Press any key to exit ...
Timeout -1>Nul
Last edited by Compo on 18 Sep 2017 08:46, edited 1 time in total.

penpen
Expert
Posts: 1999
Joined: 23 Jun 2013 06:15
Location: Germany

Re: TO DISPLAY ALL RUNNING AUTOMATIC SERVICES PAGE BY PAGE

#7 Post by penpen » 18 Sep 2017 04:31

Compo wrote:That's because you're doing a non numeric comparison!

Code: Select all

C:\Users\Compo>If /I -1 GEq 0 @Echo OK

C:\Users\Compo>If /I "-1" GEq "0" @Echo OK
OK
That's not the reason in this case, but maybe my explanation was not good enough.

Batch string comparison is done from left to right.
So if no character is ignored, then the following indicates that the minus character value ('-') is greater or equal to the one character value ('1'):

Code: Select all

Z:\>if /i "-1" GEQ "1" echo #
#

But this indicates the opposite:

Code: Select all

Z:\>if /i "-0" GEQ "1" echo #

Z:\>

Both facts cannot be true at the same time, therefore the minus character is ignored during comparison (which i never have realized before).


penpen

jopijose
Posts: 3
Joined: 17 Sep 2017 22:55

Re: TO DISPLAY ALL RUNNING AUTOMATIC SERVICES PAGE BY PAGE

#8 Post by jopijose » 18 Sep 2017 05:36

@compo & penpen,

Thanks for all your valuable suggestions and guidance, it was a nice learning as a beginner in batch scripting.

Thanks once again for a wonderful support .. have a great one. Enjoy your day !!

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: TO DISPLAY ALL RUNNING AUTOMATIC SERVICES PAGE BY PAGE

#9 Post by Compo » 18 Sep 2017 08:51

@penpen, thanks for clarifying I should learn to read before making assumptions.

@jopijose, I have updated my last post with an additional example, perhaps a little closer to what you intended!

Post Reply