2014-09-27 49 views
0

我在使用批處理文件中的for循環中的表時遇到問題。在Windows批處理中使用循環表

這裏是我的代碼:

@echo off 
SET /P input1=Nombre de camion : 
set nombreDeCamion=%input1% 

for /l %%f in (1,1,%nombreDeCamion%) DO (
    set index=%%f 
    SET /P input=Entrer le no du camion: 
    setlocal EnableDelayedExpansion 
    set tableauNoCamion[%index%]=!input! 
    echo You entered !tableauNoCamion[%index%]! 
    endlocal 
) 

for %%n in (%tableauNoCamion%) DO (
    setlocal enableDelayedExpansion 
    echo %%n 
    endlocal 
) 
pause 

在第二循環中,我想看看我的表是很好居住。

我喜歡用我輸入的用戶輸入的數據填充我的表tableauDeCamion。之後,我想用這個表格最終重命名一些文件。 我已經試過這個/r %%f in (*.jpg) DO來傳遞所有的JPG文件,它運行良好。我如何在該循環中使用表tableauDeCamion

回答

0

您有

set tableauNoCamion[%index%]=!input! 
echo You entered !tableauNoCamion[%index%]! 

%index%將與index內容在當時的for ...%%f解析

不要index都懶得進行評估的一個主要問題;使用

set tableauNoCamion[%%f]=!input! 
echo You entered !tableauNoCamion[%%f]! 

至於第二個循環中,你可以使用

set tableauNoCamion 

for /f "tokens=1*delims==" %%a in ('set tableauNoCamion') do echo %%a 

setlocal enabledelayedexpansion 
for /L %%a in (1,1,%nombreDeCamion%) do echo !tableauNoCamion[%%a]! 
endlocal 

(所有的空氣碼 - 未經)

還有其他 - 取決於你想要的報告。

對於任何你想要做的*.jpg或不可─*.jpg文件 - 這是一個謎,我...


嗯,是的 - 問題是,任何改變的環境做出在達到匹配的endlocalsetlocal撤消後。

熙的修訂......

@echo off 
setlocal EnableDelayedExpansion 
SET /P input1=Nombre de camion : 
set nombreDeCamion=%input1% 

for /l %%f in (1,1,%nombreDeCamion%) DO (
    set index=%%f 
    SET /P input=Entrer le no du camion: 
    set tableauNoCamion[%%f]=!input! 
    echo You entered !tableauNoCamion[%%f]! 
) 
echo=============== 
SET tableaunocamion 
echo=============== 

for /L %%a in (1,1,%nombreDeCamion%) do echo !tableauNoCamion[%%a]! 
echo=============== 

for /f "tokens=2,3delims=[]=" %%a in ('SET tableaunocamion') do echo %%a. %%b 

GOTO :EOF 

注:爲cmd會話中運行的代碼我。如果您從「快捷方式」運行,則需要在戰略上插入pause

這裏有一個樣品的結果:(進入4,11,22,33,44)

Nombre de camion : Entrer le no du camion: You entered 11 
Entrer le no du camion: You entered 22 
Entrer le no du camion: You entered 33 
Entrer le no du camion: You entered 44 
============== 
tableauNoCamion[1]=11 
tableauNoCamion[2]=22 
tableauNoCamion[3]=33 
tableauNoCamion[4]=44 
============== 
11 
22 
33 
44 
============== 
1. 11 
2. 22 
3. 33 
4. 44 
+0

脫線喜, 我想你的解決方案,爲第二循環,但遺憾的是,前兩個給了我這個錯誤。環境變量tableauNoCamion未定義。 第三個給我的錯誤回聲命令被停用 – N3M0QC 2014-09-27 13:52:46