Page 2 of 2

Re: Noob Question: batch conversion + transparency

Posted: 2009-07-12T02:42:50-07:00
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.

Re: Noob Question: batch conversion + transparency

Posted: 2009-07-12T14:38:01-07:00
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.

Re: Noob Question: batch conversion + transparency

Posted: 2009-07-12T20:51:19-07:00
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!

Re: Noob Question: batch conversion + transparency

Posted: 2009-07-13T15:03:54-07:00
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!

Re: Noob Question: batch conversion + transparency

Posted: 2009-07-17T02:45:49-07:00
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!

Re: Noob Question: batch conversion + transparency

Posted: 2009-07-17T03:24:12-07:00
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?

Re: Noob Question: batch conversion + transparency

Posted: 2009-07-17T04:11:25-07:00
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?

Re: Noob Question: batch conversion + transparency

Posted: 2009-07-17T18:15:19-07:00
by anthony
Thanks, that makes it clear.

Re: Noob Question: batch conversion + transparency

Posted: 2009-07-20T23:23:47-07:00
by cnledmart
that seems to be a big trouble, :(