[SOLVED] copy every 3 line in new x.txt file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
novmar
Posts: 9
Joined: 09 Oct 2013 11:10

[SOLVED] copy every 3 line in new x.txt file

#1 Post by novmar » 25 Oct 2013 01:15

I have x.txt file and I wont copy every 3 line in new y.txt file

x.txt
1*aaa
2*bb
3*ccc
4*dddd
5*zzzzzz
6*yy
7*dklfmrmfs
8*fmkmmkd
9*lizzzi
10*ttot
11*eefsd
12*cvhrtrr
13*vffwwrr
14* wwerw

and result in y.txt is...
ccc
yy
lizzzi
cvhrtrr

I know how to remove number and * and copy lines to new .txt

Code: Select all

For /f "tokens=1,2 delims=*" %%a in (x.txt) do echo %%b>>y.txt
Last edited by novmar on 25 Oct 2013 05:32, edited 2 times in total.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: copy every 3 line in new x.txt file

#2 Post by ShadowThief » 25 Oct 2013 01:35

Code: Select all

@echo off
setlocal enabledelayedexpansion

set every_num=3

set counter=1
for /f "delims=" %%A in (data.txt) do (
   set /a mod=!counter!%%%every_num%
   if !mod!==0 (
      echo %%A
      for /f "tokens=1,2 delims=*" %%B in ("%%A") do echo %%C>>output.txt
   )
   set /a counter+=1
)
Last edited by ShadowThief on 25 Oct 2013 02:19, edited 1 time in total.

novmar
Posts: 9
Joined: 09 Oct 2013 11:10

Re: copy every 3 line in new x.txt file

#3 Post by novmar » 25 Oct 2013 01:56

tnx this is it, but in x.txt I have line (example)

2*G:\..\....\..\....\xxx - xxxx xxx xxx xxx.mp3

and result is (cut txt line on first space)
G:\..\....\..\....\xxx

I need all line to copy in y.txt (without 2*)

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: copy every 3 line in new x.txt file

#4 Post by ShadowThief » 25 Oct 2013 02:19

okay, I added "delims=" to the first for loop

try it now

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: copy every 3 line in new x.txt file

#5 Post by Endoro » 25 Oct 2013 02:26

to preserve empty lines:

Code: Select all

@ECHO OFF &SETLOCAL
(for /f "delims=" %%a in ('findstr /n "^" "file.txt"') do (
   set "line=%%a"
   set /a cnt+=1
   set /a mod=cnt%%3
   SETLOCAL ENABLEDELAYEDEXPANSION
   set "line=!line:*:=!"
   if !mod! equ 0 echo(!line!
   endlocal
))>output.txt

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: copy every 3 line in new x.txt file

#6 Post by foxidrive » 25 Oct 2013 03:44

This modification of ShadowThief's code works here.

Code: Select all

@echo off
setlocal enabledelayedexpansion

set every_num=3

set counter=1
for /f "tokens=1,* delims=*" %%A in (data.txt) do (
   set /a mod=!counter!%%%every_num%
   if !mod!==0 (
      echo %%B
       >>output.txt echo(%%B
   )
   set /a counter+=1
)

novmar
Posts: 9
Joined: 09 Oct 2013 11:10

Re: copy every 3 line in new x.txt file

#7 Post by novmar » 25 Oct 2013 05:31

Tnx Endoro & foxidrive, now is OK

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: [SOLVED] copy every 3 line in new x.txt file

#8 Post by Squashman » 31 Oct 2013 17:58

And if the line numbers were truly sequential you coud just use %%A with the mod and get rid of the counter variable.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: [SOLVED] copy every 3 line in new x.txt file

#9 Post by ShadowThief » 31 Oct 2013 18:00

Squashman wrote:And if the line numbers were truly sequential you coud just use %%A with the mod and get rid of the counter variable.

I've always had bad luck getting %%A%%%mod% to work properly for some reason

Post Reply