使用批处理文件从列表创建多个文本文件

使用批处理文件从列表创建多个文本文件

本文介绍了使用批处理文件从列表创建多个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为"letters.txt"的文件中有一个字母列表,在一个名为"LetterPerSample.txt"的文件中每个字母的出现次数列表,两个文件都被排列了,所以letters.txt的第一行具有"a",第二具有"b" ...等等,对于SamplePerLetter.txt来说,第一行的最大位数为"a",第二行的最大数量为"b",以此类推,我想创建一个列表像a_1,a_2,..... a_max.txt这样的文件,其中max是上面列出的数字,并且生成的每个文件都在其内部写入了自己的字母.因此,a_1.txt的内部写有"a",b_5.txt的内部写有"b",依此类推

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
    )
  )
)

输出样本

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

但是a和b在文件LetterPerSample.txt中出现的次数不相同,a有10,b有5,那么我的代码有什么问题?

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?

推荐答案

此方法不需要letters.txt文件中的字母顺序,因此您可以在此类文件中仅插入所需的字母:

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
   )
)

您可以在.

如果letters.txt文件始终具有 az的所有字母,则该文件包含可以消除的冗余信息:

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
   )
)

这篇关于使用批处理文件从列表创建多个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 22:34