使用算术变量在for循环中替换字符串

时间:2015-02-25 07:38:32

标签: batch-file math for-loop replace

尝试生成文件名循环列表,从基本文件开始,然后循环浏览无限数量的“子”文件。例如,程序应该产生:

Layout.txt
SubLayout{1}.txt
SubLayout{2}.txt
SubLayout{3}.txt
Layout.txt
SubLayout{1}.txt
etc. 

存在同名文件的地方。 SubLayout{3}.txt不存在 - 因此循环到原始文件。我遇到了我的字符串替换和算术变量的语法问题,并努力使其工作。代码:

echo off
set _procRig=Layout.txt
set _sysSub=SubLayout{1}.txt

set _origRig=%_procrig%

:MainProceedureLoop
if "%_procRig%" == "%_origRig%" (
        echo Rig = Original Rig
        set _procRig=%_sysSub%
        set /a _subsubcount=1
    ) else (
    echo Rig is not the original
        set /a "_tempsubcount=_subsubcount"
        set /a "_subsubcount+=1"
        set _tempprocRig=%_procRig:{_tempsubcount}={_subsubcount}%
        echo Rig with replacement: [!_tempprocRig!]
        if exist "C:\tests\!_tempprocRig!" (
                echo Rig exists
                set _procRig=!_tempprocRig!
            ) else (
                echo Rig doesn't exist
                set _procRig=%_origRig%
            )
    )

echo Out: [%_procRig%]
pause
goto :MainProceedureLoop

我认为违规行是:

 set _tempprocRig=%_procRig:{_tempsubcount}={_subsubcount}%

原始代码没有,但只是在这里进行了修改以供说明。非常感谢任何帮助。


更新:请参阅Magoos帖子中的评论以获取背景信息。代码更新(仍未解决):

echo off
setlocal enabledelayedexpansion
::Set 
set _procRig=Layout.txt
set _sysSub=SubLayout{1}.txt
set _origRig=%_procrig%
:: Stuff....
:: (_procRig can potentially change during "Stuff" - hence _origRig)
:Loop_For_Example_only
::Dummy conditional:
pause
call :Cycle_filenames
echo Return Vars:
echo _procRig: [%_procRig%]
echo _subsubcount: [%_subsubcount%]
goto :Loop_For_Example_only
exit /b

:Cycle_filenames
setlocal disabledelayedexpansion
echo --------------------------------
echo Vars In:
echo _procRig: [%_procRig%]
echo _origRig: [%_origRig%]
echo _subsubcount: [%_subsubcount%]

if "%_procRig%" == "%_origRig%" (
        echo Rig = Original Rig
        echo Syssub: [%_sysSub%]
        set _procRig=%_sysSub%
        set /a _subsubcount=1
    ) else (
    echo Rig is not the original
        echo sub: %_subsubcount%
        pause
        set /a "_tempsubcount=%_subsubcount%"
        set /a "_subsubcount+=1"
        call set _tempprocRig=%%_procRig:{!_tempsubcount!}={!_subsubcount!}%%
        echo Rig with replacement: [%_tempprocRig%]
        if exist "C:\tests\%_tempprocRig%" (
                echo Rig exists
                set _procRig=%_tempprocRig%
            ) else (
                echo Rig doesn't exist
                set _procRig=%_origRig%
            )
    )
echo Out: [%_procRig%]
endlocal && set _procRig=%_procRig%&& set _subsubcount=%_subsubcount%
goto :eof 

2 个答案:

答案 0 :(得分:1)

由于您已发布"已修订"代码,它有点难以辨别。你的目标不明确。

这条线可以更改为满足您的期望:

CALL set _tempprocRig=%%_procRig:{!_tempsubcount!}={!_subsubcount!}%%

但您需要取消setlocal enabledelayedexpansion

答案 1 :(得分:0)

行。对此采取了不同的措施并使其正常运行。仅适用于99,但可轻松修改为100,1000等...

echo off
setlocal enabledelayedexpansion
::Set 
set _procRig=Layout
set _sysSub=SubLayout{1}
set _origRig=%_procrig%

:Loop_For_Example_only
::Dummy conditional:
pause
call :Cycle_filenames
echo Return Vars:
echo _procRig: [%_procRig%]
echo _subsubcount: [%_subsubcount%]
goto :Loop_For_Example_only
exit /b

:Cycle_filenames
echo --------------------------------
echo Vars In:
echo _procRig: [%_procRig%]
echo _origRig: [%_origRig%]
echo _subsubcount: [%_subsubcount%]

if "%_procRig%" == "%_origRig%" (
        echo Rig = Original Rig
        echo Syssub: [%_sysSub%]
        set "_procRig=%_sysSub%"
        set /a "_subsubcount=1"
    ) else (
    echo Rig is not the original
        echo sub: %_subsubcount%
        set /a "_tempsubcount=%_subsubcount%"
        set /a "_subsubcount+=1"
        if _subsubcount GEQ 9 (
                set _tempprocRig=!_procRig:~0,-3!
                set _tempprocRig=!_tempprocRig!!_subsubcount!}
            ) else (
                set _tempprocRig=!_procRig:~0,-2!
                set _tempprocRig=!_tempprocRig!!_subsubcount!}
            )
        echo Rig with replacement: [!_tempprocRig!]
        if exist "C:\tests\!_tempprocRig!.txt" (
                echo Rig exists
                set _procRig=!_tempprocRig!
            ) else (
                echo Rig doesn't exist
                set _procRig=!_origRig!
            )
    )
echo Out: [%_procRig%]
goto :eof