Solved Batch File to Copy Specific Lines of Text

January 10, 2013 at 21:22:55
Specs: Windows 7
Hello, I'm trying to create a text file by outputting lines from existing text files using Windows' Command Prompt/Batch files. I'm using the FOR /F command with the "SKIP=x" to find and copy specific lines from a text file. The output is starting with the first SKIP= number, but it doesn't stop until it's finished echoing the entire file. I need it to ECHO one line, stop, find the next line, ECHO new line, stop, etc... until all 7 lines are copied to the new file. Then, it should insert a space (ECHO.) and start the next file.

Here's what I've got:

for %%i in ("*.doc") do (
echo %%i >> lines.txt
for /f "skip=493 tokens=1 delims=" %%a in (%%i) do echo %%a >> lines.txt
for /f "skip=7 tokens=1 delims=" %%a in (%%i) do echo %%a >> lines.txt
for /f "skip=9 tokens=1 delims=" %%a in (%%i) do echo %%a >> lines.txt
for /f "skip=567 tokens=1 delims=" %%a in (%%i) do echo %%a >> lines.txt
for /f "skip=588 tokens=1 delims=" %%a in (%%i) do echo %%a >> lines.txt
for /f "skip=589 tokens=1 delims=" %%a in (%%i) do echo %%a >> lines.txt
for /f "skip=590 tokens=1 delims=" %%a in (%%i) do echo %%a >> lines.txt
echo. >> lines.txt

The first ECHO line is to output the original filename (for future reference), then I'm using the FOR /F lines to locate lines by number (not content), and copy just that line into the Lines.txt file.

The input data looks something like this:

<data>
<heading>
<title>
Title
Name
information
date
data


See More: Batch File to Copy Specific Lines of Text

Report •


#1
January 10, 2013 at 22:29:23
@echo off & setlocal
for %%i in ("*.doc") do (
echo %%i >> lines.txt
for %%z in (493 7 9 567 568 569 590) do call :f %%z %%i
>> lines.txt echo.
)
goto :eof
:f
for /f "skip=%1 tokens=*" %%a in (%2) do >> lines.txt echo %%a & goto :eof

Report •

#2
January 10, 2013 at 23:16:27
✔ Best Answer
Thanks for the quick reply, nbrane.
A buddy of mine finally got his version working, which I'll post below, in case anyone's interested. It took him a few days to get it right. Hopefully, this will help the next person in need. Thanks again!

@echo off
REM MAIN PROGRAM
for %%i in (*.doc) do (
echo. >> lines.txt
echo ***** >> lines.txt
echo %%i >> lines.txt
setlocal enableextensions enabledelayedexpansion
set lines=8 562 598 611 625 638 651 674 676 678
set curr=1
for /f "delims=" %%a in ('type %%i') do (
for %%b in (!lines!) do (
if !curr!==%%b echo %%a >> lines.txt
)
set /a "curr = curr + 1"
)
endlocal
)


Report •

#3
January 14, 2013 at 17:46:08
fwiw department, here's another approach:
(@for /f "tokens=2* delims=[]" %%a in ('find /v /n "" ^<test.t ^|findstr /b "^\[8\] ^\[562\] ^\[598\]"') do @echo %%a) > lines.txt

Report •

Related Solutions


Ask Question