Page 1 of 1

variable assignment

Posted: 15 Aug 2010 15:25
by penguin
Hi all,

I have an unnerving problem with batch scripting... I have these two scripts:
('identify' is part of 'Image Magick' tool suite for image manipulation)

this one works perfectly and prints out the correct image dimensions of all images in the folder:

Code: Select all

    @echo off
    c:
    cd PictureIn

    FOR %%a in (*.jpg) do (

       For /F "Tokens=*" %%J in ('identify -format "%%h" %%a') Do echo %%J
       For /F "Tokens=*" %%I in ('identify -format "%%w" %%a') Do echo %%I
    )

    @echo on
    cd c:\

This one should basically do the same as the above, but store the IM results in a variable inbetween. It doesn' work - seems to be a problem with the variable assignment as it always and only prints out the dimensions of the last image in the folder:

Code: Select all

    @echo off
    c:
    cd PictureIn

    FOR %%a in (*.jpg) do (

       For /F "Tokens=*" %%J in ('identify -format "%%h" %%a') Do set dw=%%J
       For /F "Tokens=*" %%I in ('identify -format "%%w" %%a') Do set dh=%%I
       echo %dw%x%dh%
    )

    @echo on
    cd c:\

Any help / ideas / suggestions would be appreciated, I tried about _everything_, resetting the variables first thing in the loop, putting double/single/non % all over the place, putting " instead of ' and vice versa... with funny results, but none that worked. :(

Thanks,
Jo

PS I'm on win XP

Re: variable assignment

Posted: 15 Aug 2010 15:59
by aGerman
It's not clear to me what your script is good for, but it doesn't matter.
The main problem is that the value of regular variables are expandet only once in a command line or block.

You could use EnableDelayedExpansion

Code: Select all

    @echo off
    setlocal EnableDelayedExpansion

    c:
    cd PictureIn

    FOR %%a in (*.jpg) do (

       For /F "Tokens=*" %%J in ('identify -format "%%h" %%a') Do set dw=%%J
       For /F "Tokens=*" %%I in ('identify -format "%%w" %%a') Do set dh=%%I
       echo !dw!x!dh!
    )

    @echo on
    cd c:\

or you could work directly with the dynamic variables of the FOR loops

Code: Select all

    @echo off
    c:
    cd PictureIn

    FOR %%a in (*.jpg) do (
       For /F "Tokens=*" %%b in ('identify -format "%%h" %%a') Do (
          For /F "Tokens=*" %%c in ('identify -format "%%w" %%a') Do (
             echo %%bx%%c
          )
       )
    )

    @echo on
    cd c:\


Regards
aGerman

Re: variable assignment

Posted: 15 Aug 2010 16:14
by penguin
aGerman -

This works perfectly, thank you, thank you, thank you - I was about to go nuts about this!

The script I posted should just print out image dimensions - In the end I want to compare image width to heigth to find out which images are landscape so I can rename them accordingly before rotating them to portrait and putting them through some Photoshop actions... So this variable problem was just a small part of the whole, but an essential one nevertheless.

Again, thanks a bunch, you saved my sanity (even though I don't really get why there are '!' around the variables now, but anyhow, it works.... :)

cheers,
jo

Re: variable assignment

Posted: 15 Aug 2010 16:27
by aGerman
penguin wrote:[...](even though I don't really get why there are '!' around the variables now, but anyhow, it works.... :)[...]

The help for cmd says
If delayed environment variable expansion is enabled, then the exclamation
character can be used to substitute the value of an environment variable
at execution time.

That means %var% is expanded to the value only once in the block (seems it will never change). !var! is expanded each time it is needed.

Regards
aGerman

Re: variable assignment

Posted: 15 Aug 2010 16:36
by penguin
looks like i will need to use the ! later on in the script then, too. Thanks again!