【问题标题】:Use a table for a loop in windows batch在 Windows 批处理中使用表格进行循环
【发布时间】:2014-09-27 02:56:33
【问题描述】:

我在批处理文件的for 循环中使用表时遇到问题。

这是我的代码:

@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

【问题讨论】:

    标签: windows loops batch-file


    【解决方案1】:

    你有一个大问题

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

    因为%index% 将在for ...%%f解析

    时使用index 的内容进行评估

    根本不用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 文件做什么 - 这对我来说是个谜......


    啊,是的 - 问题是,当达到匹配的 endlocal 时,在 setlocal 之后对环境所做的任何更改都将撤消。

    嘻嘻是改版...

    @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
    

    【讨论】:

    • 嗨 Magoo,我尝试了第二个循环的解决方案,但不幸的是,前两个给了我这个错误。环境变量 tableauNoCamion 未定义。第三个给了我错误 Echo command is deactivated
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 2017-03-25
    • 1970-01-01
    • 2023-03-23
    • 2013-01-25
    • 1970-01-01
    相关资源
    最近更新 更多