how do you code this [updated]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
renzlo
Posts: 116
Joined: 03 May 2011 19:06

how do you code this [updated]

#1 Post by renzlo » 05 Jun 2011 01:37

hi everyone,

i have a text file with 500 lines (that 500 is not constant, it is dynamically changing,it can be 433,601, etc..)

i want to extract the first 100 lines and then write it in a new text file then extract the next 100, write it again to a next text file, so on and so forth until all the lines of the source text file is written into a new file.

to clarify my post above, this should be the outcome.

sourcefile: tp000001.txt

after being extracted.

tp000001_001.txt -with first 100 lines.
tp000001_002.txt-with the next 100 lines.
tp000001_003.txt -with the next 100 lines.
tp000001_004.txt -with the next 100 lines.
tp000001_005.txt -with the remaining lines.

i know thìs is doable but im totally out of any ideas. Thanks in advance.
Last edited by renzlo on 05 Jun 2011 13:10, edited 1 time in total.

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: how do you code this

#2 Post by Cleptography » 05 Jun 2011 01:49

Hey renzlo,
My approach to this would be a combination of using "for" to grab each line in the file set it as a string and then use "set /a" method to count each line as it goes until it reaches 100 and then redo the same with a new file until it reaches the end sending the remaining data to the last file.
Same would hold true for naming of files given the counting of the preceding zeros that are removed using set /a method, but yes this is very doable.
Jeb, Dave, or Ed will be around to help you with a better method I am sure shortly, something to do with macros, since that seems to be the hot topic of the moment. :P

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: how do you code this

#3 Post by renzlo » 05 Jun 2011 02:59

thanks clep for the reply. I was thinking it that way but i dunno where to start.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do you code this

#4 Post by aGerman » 05 Jun 2011 06:05

Slow, but a possible way:

Code: Select all

@echo off &setlocal

set "sourcefile=tp000001.txt"
set /a lines=100


for %%i in ("%sourcefile%") do (
  set "Fdpn=%%~dpni"
  set "Fx=%%~xi"
)
set /a n=0
set /a base=1000
for /f "tokens=* delims=1234567890" %%a in ('findstr /n "^" "%Fdpn%%Fx%"') do (
  set "line=%%a"
  call :write
)
goto :eof

:write
set /a r=n%%lines, n+=1
if %r%==0 set /a base+=1
if %r%==0 set "outfile=%Fdpn%_%base:~-3%%Fx%"
if %r%==0 >"%outfile%" type nul
setlocal enabledelayedexpansion
>>"%outfile%" echo(!line:~1!
endlocal
goto :eof


Regards
aGerman

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: how do you code this

#5 Post by renzlo » 05 Jun 2011 08:36

thanks aGerman. there are some changes in my problems now.I'll explain it.

my root is set to this drive = c:\TPP

inside that root drive there is a folder named 00002075 (that name is not constant also) with these contents:

Code: Select all

00000001.tif
00000002.tif
00000003.tif
up to ....
00000500.tif
00002075.idx


as you can see it has tif files and idx, that idx is a text file with these contents:

Code: Select all

"30052011","P1700","0000006222","83096713","00000001.tif","1464005305"
"30052011","P1700","0000417617","82339836","00000002.tif","1464005304"
"30052011","P1700","0009130113","11001188452","00000003.tif","1464005303"
"30052011","P1700","0000068037","82339833","00000004.tif","1464005302"
"30052011","P1700","0000015051"," ","00000005.tif","1464005301"
"30052011","P1700","0004192006","84199213","00000006.tif","1464005300"
"30052011","P1700","0009130180","11000516088","00000007.tif","1464005299"
"30052011","P1700","0002252728","77356528","00000008.tif","1464005298"
"30052011","P1700","0009009411","71736775","00000009.tif","1464005297"
"30052011","P1700","0000076552","11000386658","00000010.tif","1464005296"
"30052011","P1700","0009121513","84239232","00000011.tif","1464005295"
"30052011","P1700","0000581497","78361295","00000012.tif","1464005294"
"30052011","P1700"," ","83815334","00000013.tif"," "
"30052011","P1700","0009121613","84239284","00000014.tif","1464005292"
"30052011","P1700","0000016709","82582322","00000015.tif","1464005291"
"30052011","P1700","0000003574","00000810","00000016.tif","1464005289"
"30052011","P1700","0000109836","00001408","00000017.tif","1464005288"
"30052011","P1700","0009306620","00000374","00000018.tif","1464005286"
"30052011","P1700","0009306620","00000373","00000019.tif","1464005285"
"30052011","P1700","0001061128"," ","00000020.tif","1464005283"
"30052011","P1700","0001061128","11001162938","00000021.tif","1464005283"
"30052011","P1700","0009756970","00000090","00000022.tif","1464005282"
"30052011","P1700","0009223020","00057705","00000023.tif","1464005281"
"30052011","P1700","0009074213","11000465301","00000024.tif","1464005280"
"30052011","P1700","0001087092","00000888","00000025.tif","1464005279"
"30052011","P1700","0009282816","00003133","00000026.tif","1464005278"
"30052011","P1700","0001087830","11000601985","00000027.tif","1464005277"
"30052011","P1700","0001005408","11000601982","00000028.tif","1464005276"
"30052011","P1700","0009007541","11000601983","00000029.tif","1464005275"
"30052011","P1700","0010112728","11000601984","00000030.tif","1464005274"
"30052011","P1700","0000009557","84055907","00000031.tif","1464005273"
"30052011","P1700","0001101478","84254183","00000032.tif","1464005272"
"30052011","P1700","0000095778","83931022","00000033.tif","1464005271"
up to...
"30052011","P1700","0001070314","83615870","00000500.tif","1639016940"


I want to move the first 100 tif files to a folder and extract the 100 lines in idx (associated lines) and write it to a new idx file.

This should be the output.

Code: Select all

00002075_001
00002075_002
00002075_003
00002075_004
00002075_005


inside those folders:

Code: Select all

00000001.tif
00000002.tif
00000003.tif
00000004.tif
00000005.tif
00000006.tif
00000007.tif
00000008.tif
00000009.tif
00000010.tif
00000011.tif
00000012.tif
00000013.tif
00000014.tif
00000015.tif
00000016.tif
00000017.tif
00000018.tif
00000019.tif
00000020.tif
00000021.tif
00000022.tif
00000023.tif
00000024.tif
00000025.tif
00000026.tif
00000027.tif
00000028.tif
00000029.tif
00000030.tif
00000031.tif
00000032.tif
00000033.tif
00000034.tif
00000035.tif
00000036.tif
00000037.tif
00000038.tif
00000039.tif
00000040.tif
00000041.tif
00000042.tif
00000043.tif
00000044.tif
00000045.tif
00000046.tif
00000047.tif
00000048.tif
00000049.tif
00000050.tif
00000051.tif
00000052.tif
00000053.tif
00000054.tif
00000055.tif
00000056.tif
00000057.tif
00000058.tif
00000059.tif
00000060.tif
00000061.tif
00000062.tif
00000063.tif
00000064.tif
00000065.tif
00000066.tif
00000067.tif
00000068.tif
00000069.tif
00000070.tif
00000071.tif
00000072.tif
00000073.tif
00000074.tif
00000075.tif
00000076.tif
00000077.tif
00000078.tif
00000079.tif
00000080.tif
00000081.tif
00000082.tif
00000083.tif
00000084.tif
00000085.tif
00000086.tif
00000087.tif
00000088.tif
00000089.tif
00000090.tif
00000091.tif
00000092.tif
00000093.tif
00000094.tif
00000095.tif
00000096.tif
00000097.tif
00000098.tif
00000099.tif
00000100.tif
00002075_001.idx


inside 00002075_001.idx:

Code: Select all

"30052011","P1700","0000006222","83096713","00000001.tif","1464005305"
"30052011","P1700","0000417617","82339836","00000002.tif","1464005304"
"30052011","P1700","0009130113","11001188452","00000003.tif","1464005303"
"30052011","P1700","0000068037","82339833","00000004.tif","1464005302"
"30052011","P1700","0000015051"," ","00000005.tif","1464005301"
"30052011","P1700","0004192006","84199213","00000006.tif","1464005300"
"30052011","P1700","0009130180","11000516088","00000007.tif","1464005299"
"30052011","P1700","0002252728","77356528","00000008.tif","1464005298"
"30052011","P1700","0009009411","71736775","00000009.tif","1464005297"
"30052011","P1700","0000076552","11000386658","00000010.tif","1464005296"
"30052011","P1700","0009121513","84239232","00000011.tif","1464005295"
"30052011","P1700","0000581497","78361295","00000012.tif","1464005294"
"30052011","P1700"," ","83815334","00000013.tif"," "
"30052011","P1700","0009121613","84239284","00000014.tif","1464005292"
"30052011","P1700","0000016709","82582322","00000015.tif","1464005291"
"30052011","P1700","0000003574","00000810","00000016.tif","1464005289"
"30052011","P1700","0000109836","00001408","00000017.tif","1464005288"
"30052011","P1700","0009306620","00000374","00000018.tif","1464005286"
"30052011","P1700","0009306620","00000373","00000019.tif","1464005285"
"30052011","P1700","0001061128"," ","00000020.tif","1464005283"
"30052011","P1700","0001061128","11001162938","00000021.tif","1464005283"
"30052011","P1700","0009756970","00000090","00000022.tif","1464005282"
"30052011","P1700","0009223020","00057705","00000023.tif","1464005281"
"30052011","P1700","0009074213","11000465301","00000024.tif","1464005280"
"30052011","P1700","0001087092","00000888","00000025.tif","1464005279"
"30052011","P1700","0009282816","00003133","00000026.tif","1464005278"
"30052011","P1700","0001087830","11000601985","00000027.tif","1464005277"
"30052011","P1700","0001005408","11000601982","00000028.tif","1464005276"
"30052011","P1700","0009007541","11000601983","00000029.tif","1464005275"
"30052011","P1700","0010112728","11000601984","00000030.tif","1464005274"
"30052011","P1700","0000009557","84055907","00000031.tif","1464005273"
"30052011","P1700","0001101478","84254183","00000032.tif","1464005272"
"30052011","P1700","0000095778","83931022","00000033.tif","1464005271"
"30052011","P1700","0000538449","84121431","00000034.tif","1464005270"
"30052011","P1700","0000026111","00000352","00000035.tif","1464005269"
"30052011","P1700","0000026111"," ","00000036.tif","1464005269"
"30052011","P1700","0000026111","00000353","00000037.tif","1464005268"
"30052011","P1700","0009857539","00000864","00000038.tif","1464005267"
"30052011","P1700","0009707682","00001213","00000039.tif","1464005266"
"30052011","P1700","0009707683","00000370","00000040.tif","1464005264"
"30052011","P1700","0001124539","84252258","00000041.tif","1464005263"
"30052011","P1700","0009009705"," ","00000042.tif"," "
"30052011","P1700","0003233399","83414189","00000043.tif","1464005261"
"30052011","P1700","0009302103","00002063","00000044.tif","1464005259"
"30052011","P1700","0003657751","00001703","00000045.tif","1464005258"
"30052011","P1700","0009928036","11001188758","00000046.tif"," "
"30052011","P1700","0009133128","11001262723","00000047.tif","1464005256"
"30052011","P1700","0009133128","11001262722","00000048.tif","1464005255"
"30052011","P1700","0009122420"," ","00000049.tif","1464005254"
"30052011","P1700","0009130773","83418975","00000050.tif","1464005253"
"30052011","P1700","0001101722","83696957","00000051.tif","1464005252"
"30052011","P1700","0003324153","78782839","00000052.tif","1464005251"
"30052011","P1700","0009801940","11000618007","00000053.tif","1464005249"
"30052011","P1700","0000033600","11000401268","00000054.tif","1464005248"
"30052011","P1700","0000003574","00000809","00000055.tif","1464005247"
"30052011","P1700","0009045138","00024797","00000056.tif","1464005246"
"30052011","P1700","0009130139","84203482","00000057.tif","1464005245"
"30052011","P1700","0000174860","11000618213","00000058.tif","1464005244"
"30052011","P1700","0001103524","00001013","00000059.tif","1464005243"
"30052011","P1700","0009124236","83413256","00000060.tif","1464005242"
"30052011","P1700","0000359900","84199683","00000061.tif","1464005240"
"30052011","P1700","0001070250","83781782","00000062.tif"," "
"30052011","P1700","0009296391","83967856","00000063.tif","1464005238"
"30052011","P1700","0009032529","80911029","00000064.tif","1464005236"
"30052011","P1700","0009045138","00024796","00000065.tif","1464005235"
"30052011","P1700","0009075970","00000111","00000066.tif","1464005234"
"30052011","P1700","0009223020","00057695","00000067.tif","1464005233"
"30052011","P1700","0001124454","00000071","00000068.tif","1464005232"
"30052011","P1700","0009009792","83728811","00000069.tif","1464005231"
"30052011","P1700","0009009792","83728810","00000070.tif","1464005230"
"30052011","P1700","0009832711","82712765","00000071.tif","1463003017"
"30052011","P1700","0009123508","00003321","00000072.tif","1463003016"
"30052011","P1700","0000024329","00001193","00000073.tif","1463003015"
"30052011","P1700","0000230063","FNC4 1K10330400","00000074.tif","1463003014"
"30052011","P1700","0010158338","83997746","00000075.tif","1463003013"
"30052011","P1700","0001783890","11001021004","00000076.tif","1463003012"
"30052011","P1700","0001046115","83962691","00000077.tif","1463003011"
"30052011","P1700","0009788594","83899831","00000078.tif","1463003010"
"30052011","P1700","0009401120","80898166","00000079.tif","1463003009"
"30052011","P1700","0000390119","11001162120","00000080.tif","1463003008"
"30052011","P1700","0000035250","00002339","00000081.tif","1463003007"
"30052011","P1700","0000224592","81137412","00000082.tif","1463003006"
"30052011","P1700","0009130059","11000304800","00000083.tif","1463003005"
"30052011","P1700","0001021153","83574592","00000084.tif","1463003004"
"30052011","P1700","0009314000","00000525","00000085.tif","1463003003"
"30052011","P1700","0009298072","00000565","00000086.tif","1463003002"
"30052011","P1700","0009045023","83574828","00000087.tif","1463003001"
"30052011","P1700","0009045174","11001188133","00000088.tif","1463003000"
"30052011","P1700","0009132516","80198309","00000089.tif","1463002999"
"30052011","P1700","0009306537","00001146","00000090.tif","1463002997"
"30052011","P1700","0009853359","74427598","00000091.tif","1463002996"
"30052011","P1700","0009309213","83996192","00000092.tif","1463002995"
"30052011","P1700","0000046618","84252366","00000093.tif","1463002994"
"30052011","P1700","0000043457","1K00000114","00000094.tif","1463002993"
"30052011","P1700"," "," ","00000095.tif","1463002991"
"30052011","P1700","0000003840","11000635587","00000096.tif","1463002990"
"30052011","P1700","0009757023","83657127","00000097.tif","1463002989"
"30052011","P1700","0000693061","00000217","00000098.tif","1463002988"
"30052011","P1700","0000026111","00000354","00000099.tif","1463002987"
"30052011","P1700","0000452194","00000152","00000100.tif","1463002986"


so on and so forth.

Thanks in advance.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: how do you code this

#6 Post by dbenham » 05 Jun 2011 10:07

Note: I prepared this message before I saw renzlo's response to aGerman
And I must have been half asleep when I wrote this (see red below)

Here is a faster version. It is basically aGerman's code restructured to eliminate the call within the loop.

Code: Select all


@echo off
setlocal enableDelayedExpansion

set "src=tp000001.txt"
set /a lines=100

for %%F in ("%src%") do (
  set "src=%%~fF"
  set "base=%%~dpnF"
  set "ext=%%~xF"
)
set suffix=1001
set "outfile=%base%_%suffix:~1%%ext%"
if exist "%outfile%" del "%outfile%"
set /a cnt=0
setlocal disableDelayedExpansion
for /f "tokens=* delims=1234567890" %%a in ('findstr /n "^" "%src%"') do (
  set "line=%%a"
  set /a "cnt=(cnt+1)%%lines"
  setlocal enableDelayedExpansion
  >>"!outfile!" echo:!line:~1!
  if !cnt!==0 (
    set /a suffix+=1
    set "outfile=%base%_!suffix:~1!%ext%"
    if exist "!outfile!" del "!outfile!"
  )
  for %%s in (!suffix!) do for %%o in (!outfile!) do (
    endlocal
    set "suffix=%%s"
    call set "outfile=%%o"
  )
)
exit /b


Be careful, both this version and aGerman's version will fail if you process a file with more than 999 lines. (Oops - I meant 99,900 lines :oops: )

Here is a version that will work no matter how many lines the file has. It is slower because it must read the file twice, but I think it is still faster than aGerman's code unless the target file is on a very slow storage device.

Code: Select all


@echo off
setlocal enableDelayedExpansion

set "src=tp000001.txt"
set /a lines=100

for %%F in ("%src%") do (
  set "src=%%~fF"
  set "base=%%~dpnF"
  set "ext=%%~xF"
)
for /f "tokens=3 delims=:" %%n in ('find /c /v "" "%src%"') do set suffix=%%n
set /a "suffix=suffix/lines, outSize=suffix*lines"
if %outSize% lss %suffix% set /a "suffix+=1"
for %%d in (1 2 3 4 5 6 7 8 9) do set "suffix=!suffix:%%d=0!"
set suffix=1%suffix:~0,-1%1
set "outfile=%base%_%suffix:~1%%ext%"
if exist "%outfile%" del "%outfile%"

set /a cnt=0
setlocal disableDelayedExpansion
for /f "tokens=* delims=1234567890" %%a in ('findstr /n "^" "%src%"') do (
  set "line=%%a"
  set /a "cnt=(cnt+1)%%lines"
  setlocal enableDelayedExpansion
  >>"!outfile!" echo:!line:~1!
  if !cnt!==0 (
    set /a suffix+=1
    set "outfile=%base%_!suffix:~1!%ext%"
    if exist "!outfile!" del "!outfile!"
  )
  for %%s in (!suffix!) do for %%o in (!outfile!) do (
    endlocal
    set "suffix=%%s"
    call set "outfile=%%o"
  )
)
exit /b


Dave Benham
Last edited by dbenham on 05 Jun 2011 10:42, edited 1 time in total.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: how do you code this

#7 Post by alan_b » 05 Jun 2011 10:16

I suggest starting again.

Replace in the initial requirement every instance of 100 with a little number like 2,
and every instance of 500 with about 8.

I for one have zero inclination to think about viewing screens with 100 lines dedicated to 100 enumerated instances of nnnnnn.tif

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: how do you code this

#8 Post by renzlo » 05 Jun 2011 10:18

hi dave, thanks for the reply. I have tried your edited version of aGerman's code and it is working, but your version of no lines limit has an output error : Divided by zero error.

my latest post above is doable too?

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: how do you code this

#9 Post by renzlo » 05 Jun 2011 10:19

alan_b wrote:I suggest starting again.

Replace in the initial requirement every instance of 100 with a little number like 2,
and every instance of 500 with about 8.

I for one have zero inclination to think about viewing screens with 100 lines dedicated to 100 enumerated instances of nnnnnn.tif


thanks alan_b for the help but i'm not that expert on doing that. Any sample code?

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: how do you code this

#10 Post by alan_b » 05 Jun 2011 10:50

Proposed New Specification

First edit 100 to 2 and then edit 500 to 8 :-

i have a text file with 8 lines (that 8 is not constant, it is dynamically changing,it can be 7,9, etc..)

i want to extract the first 2 lines and then write it in a new text file then extract the next 2, write it again to a next text file, so on and so forth until all the lines of the source text file is written into a new file.

to clarify my post above, this should be the outcome.

sourcefile: tp000001.txt

after being extracted.

tp000001_001.txt -with first 2 lines.
tp000001_002.txt-with the next 2 lines.
tp000001_003.txt -with the next 2 lines.
tp000001_004.txt -with the next 2 lines.
tp000001_005.txt -with the remaining lines.


This results in a much smaller display of results.
To properly see your explanation in your second post I would have needed a monitor as tall as a 3 storey house ! ! !

The two solutions already posted have had at about the third line

Code: Select all

set /a lines=100


To meet the proposed specification they only need to be changed to

Code: Select all

set /a lines=2


And then we can see the results of the scripts on a normal monitor without multiple page up/page down.

When that appears satisfactory it should be a simple matter of changing back to

Code: Select all

set /a lines=100


and if there are any other "fixed constants" that need to be altered they can be made clearly visible on the line(s) following

Code: Select all

set /a lines=2

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: how do you code this

#11 Post by renzlo » 05 Jun 2011 10:52

thanks alan_b,

i am messing around with your idea but still no luck.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do you code this

#12 Post by aGerman » 05 Jun 2011 11:01

Of course I could make the code faster as well. I was not sure whether the file would contain special characters or empty lines ...
How ever. Later I will try to write a code for your new requirement. But before I need to know:
- Where to create the new folders?
- What should happen if you would run the code twice? (exclude the new folders?)
- Are there some rules for the source folder-/file-names? (fixed number of characters or something else?)

Regards
aGerman

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: how do you code this

#13 Post by renzlo » 05 Jun 2011 11:16

- Where to create the new folders?
inside 00002075
- What should happen if you would run the code twice? (exclude the new folders?)
i wont run it twice
- Are there some rules for the source folder-/file-names? (fixed number of characters or something else?)
source folder is always in this format = 00002075 (eight digits) but it is changing, it can increase or decrease (it can be 00002076 or 00003075 or 00012075)

thanks aGerman.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do you code this

#14 Post by aGerman » 05 Jun 2011 12:13

OK, this code will find all subfolders in "C:\TPP" with 8 digits and process the files inside.

Code: Select all

@echo off &setlocal enabledelayedexpansion

set "sourceroot=C:\TPP"
set /a lines=100

cd /d "%sourceroot%" ||goto :eof
set /a n=0, base=1000
for /f "delims=" %%a in ('dir /ad /b^|findstr /rxc:"[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"') do (
  for /f "usebackq tokens=* delims=" %%b in ("%%a\%%a.idx") do (
    set /a r=n%%lines, n+=1
    if !r!==0 (
      set /a base+=1
      set "outfolder=%%a\%%a_!base:~-3!"
      md "!outfolder!"
      set "outfile=!outfolder!\%%a_!base:~-3!.idx"
      >"!outfile!" type nul
    )
    >>"!outfile!" echo %%b
    for /f "tokens=5 delims=," %%c in ("%%b") do move "%%a\%%~c" "!outfolder!\"
  )
)


Regards
aGerman

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: how do you code this

#15 Post by dbenham » 05 Jun 2011 12:19

renzlo wrote:but your version of no lines limit has an output error : Divided by zero error.
The code is working for me. I'm not sure where your divide by zero error is coming from. I don't see how that is possible unless lines is set to 0.

aGerman wrote:Of course I could make the code faster as well. I was not sure whether the file would contain special characters or empty lines ...
The code I posted preserves the ability to handle special characters and blank lines without a call. I never doubted you could do the same if you set your mind to it. I used your method for preserving blank lines - I hadn't seen that variation before.

Dave

Post Reply