Windows 如何使用两个变量change和increment运行命令输出文件名

Windows 如何使用两个变量change和increment运行命令输出文件名,windows,loops,batch-file,scripting,cmd,Windows,Loops,Batch File,Scripting,Cmd,如何在两个变量发生变化的情况下运行命令?一个是递增的,另一个是由某个路径确定的变量 问题是该命令不允许在一个命令中加载多个路径。 我有超过10个文件夹用于每次检查,我必须为每个命令生成一个唯一的文件名,因为它不会被下一个命令覆盖 这里的示例可能更容易理解,但它不起作用,因为没有增量,因此它会覆盖上一个文件 @echo off SET "basename=file" SET /a outname=0 :genloop SET /a outname+=1

如何在两个变量发生变化的情况下运行命令?一个是递增的,另一个是由某个路径确定的变量

问题是该命令不允许在一个命令中加载多个路径。 我有超过10个文件夹用于每次检查,我必须为每个命令生成一个唯一的文件名,因为它不会被下一个命令覆盖

这里的示例可能更容易理解,但它不起作用,因为没有增量,因此它会覆盖上一个文件

    @echo off

    SET "basename=file"
    SET /a outname=0

    :genloop
    SET /a outname+=1

    IF EXIST "%basename%-%outname%.csv" GOTO genloop
    SET "outname=%basename%-%outname%.csv"

    command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-1"
    command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-ABC"
    command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-XYZ"
    command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-ETC"
这个问题与此有关

我怎样才能以简单而优雅的方式做到这一点

提前谢谢

编辑

多亏了@MC ND&@ths,这两种解决方案都能完美地工作,尤其是因为您非常乐于助人,非常细心。我不知道是否有可能批准这两个解决方案,但如果我必须做出选择,我将选择第二个方案,因为它最接近关于这两个变量的问题,并且得到了很好的评论。 我不会忘记@Magoo,因为我感谢他的鼓励,谢谢

PS:考虑到我的问题,这个脚本是完美的,但也许在不久的将来,我会问一个与之相关的新问题,使它在某些场景中更加复杂。(在脚本解释器的可能性中)


PPS:如果我的问题不是很清楚和简洁,我为其他人感到抱歉。请随意修改内容和标记,使其更易于理解。

您基本上已经拥有了大多数必需的组件,我将添加子程序的概念:


“它不起作用”是什么意思?你能描述一下你看到的行为吗?@SLawson我编辑了我问题的内容。谢谢,请提供一个明确的示例,说明如何确定这些“超过10个”目录。它们可能在一个文件中吗?它们的子目录是否与某种模式匹配?(一个三到四个的列表可能就足够了。我们通常可以推断为十个以上。)在每种情况下,您需要什么
outname
?去吧,去死吧!如果你愿意的话,可以要求一些我们年龄的东西。也许
file.anumber.csv
就是我们所能实现的一切——但如果我们不知道你内心的愿望是什么,我们甚至不会去满足你的愿望。你想在指定的目录中生成文件吗?你好@Magoo谢谢:)是的,我想做更多,但这个问题现在对我来说已经够难了。理想情况下,我希望脚本通过关键字字符串搜索确定目录列表,然后在每个目录中执行命令。但现在我会手动操作。不,我不想要在指定目录中生成的文件。@Magoo对于您的问题,不,我不想要在指定目录中生成的文件,而是在批处理启动的地方生成的文件。请使用“dir”Hi命令查看此示例,它只生成两个文件file-1.csv&file-file-2.csv.csv这里是代码,后面是日志ah,因为您使用了outname作为计数变量以及文件名。请参阅更新。只是一个错误,您错过了一个“:”在调用和genloop之间,在这之后,它工作得非常好。再次感谢您的帮助
@echo off

SET "basename=file"
SET /a outnum=0

call :genloop
command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-1"
call :genloop
command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-ABC"
call :genloop
command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-XYZ"
call :genloop
command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-ETC"

goto :eof

:genloop
SET /a outnum+=1

IF EXIST "%basename%-%outnum%.csv" Call :genloop
SET "outname=%basename%-%outnum%.csv"
goto :eof
@echo off
    setlocal enableextensions disabledelayedexpansion

    rem configure script 
    set "basename=file"
    set "counter=1000000"
    set folders="C:\Dir-EXAMPLE-1" "C:\Dir-EXAMPLE-ABC" "C:\Dir-EXAMPLE-XYZ" "C:\Dir-EXAMPLE-ETC" 

    rem Search the last file in the sequence
    for /f "tokens=2 delims=-.0" %%a in ('
        dir /b /a-d /o-n "%basename%-??????.csv" 2^>nul
    ') do set /a "counter=1000000+%%a" & goto done
    :done

    rem Iterate the folders list
    for %%a in (%folders%) do (

        rem increment file counter
        set /a "counter+=1"

        rem get access to the counter with delayed expansion, but
        rem to prevent problems with possible exclamations in paths
        rem disable delayed expansion before executing the commands
        rem so, we need the `for` to store the counter data into %%b

        setlocal enabledelayedexpansion
        for %%b in (!counter:~-6!) do (
            endlocal 

            rem just for testing, generate the output file
            rem remove this line in real code
            type nul >"%basename%-%%b.csv" 

            rem execute the command - command is only echoed to console
            rem if the output is correct, remove the echo
            echo command.exe /out "%basename%-%%b.csv" /LoadDirectory "%%~a"
        )
    )