-3

I have 300 files (.txt) with different name. I want 2nd line of each file copied next to its file name into single text file. Path of the file is D:\WCR\

Please help me with a script to be created in notepad with .Bat extension or commands to be given in cmd

Early response will be appreciated..!!

  • `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" & "%%~ff.sec"` – penknife Sep 29 '18 at 16:27

1 Answers1

1

This batch file can be used for this task:

@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

Please note that command FOR ignores always empty lines. So if the second line is an empty line, the next non empty line is written into the LOG file.

The log file is created in source directory. For that reason it is important that the log file has not file extension txt.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

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

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143