Batch script help (Windows)

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?".
Post Reply
sTi
Posts: 7
Joined: 2014-10-18T13:11:18-07:00
Authentication code: 6789

Batch script help (Windows)

Post by sTi »

Hi,
I'm a new user with very little command line and batch script experience, so please excuse my ignorance... but I'm really trying to learn from the IM usage guides. In the Windows usage guide (http://www.imagemagick.org/Usage/windows/), the following example is given for Batch Processing Several Files:

Code: Select all

  %~d1
  CD "%~p1"
  MD small
  FOR %%a in (*.jpg) DO convert %%a -resize 50%% small\%%a
  PAUSE
However, when I create this script in a batch file and drag&drop files to it, no conversion takes place. The "small" subdirectory is created, otherwise the DOS box shows the following and stops doing anything:
Image
What do i have to do to get the script to actually process the files?

Another question:
I presume the script in question converts all .jpg files in the directory. What would I have to change in the script so that it only processes the files that are actually dragged&dropped to it?

Thank you for your help!

P.S.: I'm using IM 6.8.9.8 Q16 x64 on Windows 8 (64 bit)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch script help (Windows)

Post by snibgo »

Your filenames contain spaces. Either remove the spaces or use double-quotes in your commands, eg:

Code: Select all

FOR %%a in (*.jpg) DO convert "%%a" -resize 50%% "small\%%a"
A drag-and-drop script might look like this:

Code: Select all

:loop
if "%1"=="" (
  echo Finished
  pause
  exit
)
DO convert "%1" -resize 50%% "\small\%nx1"
shift
pause
goto loop
When it does what you want, you can remove each "echo" and "pause".
snibgo's IM pages: im.snibgo.com
sTi
Posts: 7
Joined: 2014-10-18T13:11:18-07:00
Authentication code: 6789

Re: Batch script help (Windows)

Post by sTi »

snibgo wrote:A drag-and-drop script might look like this:

Code: Select all

:loop
if "%1"=="" (
  echo Finished
  pause
  exit
)
DO convert "%1" -resize 50%% "\small\%nx1"
shift
pause
goto loop
When it does what you want, you can remove each "echo" and "pause".
Thanks, the first example that converts all files in a folder works fine now, but the drag-and-drop script doesn't function. If the filename doesn't contain a space, the DOS box claims that the command "DO" could not be found and it does nothing. If the filename does contain spaces, the window opens only momentarily and closes instantly without doing anything. Any idea what could be the problem? Ideally the script should accept spaces in filenames.

Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch script help (Windows)

Post by snibgo »

How did I put a "DO" in there? I don't know. It is wrong. Remove it. Sorry.
snibgo's IM pages: im.snibgo.com
sTi
Posts: 7
Joined: 2014-10-18T13:11:18-07:00
Authentication code: 6789

Re: Batch script help (Windows)

Post by sTi »

snibgo wrote:How did I put a "DO" in there? I don't know. It is wrong. Remove it. Sorry.
Hmm, it still gives an error:
Image
What is more, it still seems to reject file names with spaces (it closes the DOS box immediately), although the script contains the quotation marks ("%1" and "\small\%nx1") . Is there no way around this?

Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch script help (Windows)

Post by snibgo »

It needs a tilde "~" thus: convert "%1" -resize 50%% "\small\%~nx1"
snibgo's IM pages: im.snibgo.com
sTi
Posts: 7
Joined: 2014-10-18T13:11:18-07:00
Authentication code: 6789

Re: Batch script help (Windows)

Post by sTi »

snibgo wrote:It needs a tilde "~" thus: convert "%1" -resize 50%% "\small\%~nx1"
I'm afraid it still doesn't work:
Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch script help (Windows)

Post by snibgo »

That's the message you'll get if the directory \small doesn't exist. Does it? Can you write to it?
snibgo's IM pages: im.snibgo.com
sTi
Posts: 7
Joined: 2014-10-18T13:11:18-07:00
Authentication code: 6789

Re: Batch script help (Windows)

Post by sTi »

snibgo wrote:That's the message you'll get if the directory \small doesn't exist. Does it? Can you write to it?
Yes, it does exist (in the same folder where the file to be processed is located) and is not write-protected.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch script help (Windows)

Post by snibgo »

"\small" must exist at the root, not as a sub-directory. That's what the first "\" means.
snibgo's IM pages: im.snibgo.com
sTi
Posts: 7
Joined: 2014-10-18T13:11:18-07:00
Authentication code: 6789

Re: Batch script help (Windows)

Post by sTi »

snibgo wrote:"\small" must exist at the root, not as a sub-directory. That's what the first "\" means.
Thanks, I didn't know that. It works now, but only for files without spaces in the filename - I thought the "" are meant to ensure that it works with all names?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch script help (Windows)

Post by snibgo »

Putting quotes in the IM command will ensure it works when the filename contains spaces.

The problem will be the drag-and-drop of filenames with spaces. Windows probably quotes these. So you probably need "%~1" instead of "%1".
snibgo's IM pages: im.snibgo.com
sTi
Posts: 7
Joined: 2014-10-18T13:11:18-07:00
Authentication code: 6789

Re: Batch script help (Windows)

Post by sTi »

snibgo wrote:Putting quotes in the IM command will ensure it works when the filename contains spaces.

The problem will be the drag-and-drop of filenames with spaces. Windows probably quotes these. So you probably need "%~1" instead of "%1".
It still closes the DOS window immediately. I searched around a bit, and as far as could find out it seems immensely difficult to drag & drop files that contain both spaces and special characters (as mine would), so I probably should forget about doing this with drag & drop. But thanks anyway!
jaffamuffin
Posts: 59
Joined: 2009-01-30T03:46:08-07:00

Re: Batch script help (Windows)

Post by jaffamuffin »

Set it up to drag and drop a folder . Can be easier that way. So you script will look in the folder for *.jpg or whatever
Post Reply