Search code examples
batch-fileimagemagickmogrify

Problem with .BAT script on imagemagick 7.0.8-25 creating LOG of files converted and TIMESTAMPS


I'm working on a script that finds thousands of image files even in subfolders, (I have different types of mogrify sentences, for the different types) is supposed to create a .log file and insert everything there, included the current date and time.

So that's my problem, I can't make it work and I'll try a lot of things for do it, and I read literally all topics about that, but I can't find my error, and the debugger doesn't says nothing to me.

My batch file is:

@ECHO OFF
SET LOGFILE=comprimir.log
call :LOG > %LOGFILE%
exit /B
:LOG
FOR /r "C:\imagemagick\image" %a in (*.jpg) do
mogrify -quality 90 -interlace PLANE -sampling-factor 4:2:0 -compress 
JPEG2000 -colorspace RGB "%~a"
ECHO %DATE% %TIME%

But something is wrong, when I'll try to execute I get this error:

"Is not expected in this moment"

Why am I getting this error?


Solution

  • When you are inside a batch file, you should use %%A instead of %A you use. Also, where are your parenthesis?

    Another problem, is that you are in a code block (for loop) and you are using %DATE% and %TIME% without delayed expansion. This won't work.

    So, I would suggest:

    @echo off
    setlocal EnableDelayedExpansion
    
    set "logfile=comprimir.log"
    (call :log)>%logfile%
    exit /B
    
    :log
    for /R "C:\imagemagick\image" %%A in (*.jpg) do (
        mogrify -quality 90 -interlace PLANE -sampling-factor 4:2:0 -compress JPEG2000 -colorspace RGB "%%~A"
        echo !date! !time!
    )