Page 1 of 1

Batch script help (Windows)

Posted: 2014-10-18T13:29:38-07:00
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)

Re: Batch script help (Windows)

Posted: 2014-10-18T19:24:50-07:00
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".

Re: Batch script help (Windows)

Posted: 2014-10-20T08:03:29-07:00
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!

Re: Batch script help (Windows)

Posted: 2014-10-20T08:37:37-07:00
by snibgo
How did I put a "DO" in there? I don't know. It is wrong. Remove it. Sorry.

Re: Batch script help (Windows)

Posted: 2014-10-20T11:32:44-07:00
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!

Re: Batch script help (Windows)

Posted: 2014-10-20T12:22:40-07:00
by snibgo
It needs a tilde "~" thus: convert "%1" -resize 50%% "\small\%~nx1"

Re: Batch script help (Windows)

Posted: 2014-10-21T05:11:50-07:00
by sTi
snibgo wrote:It needs a tilde "~" thus: convert "%1" -resize 50%% "\small\%~nx1"
I'm afraid it still doesn't work:
Image

Re: Batch script help (Windows)

Posted: 2014-10-21T05:36:38-07:00
by snibgo
That's the message you'll get if the directory \small doesn't exist. Does it? Can you write to it?

Re: Batch script help (Windows)

Posted: 2014-10-21T05:50:39-07:00
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.

Re: Batch script help (Windows)

Posted: 2014-10-21T05:54:21-07:00
by snibgo
"\small" must exist at the root, not as a sub-directory. That's what the first "\" means.

Re: Batch script help (Windows)

Posted: 2014-10-21T06:10:36-07:00
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?

Re: Batch script help (Windows)

Posted: 2014-10-21T06:49:52-07:00
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".

Re: Batch script help (Windows)

Posted: 2014-10-21T12:21:49-07:00
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!

Re: Batch script help (Windows)

Posted: 2014-10-22T02:06:03-07:00
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