Page 1 of 1

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

Posted: 25 Oct 2013 01:15
by novmar
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

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

Posted: 25 Oct 2013 01:35
by ShadowThief

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
)

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

Posted: 25 Oct 2013 01:56
by novmar
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*)

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

Posted: 25 Oct 2013 02:19
by ShadowThief
okay, I added "delims=" to the first for loop

try it now

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

Posted: 25 Oct 2013 02:26
by Endoro
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

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

Posted: 25 Oct 2013 03:44
by foxidrive
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
)

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

Posted: 25 Oct 2013 05:31
by novmar
Tnx Endoro & foxidrive, now is OK

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

Posted: 31 Oct 2013 17:58
by Squashman
And if the line numbers were truly sequential you coud just use %%A with the mod and get rid of the counter variable.

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

Posted: 31 Oct 2013 18:00
by ShadowThief
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