Create Directory if One Does Not Exist

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
holden
Posts: 79
Joined: 2013-02-07T08:22:57-07:00
Authentication code: 6789

Create Directory if One Does Not Exist

Post by holden »

It looks like IM won't create a directory, or do anything, if there is a directory name in the command. Eg.

Code: Select all

Convert image.jpg -size 800x1000 C:\home\NewFolder\image.jpg 
If NewFolder doesn't exist then IM acts like it does something but the rendered image or new dir doesn't exist. Is there a way to make IM create a directory if one doesn't exist already, otherwise save into one that does exist already?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Create Directory if One Does Not Exist

Post by fmw42 »

As far as I know, IM will not create a new directory. I think one has to use the OS to script a test if the directory exists or not. Then if not create one. Or decide to use some other default directory. At least I think this is how it works in Unix. I do not know much about Windows.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Create Directory if One Does Not Exist

Post by snibgo »

I suppose you are on Windows. What version of IM?

When I try this on IM v6.8.9-0 on Windows 8.1, I get ...

Code: Select all

F:\web\im>%IM%convert rose: newdir\r.png

convert.exe: unable to open image `newdir\r.png': No such file or directory @ error/blob.c/OpenBlob/2647.
convert.exe: WriteBlob Failed `newdir\r.png' @ error/png.c/MagickPNGErrorHandler/1806.
... which is fair enough.

AFAIK IM won't create directories. If it can't create a file it won't try again somewhere else.

You can:

Code: Select all

if not exist newdir md newdir
snibgo's IM pages: im.snibgo.com
Post Reply