Noob Question: batch conversion + transparency

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?".
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Noob Question: batch conversion + transparency

Post by anthony »

Code: Select all

for /F "usebackq delims=" %%i in (`dir /b %1\*.%srcExt%`) do (
  echo Processing file: "%%~nxi"
  convert "%%~nxi" "%%~ni.%tgtExt%" )
That is interesting to know that for parethesis can group functions.

But how do you separate the parenthesis used as an argument to a "convert" command with the end-bracket needed by the window batch script "FOR" command?

However I checked with Wolfgang Hugemann, the author of the 'Windows Guide' in IM Examples, and he said the better way of doing a recursive sub-directory run was to
use "FOR /R" rather than getting for to process the output of a "dir" command.

See Windows Usage, IM Examples
Batch processing a (sub-)dirctory tree
http://www.imagemagick.org/Usage/windows/#for_recursive

Wolfgang is currently not available, so it may be some time before the parenthesis command grouping for the "FOR" loop can be updated, to remove the need for separate ".bat" files.

I'll make a note of this however for his review when he returns.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
bbstrikesagain

Re: Noob Question: batch conversion + transparency

Post by bbstrikesagain »

I'm not sure about the brackets in convert, but I think you can escape them, ^( or ^), so cmd won't see them as special any more, or they won't be seen as special if they're inside single or double quotes. I haven't tried this yet, as I'm only using IM for some basic things so far, like better PNG compression than IrfanView's batch options. But now I know a little of what IM can do I can see more fun things ahead.

FOR /R is good too, but perhaps not if you only want to process a single level - I didn't want to process any sub-folders.
anthony wrote:

Code: Select all

for /F "usebackq delims=" %%i in (`dir /b %1\*.%srcExt%`) do (
  echo Processing file: "%%~nxi"
  convert "%%~nxi" "%%~ni.%tgtExt%" )
That is interesting to know that for parethesis can group functions.

But how do you separate the parenthesis used as an argument to a "convert" command with the end-bracket needed by the window batch script "FOR" command?

However I checked with Wolfgang Hugemann, the author of the 'Windows Guide' in IM Examples, and he said the better way of doing a recursive sub-directory run was to
use "FOR /R" rather than getting for to process the output of a "dir" command.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Noob Question: batch conversion + transparency

Post by anthony »

bbstrikesagain wrote:FOR /R is good too, but perhaps not if you only want to process a single level - I didn't want to process any sub-folders.
If you are processin on a single level the the "dir" commnd is not needed either!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
bbstrikesagain

Re: Noob Question: batch conversion + transparency

Post by bbstrikesagain »

Yes that maybe true. I can't remember why I went down the back quote dir direction, but I think it had some advantages, probably not realised in the example I used, or maybe I just liked back quotes and braces...
anthony wrote:If you are processin on a single level the the "dir" commnd is not needed either!
bbstrikesagain

Re: Noob Question: batch conversion + transparency

Post by bbstrikesagain »

So, based on the previous suggestions I went back to simpler FOR structures. Then I came up against across one of the reasons I might have used the `DIR` methods instead: passed in or dropped filespec arguments that are UNC paths...

In CMD you can't set a drive and then CD to a path based on a UNC filespec, so normal FOR and FOR /R methods don't work. My solution was to re-invent my UNC tolerant FOR /F ... IN (`dir...`) method, here's some snippets from my latest .cmd:

Only works for non-UNC paths...

Code: Select all

:nonUNC
%~d1 & cd "%~pnx1"
for %traverse% %%i in (*.%srcExt%) do set /a ctrDirTotal+=1
for %traverse% %%i in (*.%srcExt%) do ( set /a ctrDir+=1
  echo Processing file !ctrDir! of %ctrDirTotal%: "%%~fi"
  %%~di & cd "%%~pi" & convert "%%~nxi" "%%~ni.%tgtExt%" && set /a ctrAll+=1
  if .%srcDel%==.y del "%%~nxi" )
Works for UNC or non-UNC arguments...

Code: Select all

:UNC
for /F "usebackq delims=" %%i in (`dir %slashs% /b %1\*.%srcExt% ^2^>%1\%stdErr%`) do set /a ctrDirTotal+=1
for /F "usebackq delims=" %%i in (`dir %slashs% /b %1\*.%srcExt% ^2^>%1\%stdErr%`) do ( set /a ctrDir+=1
  echo Processing file !ctrDir! of %ctrDirTotal%: "%%~fi"
  convert "%%~fi" "%%~dpni.%tgtExt%" && set /a ctrAll+=1
  if .%srcDel%==.y del "%~dpnx1\%%~nxi" )
del %1\%stdErr%
bbstrikesagain wrote:I can't remember why I went down the back quote dir direction
anthony wrote:If you are processin on a single level the the "dir" commnd is not needed either!
Last edited by bbstrikesagain on 2009-07-17T03:33:04-07:00, edited 1 time in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Noob Question: batch conversion + transparency

Post by anthony »

Sorry I don't follow the UNC and nonUNC terms you use.

However I see in other window batch examples things like...

Code: Select all

  %~d1
  CD %~p1
Which seems to be something like -- change drives, then change directory.

Why can't you do this in the FOR loop?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
bbstrikesagain

Re: Noob Question: batch conversion + transparency

Post by bbstrikesagain »

A non-UNC path: "d:\etc..."

A UNC path: "\\hostname\sharename\etc..."
  • %~d evaluates to "\\"
    %~p evaluates to "hostname\sharename\etc..."
Not everyone maps their drives the same, so in a networked environment UNC paths can be more reliable. Explorer can explore network shares directly and will pass on UNC style file/folder paths. If the cmd script receives a UNC path you need to be aware that:
  • (a) you can't set the current drive to a UNC path, and
    (b) since %~p is a relative path you can't CD to that either
FOR works only in the current directory.
FOR /R works down from the current directory or from [[drive:[path]


The FOR /F and `DIR ...` method should work with anything you throw at it ;-)
anthony wrote:Sorry I don't follow the UNC and nonUNC terms you use.

However I see in other window batch examples things like...

Code: Select all

  %~d1
  CD %~p1
Which seems to be something like -- change drives, then change directory.

Why can't you do this in the FOR loop?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Noob Question: batch conversion + transparency

Post by anthony »

Thanks, that makes it clear.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
cnledmart

Re: Noob Question: batch conversion + transparency

Post by cnledmart »

that seems to be a big trouble, :(
Post Reply