1

I have a list of letters in a file called "letters.txt" and a list of number of occurrence of each letter in a file called "LetterPerSample.txt",both files are arranged, so first row of letters.txt has "a" second has "b"...etc, and same for SamplePerLetter.txt the first row has max nymber of "a",second has max number of "b" and so in,i want to create a list of files like this a_1,a_2,.....a_max.txt, where max is a number as listed above, and each file generated has it's own letter written inside. So a_1.txt has "a" written inside, b_5.txt has "b" written and so on

what i have done so far is:

@echo off
setlocal enableDelayedExpansion


for /f "tokens=*" %%a in (letters.txt) do (
    set letter=%%a
    for /f "tokens=*" %%b in (SamplePerLetter.txt) do (
    set num=%%b
    for/L %%g IN (1,1,!num!) do (
        set index=%%g
        echo !letter!>letter_labels/!letter!/!letter!!index!.lab
    )
  )
)

sample of the output

a_1.txt
a_2.txt
...
a_10.txt
b_1.txt
b_2.txt
...
b_10.txt

but a and b doesnt have the same number of occurrence in the file LetterPerSample.txt a has 10 and b has 5, so what's wrong with my code?

Community
  • 1
  • 1
  • 5
    I am afraid your question is pretty confusing. Post a small example of the input files (two or three lines each) and the output you want with such input... – Aacini May 06 '16 at 19:28
  • I have rephrased the question, hope it helps – Ahmed Hussein May 08 '16 at 04:44
  • 1
    Your question is really unclear; the description of what you're wanting to do is somewhat garbled and confusing. It's also unclear what the problem is with your code; you've not explained how it isn't working the way you want. I really can't make any sense out of *but a and b doesn't have the same number of occurrence* either. I have no idea what you're asking, even after your edit. – Ken White May 08 '16 at 05:06

2 Answers2

0

your problem is, to read two files simultanioulsly. Here is a trick to do so:

@echo off
setlocal enabledelayedexpansion
<letterpersample.txt (
  for /f %%a in (letters.txt) do (
    set /p num=
    for /l %%i in (1,1,!num!) do (
      echo %%a>letter_labels\%%a\%%a%%i.lab
    )
  )
)

The for loop (%%a) reads one line after the other from letters.txt. set /p reads a line from STDIN (which is redirected from letterspersample.txt), so if for reads line number 5 from one file, set /p reads line number 5 from the other.

(PS: I doubt, your echo logic is ok. Seems odd)

Stephan
  • 53,940
  • 10
  • 58
  • 91
0

This method does not require that the letters in letters.txt file be in order, so you may insert just the desired letters in such file:

@echo off
setlocal EnableDelayedExpansion

rem Load the number of occurrences of each letter from "LetterPerSample.txt" file
set "letters=abcdefghijklmnopqrstuvwxyz"
set "i=0"
for /F %%b in (LetterPerSample.txt) do (
   for %%i in (!i!) do set "number[!letters:~%%i,1!]=%%b"
   set /A i+=1
)

rem Process the letters in "letters.txt" file (in any order)
for /F %%a in (letters.txt) do (
   set "letter=%%a"
   set "num=!number[%%a]!"
   for /L %%g in (1,1,!num!) do (
      set "index=%%g"
      echo !letter!>letter_labels\!letter!\!letter!_!index!.lab
   )
)

You may review the management of arrays in Batch files at this post.

If letters.txt file have always all the letters, from a to z, then this file contain redundant information that can be eliminated:

@echo off
setlocal EnableDelayedExpansion

rem Load the number of occurrences of each letter from "LetterPerSample.txt" file
rem and create the desired files

set "letters=abcdefghijklmnopqrstuvwxyz"
set "i=0"
for /F %%b in (LetterPerSample.txt) do (
   for %%i in (!i!) do set "letter=!letters:~%%i,1!"
   set /A i+=1
   set "num=%%b"
   for /L %%g in (1,1,!num!) do (
      set "index=%%g"
      echo !letter!>letter_labels\!letter!\!letter!_!index!.lab
   )
)
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108