2016-05-06 62 views
1

我有一個名爲「letters.txt」的文件中的字母列表和一個名爲「LetterPerSample.txt」的文件中每個字母出現的次數列表,兩個文件因此第一行letters.txt的「a」秒有「b」...等,對於SamplePerLetter.txt也是如此,第一行的最大值爲「a」,第二行的最大值爲「b」,並且所以,我想創建一個像這樣的文件列表a_1,a_2,..... a_max.txt,其中max是上面列出的一個數字,每個生成的文件都有它自己寫在裏面的信。所以a_1.txt有「一」裏面寫的,b_5.txt有「B」的書面等使用批處理文件從列表中創建多個文本文件

我做了什麼至今:

@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,所以我的代碼有什麼問題?

+5

恐怕你的問題是相當混亂。發表一個輸入文件的小例子(每行兩到三行)和你想用這種輸入的輸出... – Aacini

+0

我已經改寫了這個問題,希望它有幫助 –

+1

你的問題真的不清楚;你想要做什麼的描述有點亂碼和混亂。目前還不清楚你的代碼是什麼問題;你沒有解釋它如何不按你想要的方式工作。我真的無法理解*,但a和b的出現次數並不相同。即使在編輯之後,我也不知道你在問什麼。 –

回答

0

這種方法不需要在letters.txt文件中的字母是爲了,所以你可以將剛剛在這樣的文件所需的字母:

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

你可以在this post審查陣列在批處理文件的管理。

如果letters.txt文件有總是所有的字母,從az,那麼這個文件包含可以消除冗餘信息:

@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 
    ) 
) 
+0

非常感謝。數組技巧做到了! –

0

你的問題是,要同時讀取兩個文件。這裏有一個竅門可以這樣做:

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

for環(%%a)讀取letters.txt後等一行。 set /p從STDIN(從letterspersample.txt重定向)讀取一行,所以如果for從一個文件讀取行號5,則set /p從另一個文件讀取行號5。

(PS:我懷疑,你echo邏輯是確定似乎很奇怪)

相關問題