2013-04-14 20 views
1

我試圖編寫看起來像一個簡單的腳本,但我無法弄清楚。Windows命令行腳本基於用戶定義的起始變量在FOR循環中遞減變量

基本上,一個用戶被問到問題1:「他們想要添加多少(在這種情況下)視頻文件一起創建1個大視頻文件?」

然後用戶被問到問題2:「你想添加的文件的名稱是什麼?」現在這裏是我遇到的問題...

如何創建for循環,詢問問題2在第一個問題中給出的次數並將每個答案保存爲唯一變量(我猜測在遞減的變量)

我從用戶擁有所有正確的文件名後,然後程序將根據視頻程序語法調用視頻程序(THAT語法我不需要幫助,我明白那部分)

ex。 (一個「?」意味着我不知道要放什麼東西在那裏)

關閉@echo

組/ P的howmany =多少個文件,你要補充的嗎?

for /? %%變量(???)in(%howmany%???)do(set/p inputfilename =要添加的第一個文件的名稱是什麼?inputfilename = filename set%howmany%-1 ??? = %howmany%????)

所以如果用戶回答問題1 5,那麼for循環應該問問題5五次,每次給出答案時創建5個唯一變量。 inputfilename1 = movie1.mov inputfilename2 = movie2.mov等..

我一直想弄清楚這幾天..我不能說唱我的頭。之前我已經完成了很多命令,但是這讓我難倒了。我的瀏覽器歷史充滿了谷歌搜索,看起來像人們會問任何類型的文件。如果我在遠離這個問題的地方找到了任何東西,它總是被要求使用不同的編程語言。我的大腦被炸。這甚至有可能嗎?請提前幫助和感謝。

回答

4

雖然馬丁的回答說明瞭如何創建的唯一變量,他沒有解釋如何閱讀他們。當你在談論「將每個答案保存爲唯一變量」時,這裏涉及的概念是ARRAY。您需要使用Delayed Expansion來獲取唯一變量的值(「數組元素」);有關更多詳細信息,請鍵入set /?並查找「延遲擴展」。你可以在這篇文章閱讀中的批處理文件陣列管理的詳細介紹:Arrays, linked lists and other data structures in cmd.exe (batch) script

@echo off 
setlocal EnableDelayedExpansion 

set /p howmany=How many files do you want to add? 
for /L %%i in (1,1,%howmany%) do (
    set /p inputfilename[%%i]=what is the name of the file you want to add? 
) 

rem Process array elements (just show them in this case) 
for /L %%i in (1,1,%howmany%) do (
    echo %%i- !inputfilename[%%i]! 
) 

下面的例子可以幫助你在一個更簡單的方式瞭解陣列管理:

@echo off 
setlocal EnableDelayedExpansion 

rem Create an array of ordinal terms 
set i=0 
for %%a in (first second third fourth fifth sixth) do (
    set /A i+=1 
    set term[!i!]=%%a 
) 
rem Previous FOR is equivalent to: set term[1]=first, set term[2]=second, ... 

set /p howmany=How many files do you want to add? 
for /L %%i in (1,1,%howmany%) do (
    set /p inputfilename[%%i]=what is the name of the !term[%%i]! file you want to add? 
) 

rem Process array elements (just show them in this case) 
for /L %%i in (1,1,%howmany%) do (
    echo The !term[%%i]! file is !inputfilename[%%i]! 
) 
+0

我讀了你的詳細解釋,太棒了!我正在爲數組的描述添加書籤!我會「提高」它,但不幸的是,我還沒有足夠的聲望。 :(我也閱讀了你的評論,我完全同意你的看法,我不明白爲什麼有人認爲抨擊任何編程語言應該被認爲是「有用的」,我非常感謝你花時間幫助潛在的程序員理解如何做事不只是爲他們做,謝謝 – julesverne

+0

+ 1,在大多數情況下,可以使用「數組」,沒有「延遲擴展」或「貼近」它來保留驚歎號,但它確實很慢。 – Endoro

0

你需要這些N個變量嗎?我想你需要傳遞一些文件名到一些腳本/命令行應用程序。

那麼,你不會更好地用一個變量(空格? - )分隔文件名列表嗎?

像:

@echo off 

set /p howmany=How many files do you want to add? 

set list= 

:NEXT 

if %howmany% leq 0 goto END 

set /p inputfilename=what is the name of the first file you want to add? 

set list=%list% "%inputfilename%" 
set /a howmany=%howmany% - 1 
goto NEXT 

:END 

echo %list% 
2

反正回答您的實際問題:

@echo off 

set /p howmany=How many files do you want to add? 

for /L %%i in (1, 1, %howmany%) do (
set /p inputfilename%%i=what is the name of the first file you want to add? 
) 

rem Output the variables to check 
set inputfilename 

輸出:

How many files do you want to add? 3 
what is the name of the first file you want to add? first 
what is the name of the first file you want to add? second 
what is the name of the first file you want to add? third 
inputfilename1=first 
inputfilename2=second 
inputfilename3=third 
+0

謝謝你這麼多應答。我認爲這正是我需要的。謝謝!我將在今晚晚些時候對此進行測試,並讓你知道它是如何發生的。 – julesverne

+0

不客氣!請不要忘記接受答案:) –