Page 1 of 1

Repetitively read x lines from txt file and pass to called program

Posted: 17 Jun 2020 11:39
by Bryan
Hi all,

New to batch programming so...

I have a large text file with repeated sets of lines of data - same number of lines (35) each time.

I wish to create a batch file to repetitively read blocks of 35 lines, passing them to another program that will read them as if from stdin.

Once I get that working, I need to extract the 4 digit number prior to the word Solution on the first line of each set of lines (sample data below), and build a filename from that which will be added to the command line call...

I have got this far in testing - and am happy to move forward step by step.

Code: Select all

@echo off
setlocal DisableDelayedExpansion

set /a lines = 0

for /f "usebackq delims=" %%i in ("icosa v05-5.txt") do (

  echo %%i
  echo %lines%

  set /a lines += 1

  pause
)
But have reached two stumbling blocks.
1/. The line count isn't being incremented (or at least always displays 0).
2/. The second line of every set of my data (see below) is simply the word OFF. When it gets to the second line of any set there is no output of OFF, just press any key to continue... - is it because there is only one element in the line? I would prefer if the line was read complete into one variable.

If I put %%i and %lines% in the one echo statement, it outputs OFF 0... head scratching...

an example of a program I wish to pass the data to if the input was myfile.off

antiview myfile.off

Or, I can run antiview, paste the 35 lines on the command line and type ctrlz enter

When I have it working, there will be a more complicated command line which will still accept the same data, and hopefully, redirect output to a specified file, name of which is created by the batch file as mentioned above.

off2pov [options] [data] > Filename_from_4_Digits.pov

If all goes well, I will then call another program, pvengine64 [options] Filename_from_4_Digits.pov /EXIT to get it rendered to a .png

I realise there may need to be a method of waiting for each *.pov to be created before calling povray.

And, if all goes even better, delete the *.pov files and be left with just the .png's

Sample of data

# 111412135, 1 2, 1 3, 1 5, 1 7, 5, 0002 Solution 1 Seed 1 0.1 0.2 0.1 0.1
OFF
9 22
0.303530999103343 0.525731112119134 0.794654472291766
0.337320057246881 0.326071178562728 0.883115374959396
0.35463739892041 0.110488823890414 0.92845276405549
0.35463739892041 -0.110488823890414 0.92845276405549
0.113725895453813 0.455163328063184 0.883115374959396
0.143959750150644 0.249345601505837 0.957654614851438
0.171848502003268 -2.90837232769237E-17 0.985123389408267
-0.0859242510016338 0.148825168337131 0.985123389408267
-0.0859242510016338 -0.148825168337131 0.985123389408267
3 0 1 4
3 1 2 5
3 1 4 5
3 2 3 6
3 2 5 6
3 5 6 7
3 6 7 8
2 0 1 0
2 0 4 0
2 1 4 1
2 1 2 0
2 2 3 0
2 1 5 0
2 4 5 0
2 2 5 2
2 2 6 0
2 3 6 0
2 5 6 3
2 5 7 3
2 6 7 4
2 6 8 4
2 7 8 4

repeated too many times to have individual files...

Re: Repetitively read x lines from txt file and pass to called program

Posted: 21 Jun 2020 12:21
by pieh-ejdsch
Hallo Bryan,
Your example has only 34 lines ...
I would not read the lines, but always read the first block line.
Look for the 4-digit number and create the file with this number.
Then transfer to the processing program.
The last file is treated at the end
- or the first line of the original file is added to the original file to start processing.

Code: Select all

@echo off
setlocal
set "ext=.pov"
set "fileN="
for /f "usebackQ delims=" %%i in ("icosa v05-5.txt") do (
 set "Line=%%i"
 set "delFile="
 setlocal enableDelayedExpansion
 set "filetest=!line: Solution=\!"
 if !line! neq !filetest! (
  set "delfile=1"
  set "file=!filetest: =.!\.."
  for %%f in ("!file!") do set "file=%%~xf"
 )
 for /f "delims=." %%f in ("!file!") do (
   rem its time to wait for next file
  if defined delfile (
   if defined fileN (
    echo File !fileN! is ready to povray
    pause
   )
   type nul >%%f%ext%
  )
  if :!! equ : endlocal
  set "file=%%f"
  set "fileN=%%f%ext%"
  >>%%f%ext% echo(%%i
 ) 
)

 rem lastfile will be used
echo File %fileN% is ready to povray

pause
Phil