【问题标题】:How to extract a specific line from multiple text file into a single txt file on Windows?如何从多个文本文件中提取特定行到 Windows 上的单个 txt 文件中?
【发布时间】:2018-09-29 10:47:47
【问题描述】:

我有 300 个不同名称的文件 (.txt)。 我希望将每个文件的第二行复制到其文件名旁边的单个文本文件中。 文件路径为 D:\WCR\

请帮助我在记事本中创建一个带有 .Bat 扩展名的脚本或在 cmd 中给出的命令

我们将不胜感激..!!

【问题讨论】:

  • pushd "d:\dcr\" &for /f "tokens=*" %%f in ('dir /b /a-d *.txt') do set "flag=" & for /f "skip=1 tokens=* delims=" %%l in ('type "%%~f"') do if not defined flag set "flag=1" & <nul set /p "=%%~l" >"%%~ff.sec"

标签: batch-file command-line


【解决方案1】:

此批处理文件可用于此任务:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFolder=D:\WCR"
del "%SourceFolder%\SecondLines.log" 2>nul

for %%I in ("%SourceFolder%\*.txt") do call :ProcessFile "%%I"

endlocal
goto :EOF

:ProcessFile
for /F usebackq^ skip^=1^ delims^=^ eol^= %%L in (%1) do (
    >>"%SourceFolder%\SecondLines.log" echo %~nx1: %%L
    goto :EOF
)
goto :EOF

请注意命令 FOR 总是忽略空行。所以如果第二行是空行,则将下一个非空行写入LOG文件。

日志文件在源目录中创建。因此,重要的是日志文件没有文件扩展名txt。

要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

  • call /?
  • del /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • set /?
  • setlocal /?

另见:

【讨论】:

    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多