如何在两个变量发生变化的情况下运行命令?一个是递增的,另一个是由某个路径确定的变量。
问题是该命令不允许在一个命令中加载多个路径。每次检查我都有超过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"
这个问题与此有关如何为Windows脚本创建唯一的输出文件名?
我怎样才能以简单优雅的方式做到这一点?
提前谢谢。
编辑
感谢@MC ND&这两种解决方案都非常有效,尤其是因为你非常乐于助人,非常细心。我不知道是否有可能批准这两个解决方案,但如果我必须做出选择,我会选择第二个选项,因为它最接近关于这两个变量的问题,并且得到了很好的评论。我不会忘记@Magoo,因为我很感激他的鼓励,谢谢。
附言:考虑到我的问题,剧本是完美的,但也许在不久的将来,我会问一个与之相关的新问题,使它在某些场景中更加复杂。(在脚本解释器的可能性中)
PPS:对不起,如果我的问题不是很清楚和简洁。请随意修改内容和标签,使其更易于理解。
@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"
)
)
您基本上已经拥有了最需要的组件,我将添加子程序的概念:
@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