Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch for loop increment value ENABLEDELAYEDEXPANSION

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.

like image 407
Hakkelaar Avatar asked Apr 24 '26 02:04

Hakkelaar


2 Answers

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"
)
like image 113
Aacini Avatar answered Apr 27 '26 05:04

Aacini


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.