batch for loop increment value ENABLEDELAYEDEXPANSION

跟風遠走 提交于 2019-12-07 22:42:39

问题


I'm trying to make a simple batch-file to make 7zip-archives from all the files in it's directory.

I want the 7zip-archives to get names like a01.7z, a02.7z, a03.7z...

Apparently incrementing a value in a batch-for-loops isn't easy.

The setlocal ENABLEDELAYEDEXPANSION solution doesn't work on my computer (windows 10, 64-bit)

Someone suggested putting the increment-code in a subroutine:

set /a counter=0
for %%i in (*.*) do (
    call :pass2
    goto :cont
    :pass2
        set /a counter=%counter%+1
        goto :EOF
    :cont
        "c:\Program Files\7-Zip\7z.exe" a a%counter% "%%i"
)

This doesn't work because somehow DOS doesn't understand the final "%%i" anymore and just outputs "%i".

Please teach me how to make a for-loop in batch with a counter.


回答1:


This is the simplest way to generate two-digits numbers, with a left zero:

@echo off
setlocal EnableDelayedExpansion

set /A counter=100
for %%i in (*.*) do (
   set /A counter+=1
   "c:\Program Files\7-Zip\7z.exe" a a!counter:~1! "%%i"
)



回答2:


setlocal enabledelayedexpansion
set /a counter=0
for %%i in (*.*) do (
        set /a counter=!counter!+1
        @echo "c:\Program Files\7-Zip\7z.exe" a a!counter! "%%i"
)

This adds 1 file per zip.



来源:https://stackoverflow.com/questions/36973419/batch-for-loop-increment-value-enabledelayedexpansion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!