create directory

Dec 21, 2010 at 2:16pm

create directory

Hi Max Developers,

I am writing an external (in C/C++) which is able to save and load (text based) files. I followed the SDK instruction to write the save and load functions so I can give my external either the file name alone or a full path/filename combination. And it works well.

However, I would like to have an additional feature: I would like my object to create the directory given in the full path/filename combination if it doesn’t exist yet. For example if I give my external the message write System:/Users/Me/Desktop/Name/File.json and the folder “Name” doesn’t exist on my desktop, I would like it to create the folder then write the file inside.

I would be ok with a solution which creates the folder outside of my external, prior to sending the write message to my object. But I have read this post about it http://cycling74.com/forums/topic.php?id=24563 and it doesn’t seems to be any way without Java or a custom external. As I am writing my own external too, I’d rather integrate this feature to it than depend on someone else’s code.

Do you have any idea which function(s) I could use in the API to do it ?

Thank’s

#54045
Dec 22, 2010 at 10:08pm
#194521
Dec 23, 2010 at 11:14am

Hi,

Thank’s Timothy
I’ll have a look and try to find my way around…

I’ll let you know.

Cheers

#194522
Dec 23, 2010 at 1:18pm

Hi there,

Thank’s to you code Timothy, I think I have the behavior I wanted…
Here is my code :

if (s == gensym(""))
		{      // if no argument supplied, ask for file
			if (saveasdialog_extended(filename, &newpath, &outtype, &filetype, numtypes))     // non-zero: user cancelled
				return;
		}
		else
		{
			strcpy(fullpath, s->s_name);
			path_frompotentialpathname(fullpath, &path, filename);
			foldername = strrchr(fullpath, '/');
			if (foldername)
			{
				*foldername = 0;
				foldername = strrchr(fullpath, '/');
				if (foldername)
				{
					*foldername = 0;
					foldername++;
					err = path_frompathname(fullpath, &path, temp);
					if (!err)
						err = path_createfolder(path, foldername, &newpath);
					if (err)
						object_error((t_object*)x, "error creating folder", err);
				}
				else
					path_frompathname(fullpath, &newpath, temp);
			}
			else
			{
				newpath = path_getdefault();
				strcpy(filename, s->s_name);
			}
		}

Cheers

PS: I have an afterthought, maybe the behavior I wanted isn’t obvious. Here is a bit more information :
I wanted it to be consistent with the usual “write” message which creates a file wherever you say if you give an argument and asks for a place if you don’t. Except that I wanted it to be able to create the last folder if it doesn’t exists. And only the last one (to avoid mistakes).

#194523
Dec 23, 2010 at 5:07pm

Probably a bit of an aside, but it’s pretty easy (in the Mac world at least, and I assume in the Windows world) to lift your utility code into C++ and just use the standard C++ libraries for portable file I/O. You’ll just have to call into the MaxAPI for things like Max path interrogation and conversion between different filename formats.

#194524

You must be logged in to reply to this topic.