2015-09-11 20 views
0

顯然這個批處理文件應該返回給定爲自變量的輸入文件並置,但它不工作:串聯Windows Shell中不工作

mybatchfile.bat example1.txt example2.txt 

的:

set files= 
for %%i in (%1 %2 %3 %4 %5 %6 %7 %8) do (
    echo %%i 
    set files=%files% %%i 
) 

echo "the file list is %files%" 

時調用預期結果應該是:

example1.txt 
example2.txt 
the file list is example1.txt example2.txt 

但在最後一行中只有「example2.txt」。任何想法???

+1

延遲擴展。 [This](http://stackoverflow.com/a/30177832/2861476)應該有幫助 –

+0

好的,謝謝!現在它正在工作 –

+0

你知道'%*'嗎? (會給你完整的參數列表,只需要'echo%*') – Stephan

回答

1

您需要啓用延遲變量擴展(請注意下面的代碼擴展!files!):

set files= 
setlocal EnableDelayedExpansion 
for %%i in (%1 %2 %3 %4 %5 %6 %7 %8) do (
    echo %%i 
    set files=!files! %%i 
) 
endlocal & set files=%files% 
echo "the file list is %files%"