ash
ash

Reputation: 9

A windows batch script to display between the lines

I'm looking for a batch script that displays a couple of lines from a given file. For example lets say i have a file with the contents as

SERVICE_NAME: Appinfo
DISPLAY_NAME: Application Information
        TYPE               : 20  WIN32_SHARE_PROCESS  
        STATE              : 4  RUNNING 
                                (STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

SERVICE_NAME: BFE
DISPLAY_NAME: Base Filtering Engine
        TYPE               : 20  WIN32_SHARE_PROCESS  
        STATE              : 4  RUNNING 
                                (STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

SERVICE_NAME: BITS
DISPLAY_NAME: Background Intelligent Transfer Service
        TYPE               : 20  WIN32_SHARE_PROCESS  
        STATE              : 4  RUNNING 
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

SERVICE_NAME: CertPropSvc
DISPLAY_NAME: Certificate Propagation
        TYPE               : 20  WIN32_SHARE_PROCESS  
        STATE              : 4  RUNNING 
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

SERVICE_NAME: Cissesrv
DISPLAY_NAME: HP Smart Array SAS/SATA Event Notification Service
        TYPE               : 10  WIN32_OWN_PROCESS  
        STATE              : 4  RUNNING 
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

Now if I want to print from SERVICE_NAME: BFE till SERVICE_NAME: CertPropSvc. How do I do this in a windows batch script?

Upvotes: 0

Views: 259

Answers (2)

Aacini
Aacini

Reputation: 67296

You may invoke a subroutine redirecting its input to the file you want to read and inside the subroutine read file lines with set /P line= command. The subroutine must search for the starting line and then echo from this line until the ending one. The Batch file below take the starting and ending lines from parameters %1 and %2:

@echo off
call :SearchStart %1 %2 < the_file.txt
exit /b

:SearchStart
set /P line=
if /I not "%line%" == "%~1" goto SearchStart
:PrintUntilEnd
echo/%line%
set line=
set /P line=
if /I not "%line%" == "%~2" goto PrintUntilEnd

To get the lines you want, if previous Batch file was named BETWEEN.BAT:

BETWEEN "SERVICE_NAME: BFE" "SERVICE_NAME: CertPropSvc"

Perhaps you may want to add some additional code to check if the starting or ending lines given in the parameters are not present in the file and exit from the reading loops in these cases.

Upvotes: 1

PA.
PA.

Reputation: 29369

read HELP FOR and HELP IF

then begin implementing a loop to read your file this way

for /f "tokens=*" %%a in (c:\temp\services.txt) do (
  echo %%a
)

and add to it some checking of the contents...

for /f "tokens=*" %%a in (c:\temp\services.txt) do (
  if /i "%%a"=="SERVICE_NAME: BFE" echo Begin here
  if /i "%%a"=="SERVICE_NAME: CertPropSvc" echo End here
)

once you have this setup, use a variable to mark the part you need to copy, something like this

if /i "%%a"=="SERVICE_NAME: BFE" set /a inside=1
if /i "%%a"=="SERVICE_NAME: CertPropSvc" set /a inside=0

you will need to initialize this var outside the loop

set /a inside=0

and use this variable to decide whether or not use the line

if %inside%==1 echo %%a

but, it may not work, you will need to take care of delayed expansion ... read HELP SET and then try adding SETLOCAL ENABLEDELAYEDEXPANSION at the top of your BAT file, and changing variable references, instead of %VAR%, to !VAR!

I leave putting all pieces together as an excercise to you.

Upvotes: 1

Related Questions