0

すでに作成したファイルからテキストのブロックを取得し、その情報を新しいファイルに保存しようとしています。解析したいテキストファイルIDは次のように設定されます。

ホスト名:xxxxxx

(8行程度の情報)

ホスト名:xxxxxx

私が抱えている最大の問題は、テキストのブロック間で「admin」という単語を具体的に探していることです。adminという単語が含まれている行を見つけて返すことができます。次に、adminという単語がテキストのブロック内で見つかった場合に、上下の情報を取得するループを作成する必要があります(空の行を使用する予定でした)私のファイル全体で一貫している区切り文字として)。

私はいくつかのforループを書きましたが、どれも私にどのようなIDを与えてくれませんでした。どんな助けでもいただければ幸いです。
ありがとう

4

1 に答える 1

2

以下のプログラムはあなたが望むことをします:

@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バッチはプログラミング言語です!

于 2012-06-23T01:39:15.043 に答える