0

I have search any reference regarding my plan to copy every 2 line and them paste it into new file. I found some good reference but it for copy every line not every 2 line you can check here Batch File To Copy Every Line In .txt File And Then Create New .txt File For Each Line

Can i copy every 2 line from .txt file and paste it to another automatic generated .txt file that contain my 2 line from .txt source?

Some example source.txt like this:

mango
orange
apple
grape
pear
banana
papaya
etc.

i want to split it into every 2 line in new .txt file, result like this:

filename new.txt contain:
mango
orange

filename new2.txt contain:
apple
grape

filename new3.txt contain:
pear
banana

any someone to know the answer?

Community
  • 1
  • 1

2 Answers2

1
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "destadd="
SET "added="
FOR /f "delims=" %%a IN (q22022330.txt) DO (
 >>new!destadd!.txt ECHO(%%a 
 IF DEFINED added (SET "added="&SET /a destadd+=1) ELSE (SET added=Y)
 IF !destadd!==1 SET destadd=2
)

GOTO :EOF

I used a file named q22022330.txt for my testing.

Magoo
  • 77,302
  • 8
  • 62
  • 84
1

One way ;

@echo off
setlocal enabledelayedexpansion

set lines_per_file=2

set lineno=1
set mod=
set filenumber=0

for /f "delims=" %%l in (the.file) do (
    set /a mod=!lineno! %% !lines_per_file!
    if !mod! equ 1 set /a filenumber = filenumber + 1

    echo %%l >> file!filenumber!.txt

    set /a lineno=lineno + 1    
)
Alex K.
  • 171,639
  • 30
  • 264
  • 288