下面的程序做你想做的事:
@echo off
setlocal EnableDelayedExpansion
rem Create the info removing empty lines
(for /F "delims=" %%a in ('schtasks /query /fo list /v') do echo %%a) > schtasks.txt
rem Add an additional "HostName:" line at end as delimiter
echo HostName: >> schtasks.txt
rem Create a vector of number of lines containing "HostName:"
set i=0
for /F "delims=:" %%a in ('findstr /N "HostName:" schtasks.txt') do (
set /A i+=1
set header[!i!]=%%a
)
rem Seek for the LINES containing "admin"
for /F "delims=:" %%a in ('findstr /N /I "administrator" schtasks.txt') do (
rem Seek for the NEXT section that contains "admin" line
for /L %%i in (1,1,%i%) do if %%a gtr !header[%%i]! set thisSection=%%i
rem Locate that section
set /A start=header[!thisSection!], nextSection=thisSection+1
set /A end=header[!nextSection!]-1
rem ... and show it
set line=0
for /F "delims=" %%a in (schtasks.txt) do (
set /A line+=1
if !line! geq !start! if !line! leq !end! echo %%a
)
echo ----------------------------------------------
)
del schtasks.txt
PS - 当然,MS-DOS/Windows Batch 是一种编程语言!