batch append not working - DOS

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
FlyingJenny2bucks
Posts: 22
Joined: 2015-03-01T19:40:59-07:00
Authentication code: 6789

batch append not working - DOS

Post by FlyingJenny2bucks »

Hi, is there something wrong with my code below ? (6.9.0 .Q16.win 7)

Code: Select all

for %%a in (*.jpg) do convert ^
 ( Blues\%%~na-007-a.jpg Blues\%%~na-007-b.jpg +append ) ^
 ( Blues\%%~na-007-c.jpg Blues\%%~na-007-d.jpg +append ) ^
 ( Blues\%%~na-007-e.jpg Blues\%%~na-007-f.jpg +append ) ^
 ( Blues\%%~na-007-g.jpg Blues\%%~na-007-h.jpg +append ) ^
 ( Blues\%%~na-007-i.jpg Blues\%%~na-007-j.jpg +append ) ^
 ( Blues\%%~na-007-k.jpg Blues\%%~na-007-l.jpg +append ) ^
 ( Blues\%%~na-007-m.jpg Blues\%%~na-007-n.jpg +append ) ^
 ( Blues\%%~na-007-o.jpg Blues\%%~na-007-p.jpg +append ) ^
 -background grey -append Done\%%~na.jpg
The result should be

A C E G I K M O
B D F H J L N P


but instead it does this

A B C D E F G H I J K L M N O P
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: batch append not working - DOS

Post by snibgo »

I'm surprised it gives

A B C D E F G H I J K L M N O P

I would expect:

A B
C D
E F
G H
I J
K L
M N
O P

That's what I get, with v6.9.1-0 on Win 8.1.

Anyhow, you have +append and -append the wrong way round. See http://www.imagemagick.org/script/comma ... php#append
snibgo's IM pages: im.snibgo.com
FlyingJenny2bucks
Posts: 22
Joined: 2015-03-01T19:40:59-07:00
Authentication code: 6789

Re: batch append not working - DOS

Post by FlyingJenny2bucks »

The sad part is it still isn't working. I have reverted to two separate steps to get the desired effect.

A C E G I K M O
B D F H J L N P

Code: Select all

For %%a in (*.jpg) do  (
 convert Blues\%%~na-007-[a-b].jpg -append Done\%%~na001.jpg
 convert Blues\%%~na-007-[c-d].jpg -append Done\%%~na002.jpg
 convert Blues\%%~na-007-[d-e].jpg -append Done\%%~na003.jpg
 convert Blues\%%~na-007-[f-g].jpg -append Done\%%~na004jpg
 convert Blues\%%~na-007-[h-i].jpg -append Done\%%~na005.jpg
 convert Blues\%%~na-007-[j-k].jpg -append Done\%%~na006.jpg
 convert  Done\%%~na00[0-9].jpg  -background grey  +append  Done\%%~na.jpg
 del %%~na00?.jpg
)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: batch append not working - DOS

Post by fmw42 »

should it not be

Code: Select all

for %%a in (*.jpg) do convert ^
 ( Blues\%%~na-007-a.jpg Blues\%%~na-007-b.jpg -append ) ^
 ( Blues\%%~na-007-c.jpg Blues\%%~na-007-d.jpg -append ) ^
 ( Blues\%%~na-007-e.jpg Blues\%%~na-007-f.jpg -append ) ^
 ( Blues\%%~na-007-g.jpg Blues\%%~na-007-h.jpg -append ) ^
 ( Blues\%%~na-007-i.jpg Blues\%%~na-007-j.jpg -append ) ^
 ( Blues\%%~na-007-k.jpg Blues\%%~na-007-l.jpg -append ) ^
 ( Blues\%%~na-007-m.jpg Blues\%%~na-007-n.jpg -append ) ^
 ( Blues\%%~na-007-o.jpg Blues\%%~na-007-p.jpg -append ) ^
 -background grey +append Done\%%~na.jpg
where I have swapped the + and - appends
FlyingJenny2bucks
Posts: 22
Joined: 2015-03-01T19:40:59-07:00
Authentication code: 6789

Re: batch append not working - DOS

Post by FlyingJenny2bucks »

For some reason, thats not working for me. Do I need a version update?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: batch append not working - DOS

Post by fmw42 »

FlyingJenny2bucks wrote:For some reason, thats not working for me. Do I need a version update?
You can try upgrading to IM 6.9.1.6 and see. I am not a Windows user, so do not know the proper Windows syntax nor wild card usage. But I do not see where %%a is being used in the rest of the command line. Typically in a for loop as you have it, the argument %%a would be the full file name of the images in the folder. As it is, you seem to be reading every image in the folder and trying to process each one. You need do some string filtering of the files to get only the first part of the filename, so that you do not read every image and can just use the first part of the filename and add -a.jpg, -b.jpg, etc in your command lines.

Otherwise, wait for a Windows IM expert to help or confirm.
FlyingJenny2bucks
Posts: 22
Joined: 2015-03-01T19:40:59-07:00
Authentication code: 6789

Re: batch append not working - DOS

Post by FlyingJenny2bucks »

I know this is a real clumsy way of doing it but its simple enough for me to understand.

%%a = original bunch of files with original filenames
%%~na-007-a.jpg = Post processed %%a with "-007-a" suffix added to the filename inside folder "Blues"
%%~na.jpg = appended files with the original filenames (i.e. %%a) inside folder "Done"

I can get all of them to do -append or +append but when I try tiling them using parenthesis, they don't work. My ultimate goal is set conditions for appending horizontally or vertically e.g.

Code: Select all

Set Filesize=1000000
::A/B/C  (if 3 files exists and file "a" is smaller/equal than 1mb, append them vertically)
For %%a in (*.jpg) do  (
 IF EXIST "Blues\%%~na-007-c.jpg" AND "Blues\%%~na-007-a.jpg" LEQ %Filessize%   (
 convert  Blues\%%~na-007-a.jpg  Blues\%%~na-007-b.jpg  Blues\%%~na-007-c.jpg  -background grey  -append  Done\%%~na.jpg
::A/B+C/D+E/F (if 4-6 files exists and file "a" is smaller/equal than 1mb, append them in tiles)
)  ELSE IF EXIST "Blues\%%~na-007-d.jpg" AND "Blues\%%~na-007-a.jpg" LEQ %Filessize%  ( 
 convert  Blues\%%~na-007-a.jpg  Blues\%%~na-007-b.jpg  -background grey  -append  Done\%%~naPART1.jpg
 convert  Blues\%%~na-007-c.jpg  Blues\%%~na-007-d.jpg  -background grey  -append  Done\%%~naPART2.jpg
 convert  Blues\%%~na-007-e.jpg  Blues\%%~na-007-f.jpg  -background grey  -append  Done\%%~naPART3.jpg
 convert Done\%%~naPART1.jpg  Done\%%~naPART2.jpg  Done\%%~naPART3.jpg  -background grey  +append  Done\%%~na.jpg
 del *PART?.jpg
::A B C D E F (if file "a" is larger than 1mb, append horizontally)
)  ELSE  IF EXIST "Blues\%%~na-007-a.jpg" AND "Blues\%%~na-007-a.jpg" GTR %Filessize%   (
 convert  ^
 Blues\%%~na-007-a.jpg  Blues\%%~na-007-b.jpg  ^
 Blues\%%~na-007-c.jpg  Blues\%%~na-007-d.jpg  ^
 Blues\%%~na-007-e.jpg  Blues\%%~na-007-f.jpg  ^
 -background grey  +append  Done\%%~na.jpg
)
)
As usual, it is not working :(
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: batch append not working - DOS

Post by snibgo »

Code: Select all

IF EXIST "Blues\%%~na-007-c.jpg" AND "Blues\%%~na-007-a.jpg" LEQ %Filessize%   (
That isn't valid syntax (for Windows 8.1). "And" isn't a valid keyword. Even if it was, it wouldn't compare the filesize of a file with an integer.

Code: Select all

for %%a in (*.jpg) do convert ^
 ( Blues\%%~na-007-a.jpg Blues\%%~na-007-b.jpg -append ) ^
 ( Blues\%%~na-007-c.jpg Blues\%%~na-007-d.jpg -append ) ^
 ( Blues\%%~na-007-e.jpg Blues\%%~na-007-f.jpg -append ) ^
 ( Blues\%%~na-007-g.jpg Blues\%%~na-007-h.jpg -append ) ^
 ( Blues\%%~na-007-i.jpg Blues\%%~na-007-j.jpg -append ) ^
 ( Blues\%%~na-007-k.jpg Blues\%%~na-007-l.jpg -append ) ^
 ( Blues\%%~na-007-m.jpg Blues\%%~na-007-n.jpg -append ) ^
 ( Blues\%%~na-007-o.jpg Blues\%%~na-007-p.jpg -append ) ^
 -background grey +append Done\%%~na.jpg
The command does use %%a. %%~na expands to the "name" of file %%a, the part after the drive and directory, before the dot-extension.
FlyingJenny2bucks wrote:For some reason, thats not working for me. Do I need a version update?
You might provide some clues. Do you get an error message? Is an image created? What does it look like? Have you tried "-debug all" to see (near the top of the output) exactly what arguments the shell passes to convert?

What does "dir *.jpg" show? Do you have corresponding files, with suffixes "-007-a" etc in directory "Blues\"?
snibgo's IM pages: im.snibgo.com
FlyingJenny2bucks
Posts: 22
Joined: 2015-03-01T19:40:59-07:00
Authentication code: 6789

Re: batch append not working - DOS

Post by FlyingJenny2bucks »

Thanks. Where should I put "-debug all" in the script?

I removed "AND "Blues\%%~na-007-a.jpg" LEQ %Filessize%" and the script (without the parenthesis) is now working. I have checked the files in both directories and they are all named and displayed correctly in dos. I think its the parenthesis but I got around it now.

If I want to use conditions mentioned above, is shell script the best way? I have cygwin installed but avoided using shell scripts thus far because I find the synthax and wildcards too complicated (e.g. you can't simply attach suffixes to a file as easily as in dos) for my level.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: batch append not working - DOS

Post by snibgo »

Change

Code: Select all

convert
to

Code: Select all

convert -debug all
You are using a shell script. Your shell is Windows CMD (also known as DOS). With Cygwin, you also have a shell called bash. You probably have many others from both Windows and Cygwin.

With Cygwin, you also have a massive range of tools (like "stat") available from either Windows CMD or bash.

Bash is certainly far more powerful than CMD, but I hate it. If it had been available on personal computers 30 years ago, perhaps I would love it.

If you have a command:

Code: Select all

if "Blues\%%~na-007-a.jpg" LEQ %Filessize% (
  {blah}
)
then you are actually comparing two strings. The first two characters of each string are "B and 10. The first string is always less than the second.

The following CMD command, in a BAT file, will set BYTES to the number of bytes in the file x.jpg.

Code: Select all

for /F "usebackq" %%B in (`stat --printf=%%s x.jpg`) do set BYTES=%%B
snibgo's IM pages: im.snibgo.com
FlyingJenny2bucks
Posts: 22
Joined: 2015-03-01T19:40:59-07:00
Authentication code: 6789

Re: batch append not working - DOS

Post by FlyingJenny2bucks »

Thank you for the clarifications.
for /F "usebackq" %%B in (`stat --printf=%%s x.jpg`) do set BYTES=%%B
I am uncertain as to what this does. How is it different to below ?

Code: Select all

Set Bytes=%%~za
If I use %%a and %%b at the same time, how would I know they both correspond to the same file at the same time?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: batch append not working - DOS

Post by snibgo »

Set Bytes=%%~za
Ah, yes, I'd forgotten that. Thanks.
If I use %%a and %%b at the same time, how would I know they both correspond to the same file at the same time?
They wouldn't. %%a and %%b are independent variables.
snibgo's IM pages: im.snibgo.com
FlyingJenny2bucks
Posts: 22
Joined: 2015-03-01T19:40:59-07:00
Authentication code: 6789

Re: batch append not working - DOS

Post by FlyingJenny2bucks »

Great but my code below still isn't working. What have I done wrong?

Two conditions:
- %%~na-007-a.jpg exists
- %%~na-007-a.jpg file size is less/equal to 1mb

Code: Select all

Set FILESIZE=1000000
Set ZERO=Blues\%%~na-007-a.jpg
Set BYTE=%%~zZERO%

For %%a in (*.jpg) do (
 IF EXIST Blues\%%~na-007-a.jpg GOTO  APPEND1
)

:APPEND1
IF  %BYTE%  LEQ  %FILESIZE%  (
convert  Blues\%%~na-007-a.jpg  Blues\%%~na-007-b.jpg Blues\%%~na-007-c.jpg Blues\%%~na-007-d.jpg  -background grey  +append  Done\%%~na.jpg
) ELSE (
convert  Blues\%%~na-007-a.jpg  Blues\%%~na-007-b.jpg Blues\%%~na-007-c.jpg Blues\%%~na-007-d.jpg  -background grey  -append  Done\%%~na.jpg
)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: batch append not working - DOS

Post by snibgo »

After setting BYTE, do an "echo %BYTE%". I think you will find it didn't work.

You could do it like this:

Code: Select all

for %%f in (%ZERO%) do set BYTE=%%~zf

echo %BYTE%
snibgo's IM pages: im.snibgo.com
FlyingJenny2bucks
Posts: 22
Joined: 2015-03-01T19:40:59-07:00
Authentication code: 6789

Re: batch append not working - DOS

Post by FlyingJenny2bucks »

Sign..I'm getting this error "...1000000 is unexpected at this time".

Code: Select all

Set FILESIZE=1000000

For %%a in (*.jpg) do (
 Set ZERO=Blues\%%~na-007-a.jpg
 IF  EXIST  %ZERO%  GOTO  APPEND1

:APPEND1
for %%f in (%ZERO%) do set BYTE=%%~zf
IF  %BYTE%  LEQ  %FILESIZE%  (
convert  Blues\%%~na-007-a.jpg  Blues\%%~na-007-b.jpg Blues\%%~na-007-c.jpg Blues\%%~na-007-d.jpg  -background grey  +append  Done\%%~na.jpg
) ELSE (
convert  Blues\%%~na-007-a.jpg  Blues\%%~na-007-b.jpg Blues\%%~na-007-c.jpg Blues\%%~na-007-d.jpg  -background grey  -append  Done\%%~na.jpg
)
)
Post Reply