2017-08-27 18 views
0

所以我想在今年夏天教自己一些基本的編碼,並且我正在爲自己和我的朋友編寫一個程序。我們都喜歡音樂,每隔一段時間我們就會混合歌曲。儘管它不僅僅需要找到好的歌曲組合,我還想編寫一個幫助我們記住組合的程序。如何寫一個批處理文件來查找變量的組合

實際上,我想手動把所有的歌曲組合在一個.txt文件,例如:

song1 song2 
song1 song3 
song2 song4 
song4 song3 

然後,我想創建一個批處理,可以找到例如每結合。 song2並給出這個列表。當我輸入的是song2在這種情況下,我想我的輸出是:

song1 
song4 

我在這下面的嘗試,請注意,我已經寫腳本來創建兩個.txt文件。我很抱歉,如果它是唾棄的,我不瞭解編碼,除了我在網上學到的東西。

:list 
    setlocal EnableDelayedExpansion 
    cls 
    echo With what track would you like to start? 
    echo. 
    Set /P "track= " 
    FIND /I "%track%" combinations.txt >> resultstemp.txt 
    echo. 
    echo --- GOES WITH: --- 
    echo. 
    echo. 
    attrib -h -s %~dp0\resultstemp.txt 
    type %~dp0\resultstemp.txt 
    break>resultstemp.txt 
    attrib +h +s %~dp0\resultstemp.txt 
    echo. 
    echo. 
    pause 
    cls 

的電流輸出爲:

With what track would you like to start? 

song2 

---goes with: --- 

------------Combinations.txt 
song2 song1 
song4 song2 

Press any key to coninue . . . 

雖然我希望它是:

With what track would you like to start? 

song2 

---goes with: --- 


song1 
song4 

Press any key to coninue . . . 

回答

0

編輯:我修改了代碼,以便管理歌曲,包括空格。

@echo off 
setlocal 

echo With what track would you like to start? 
echo/ 
set /P "track=" 
echo/ 
echo ---goes with: --- 
echo/ 
for /F "delims=" %%a in ('FIND /I "%track%" ^< combinations.txt') do (
    for %%b in (%%a) do (
     if "%%~b" neq "%track%" echo %%~b 
    ) 
) 

如果你有關於這個代碼的任何問題,你可能會刪除@echo off線,看看到底是執行了什麼......

例如,在輸入文件中使用這樣的數據:

"Love me do Insomnia" song2 
"Love me do Insomnia" "Another song" 
song2 song4 
song4 "Another song" 

...這是一個輸出例子:

C:\Users\Antonio\test> test.bat 
With what track would you like to start? 

song2 

---goes with: --- 

Love me do Insomnia 
song4 

C:\Users\Antonio\test> test.bat 
With what track would you like to start? 

Another song 

---goes with: --- 

Love me do Insomnia 
song4 
+0

這似乎工作的偉大,直到歌曲包含SP尖子。當我寫作「愛我失眠」(或任何其他)時,當我給失眠作爲輸入時,我會得到愛,我會分開做。任何方式繞過這個在你的方法? –

+0

看我的編輯... – Aacini

相關問題