1

In the following thread:

batch to copy last line of several .txt files into 1 new .txt file and appends file name on new line

...dbenham offered a solution to another poster, which works perfectly for my needs.

    @echo off
setlocal enableDelayedExpansion
>output.txt (
  for %%F in (*.log) do (
    <nul set /p "=%%F: "
    for /f %%N in ('type "%%F"^|find /c /v ""') do set /a skip=%%N
    if !skip! gtr 0 set /a skip-=1
    more +!skip! "%%F"
  )
)
type output.txt

My question: Can the syntax above be modified to copy the THIRD line of each *.txt file in the directory, append the filename of each file to the line, and send the results to a new output file?

Thanks kindly.

4

1 回答 1

0
@ECHO OFF
SETLOCAL
(
 FOR %%f IN (*.log) DO (
  SET output=Y
  FOR /f "usebackqskip=2delims=" %%i IN ("%%f") DO IF DEFINED output (
  SET "output="
  ECHO %%f : %%i
  )
 )
)>output.txt
GOTO :EOF

这将生成包含每个文件的第三个非空行的输出文件.LOG,前面是该文件的名称和一个冒号。对.txt文件执行相同操作的修复应该是显而易见的。

不过要注意一点 - 创建的输出文件应该NOT与正在处理的文件具有相同的扩展名。

通常,将在这些pedFOR/F之后输出每一行。SKIP通过在变量上添加门,在output扫描每个目标文件之前将其设置为某个值,然后在第三行被ECHO编辑时清除它,只有从每个文件中选择的第一行被echo编辑。

于 2013-04-30T21:55:27.810 回答