cheggman
cheggman

Reputation: 13

batch add leading zeroes filename

I've got a script that I'm using for all kinds of things, including renaming and repacking comics. Variants of this script are used by others as well.

I've been seeing a limitation in repacking comics, however; I've found that some comics don't have the leading zeroes at their pages, which makes the pages appear out of order. So, I've added a part that should add leading zeroes.

The file does the following:

  1. loop over all subfolders of the current folder
  2. rename comic archives to their proper extension (so I can see where it is)
  3. loop over all archives, and extract them to a temporary folder
  4. loop over all files in the temporary folder, and add leading zeroes
  5. repack it as a 7zip
  6. rename the file to a comic book extension

Somehow, it's not renaming the files properly, and the name of the archive appears as a subfolder in the archive. For example: let's repack 'testfolder', which contains images from 1 to 100. It renames, extracts, packs, and renames again, without problem. However, the new archive contains the folder named 'testfolder' in the archive next to the images, which don't have the leading zeroes. I'm not sure what's going on, and I've been fighting with it for a while now, so I thought to put it online (it's a good script to share, anyway). Does anyone have an idea on what's going wrong here?

@ECHO ON
rem mode con: cols=80 lines=60
for /f "delims=" %%F in ('dir /ad/s/b') do (
    cd %%F

    IF EXIST *.cbr (
        RENAME *.cbr *.rar
    )

    IF EXIST *.cbz (
        RENAME *.cbz *.zip
    )

    IF EXIST *.cb7 (
        RENAME *.cb7 *.7z
    )

    FOR %%I IN (*.RAR, *.ZIP *.7Z) DO (
        ECHO Extracting %%I...
        "C:\Program Files\7-Zip\7z.exe" e "%%I" -oC:\TMPPACKDIR\* -y | FIND /V "ing  "

        echo %%~nI
        cd C:\TMPPACKDIR\%%~nI\

        FOR /f "delims=" %%P IN ('dir *.JPG, *.PNG, *.BMP') DO (
            SET %%N = %%P
            SET %%N = 00%%N
            SET %%N = %%N:~-2%
            echo %%P
            echo %%N
            pause
            rename 'C:\TMPPACKDIR\%%~nI\%%P' %%N
        )

        pause

        echo %%F

        cd %%F

        ECHO Repacking
        "C:\Program Files\7-Zip\7z.exe" a -t7z "%%~nI.7z" "C:\TMPPACKDIR\%%~nI*" -mx=9 | FIND /V "ing  "

        IF %ERRORLEVEL% EQU 0 RD /S /Q C:\TMPPACKDIR

        ECHO Renaming new file
        RENAME *.7z *.CB7

        ECHO Removing original file
        DEL "%%I"
        ECHO File %%I is done
    )
)


REM del /f/q "%~0" | exit

Upvotes: 0

Views: 645

Answers (2)

aschipfl
aschipfl

Reputation: 34909

Yes, the problem is in the code portion between cd C:\TMPPACKDIR\%%~nI\ and pause. You are trying to set a for variable reference %%N, which does not work. You need to use a normal environment variable instead, like NAME, for example; you can only do sub-string expansion (like ~-2 in your code) using normal environment variables. In addition, since you are setting and reading the same environment variable within a single block of code, you need to use delayed expansion; otherwise, you would always receive the value present when the entire block is read.

The code portion should look like this:

        cd /D "C:\TMPPACKDIR\%%~nI"

        for /F "delims=" %%P in ('dir /B *.JPG, *.PNG, *.BMP') do (
            set "FILE=C:\TMPPACKDIR\%%~nI\%%P"
            set "NAME=00%%~nP"
            setlocal EnableDelayedExpansion
            set "NAME=!NAME:~-2!"
            rename "!FILE!" "!NAME!%%~xP"
            endlocal
        )

        pause

Upvotes: 1

Compo
Compo

Reputation: 38604

I've removed some unnecessary stuff and made a few changes, (the main error being that which aschipfl has already identified).

FOR /F "DELIMS=" %%F IN ('DIR/AD/S/B') DO (
    PUSHD "%%F"
    IF EXIST *.cbr REN *.cbr *.rar
    IF EXIST *.cbz REN *.cbz *.zip
    IF EXIST *.cb7 REN *.cb7 *.7z
    FOR %%I IN (*.RAR, *.ZIP *.7Z) DO (
        ECHO Extracting %%I...
        "%ProgramFiles%\7-Zip\7z.exe" e "%%I" -o"C:\TMPPACKDIR\*" -y
        PUSHD "C:\TMPPACKDIR\%%~nI"
        FOR %%P IN (*.JPG, *.PNG, *.BMP) DO (
            SET "_N=100%%~nP"
            SETLOCAL ENABLEDELAYEDEXPANSION
            SET "_N=!_N:~-2!"
            REN "%%P" "!_N!%%~xP"
            ENDLOCAL
        )
        POPD
        ECHO Repacking
        "%ProgramFiles%\7-Zip\7z.exe" a -t7z "%%~nI.7z" "C:\TMPPACKDIR\%%~nI*" -mx=9
        IF NOT ERRORLEVEL 1 RD/S/Q "C:\TMPPACKDIR\%%~nI"
        ECHO Renaming new file
        REN "%%~nI.7z" "%%~nI.CB7"
        ECHO Removing original file
        DEL "%%I"
        ECHO File %%I is done
    )
    POPD
)

Things to look into:
Can 7z.exe not just extract .cbr, .cbz & .cb7 directly without renaming them first. In the same way, when repacking once you've provided the file type, -t7z can the file not be given the name "%%~nI.CB7" directly instead of later renaming it.

Upvotes: 1

Related Questions