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
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
)