Batch Guru needed?

Associate
Joined
28 May 2021
Posts
1,503
Location
St Albans
I am trying to process some archives (comic CBR/CBZ files which are basically RAR/ZIP files) to convert from CBR to CBZ and remove some unwanted bits at same time

1) Unpack to temp dir
2) If this creates a sub directory, move all contents to the root processing directory
3) Delete some unwanted files & filetypes
4) Repack in new archive format

1 & 3 are easy
2 I can't get working
4 works but using %1 I end up with FILENAME.CBR getting processed to FILENAME.CBR.CBZ

Code:
@ECHO OFF

rem unpack (WORKS)
"C:\Program Files\7-Zip\7z.exe" e %1 -oC:\Temp -r

rem move all files from any folders to root UNFINISHED
rem cd c:\temp
rem dir /b /AD-H >> output  I can list the extra DIR it creates but???
rem move output /s c:\temp   OBV doesnt work like this
rem delete output  Just need to tidy up after

rem unwanted files
del *.xml
del *.txt

rem Remake CBZ, rename file, move to desktop (WORKS but not great)
"C:\Program Files\7-Zip\7z.exe" a %1.cbz
del *.jpg  REM really just need to empty the temp dir
forfiles /S /M *.CBZ /C "cmd /c rename @file @fname"
forfiles /S /M *.CBR /C "cmd /c rename @file @fname.cbz"
move *.cbz %USERPROFILE%\Desktop\

rem pause

rem Still need to be able to drop 10 files onto and have it process them ONE at a time

And ideally I dont want to drop files one at a time onto the BAT file, either groups of CBRs or a folder even better
 
Are you dealing with just one sub-folder or can it create multiple?

You should be able to loop through any directories and do a process on each with something like:

FOR /F "tokens=1" %%F IN ('DIR /AD-H /B') DO (CALL :dodirthing %%F)

:dodirthing
ECHO %1
GOTO :eof

Where %1 is the directory name (without path).

Due to some variable expansion issues depending on DOS feature support you need to call out of the FOR loop to handle each instance for *reasons*.

EDIT: If the extension is always .XXX you can use:

SET outputname=%filename:~0,-4%

to chop the .XXX from the filename.

If you drop multiple files onto a DOS batch file the names are stored in %* in a space separated list which can be parsed with a FOR loop (get a bit complicated with filepaths with spaces in which are surrounded in quotes and some without which are not).

EDIT2: Another option is to use FOR /R to walk the directory tree and just pull the files out to the destination without their original path https://ss64.com/nt/for_r.html
 
Last edited:
Back
Top Bottom