用于在行之间显示的 Windows 批处理脚本

发布于 2024-12-15 19:02:54 字数 2103 浏览 0 评论 0原文

我正在寻找一个批处理脚本,它显示给定文件中的几行。 例如,假设我有一个内容为

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 的文件,如果我想从 SERVICE_NAME: BFE 打印到 SERVICE_NAME: CertPropSvc。如何在 Windows 批处理脚本中执行此操作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的韩爷 2024-12-22 19:02:54

读取 HELP FORHELP IF

然后开始实现一个循环以这种方式读取文件

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

并添加一些内容检查...

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
)

一旦完成此设置,请使用一个变量来标记您需要复制的部分,像这样

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

您需要在循环外初始化这个 var

set /a inside=0

并使用这个变量来决定是否使用该行,

if %inside%==1 echo %%a

但是,它可能不起作用,您需要注意延迟扩展 ...阅读帮助SET,然后尝试在 BAT 文件顶部添加 SETLOCAL ENABLEDELAYEDEXPANSION,并将变量引用(而不是 %VAR%)更改为 !VAR!

我把所有的部分放在一起作为练习留给你。

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.

回心转意 2024-12-22 19:02:54

您可以调用子例程,将其输入重定向到您想要读取的文件,并在子例程内使用 set /P line= 命令读取文件行。子例程必须搜索起始行,然后从该行开始echo,直到结束行。下面的批处理文件从参数 %1 和 %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

要获取所需的行,如果之前的批处理文件名为 BETWEEN.BAT

BETWEEN "SERVICE_NAME: BFE" "SERVICE_NAME: CertPropSvc"

也许您可能需要添加一些额外的内容代码来检查参数中给出的起始行或结束行是否不存在于文件中,并在这些情况下退出读取循环。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文