【问题标题】:Concatenating a list in command prompt在命令提示符下连接列表
【发布时间】:2017-05-25 21:54:50
【问题描述】:

我有一个 Windows 控制台应用程序,它接受一个文件列表作为参数,每个参数用空格分隔,例如 MyApp.exe 1.txt 2.txt 3.txt。我的文件夹除了txt还有其他文件格式,但我只对txt文件感兴趣。我想实现类似MyApp.exe for %%i in (.\*.txt) 的目标。提前致谢。

【问题讨论】:

    标签: batch-file command-line command-line-arguments command-prompt


    【解决方案1】:

    唯一的特点是您需要启用delayed expansion - 这样做:

    @ECHO OFF
    SETLOCAL EnableDelayedExpansion
    FOR %%I IN (.\*.txt) DO SET files=!files! %%I
    MyApp.exe !files!
    

    这将调用:

    MyApp.exe .\1.txt .\2.txt .\3.txt
    

    如果您想去除任何路径信息,请使用%%~nxI 而不是%%I

    @ECHO OFF
    SETLOCAL EnableDelayedExpansion
    FOR %%I IN (.\*.txt) DO SET files=!files! %%~nxI
    MyApp.exe !files!
    

    这将导致:

    MyApp.exe 1.txt 2.txt 3.txt
    

    注意有a limit on the length of the command:

    在运行 Microsoft Windows XP 或更高版本的计算机上,最大长度 您可以在命令提示符下使用的字符串是 8191 字符。

    【讨论】:

    • 也许%%~fI 仅获取问题中指定的文件名?
    • @geisterfurz007:有效点,也许除了那当然是%%~nxI
    • 嗯,是的,德国文档告诉它会扩展为完整的文件名,所以我分心了,但你是对的 :)
    • @zb226:谢谢,你节省了我的时间!
    猜你喜欢
    • 1970-01-01
    • 2018-03-02
    • 2018-12-08
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多