用于在行之间显示的 Windows 批处理脚本
我正在寻找一个批处理脚本,它显示给定文件中的几行。 例如,假设我有一个内容为
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
读取
HELP FOR
和HELP IF
然后开始实现一个循环以这种方式读取文件
并添加一些内容检查...
一旦完成此设置,请使用一个变量来标记您需要复制的部分,像这样
您需要在循环外初始化这个 var
并使用这个变量来决定是否使用该行,
但是,它可能不起作用,您需要注意延迟扩展 ...阅读
帮助SET
,然后尝试在 BAT 文件顶部添加SETLOCAL ENABLEDELAYEDEXPANSION
,并将变量引用(而不是 %VAR%)更改为 !VAR!我把所有的部分放在一起作为练习留给你。
read
HELP FOR
andHELP IF
then begin implementing a loop to read your file this way
and add to it some checking of the contents...
once you have this setup, use a variable to mark the part you need to copy, something like this
you will need to initialize this var outside the loop
and use this variable to decide whether or not use the line
but, it may not work, you will need to take care of delayed expansion ... read
HELP SET
and then try addingSETLOCAL 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.
您可以调用子例程,将其输入重定向到您想要读取的文件,并在子例程内使用
set /P line=
命令读取文件行。子例程必须搜索起始行,然后从该行开始echo
,直到结束行。下面的批处理文件从参数 %1 和 %2 中获取起始行和结束行:要获取所需的行,如果之前的批处理文件名为
BETWEEN.BAT
:也许您可能需要添加一些额外的内容代码来检查参数中给出的起始行或结束行是否不存在于文件中,并在这些情况下退出读取循环。
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 thenecho
from this line until the ending one. The Batch file below take the starting and ending lines from parameters %1 and %2:To get the lines you want, if previous Batch file was named
BETWEEN.BAT
: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.