2

I use this batch script very often to collate images side by side using ImageMagick, and it serves well the purpose, unless I am facing images whose file names start with digits.

Example:

ThisImage1.JPG
ThisImage2.JPG
...
ThisImage6.JPG

will be first renamed to:

A1.JPG
A2.JPG
...
A6.JPG

then collated 2 by 2 into:

B1.JPG
B3.JPG
B5.JPG

But if the images are originally named like:

111.JPG
112.JPG
...
116.JPG

then the preliminary renaming will be:

A2.JPG
A3.JPG
...
A7.JPG

which is a mistake of course...

Can this be solved?

My batch script:

SETLOCAL EnableDelayedExpansion
SET IMCONV="%PROGRAMFILES%\ImageMagick-7.0.4-Q16\Convert"

SET COUNT=0
setlocal ENABLEDELAYEDEXPANSION
FOR /R %%T IN (*.jpg) DO (
SET /A COUNT=!COUNT!+1
REN %%T A!COUNT!.jpg
)
:EOF

 @echo off
set count=0
for %%x in (*.jpg) do set /a count+=1
set /a demi=%count% / 2

FOR /L %%x IN (1,2,%count%) DO (
set /a y = %%x + 1
%IMCONV% A%%x.jpg A!y!.jpg +append B%%x.jpg
)

endlocal
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Ziad El Hachem
  • 311
  • 3
  • 12
  • Doesn't seem like a mistake; you're trying to increment the last digit in a string and change the rest of the string to something else. The `11` gets changed to an `A`. What output are you expecting? – SomethingDark May 30 '17 at 19:21
  • I am trying to get A1 again, since the later portion of the program needs to collate A1 to A2, not A2 to A3... – Ziad El Hachem May 30 '17 at 19:26
  • Your batch is unclear to me, the first for iterates all jpg's from current folder into all subfolders (and leaves a count). The second for only counts in the current folder. If the calculation of demi should be to single out uneven results you should use `set /A even=count / 2 * 2`. Otherwise the previous naming of the jpg files has no influence on the new naming. –  May 30 '17 at 21:46
  • bash lists files alphabetically, not numerically. So perhaps add leading zeros to your numbered files – fmw42 May 30 '17 at 22:34
  • Instead of using `for [/R] %%T in (*.jpg) do (` to enumerate files which are renamed in the loop, you should use `for /F "delims=" %%T in ('dir [/S] /B /A:-D "*.jpg"') do (`, because otherwise you may run into trouble as `for [/R]` does not fully enumerate all files prior to processing them, but the `for /F` approach does; see [this post](https://stackoverflow.com/q/31975093) for details... – aschipfl May 31 '17 at 16:13
  • @LotPings, changing `demi` to an even number is not necessary as the following `for /L` loop has got a step size of `2` anyway; so the line `set /A demi=%count% / 2` should be omitted... – aschipfl May 31 '17 at 16:19

0 Answers0