Check if a certain file exists

kraftwerk's icon

I spend some time browsing through all the file-related objects and I can't find any solution for my simple task:

before my patch opens a .xml file at startup (which contains some user default settings), it should check whether the file exists. Is there a quick way to accomplish this?

Thanks in advance

pdelges's icon

Here is a way:

https://cycling74.com/tools/mxj-filesys-v0-2-3/
but it's also possible to do this in pure Max (it's more fun to compute).

p

dhjdhjdhj's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Here's a simple Javascript approach that I use.

dupecom's icon

there is no need of java or javascript.

Max Patch
Copy patch and select New From Clipboard in Max.

plain max version:

dhjdhjdhj's icon

Cool --- I never thought about that one. Glad you mentioned it --- I suppose the only benefit of my version is that it's just one object instead of four but putting it in an abstraction addresses that.

Max Patch
Copy patch and select New From Clipboard in Max.

I'm curious however as to why you used trigger objects rather than just messages containing 0 and 1 respectively?
E.g.,

dupecom's icon

1) for backwards compatibility

2) less cpu usage.
a message box is an interface object, trigger object not.
not much in this case, but if you have many of them, it will affect performance.

jvkr's icon

> why you used trigger objects

Maybe it's a matter of style; I do it too. Then again, I remember a case in which I didn't anticipate the message object receiving a set message, breaking functionality. With a trigger it is completely unambiguous.

@dupecom nice indeed

dhjdhjdhj's icon

@dupecom
Thanks for the explanation --- I have a lot of message objects in my patchers for such things. Sounds like it will be worth my while to replace them with triggers...particularly where I'm dealing with incoming highspeed OSC messages.

dupecom's icon

btw.
its not my solution. copied from 11olsen. thanx !!
he posted it here:
https://cycling74.com/forums/sharing-does-a-file-or-folder-exist

dhjdhjdhj's icon

It doesn't matter if it's not your solution --- the value of teaching is not about originality.

DRKOSS's icon

The problem with the [absolutepath] object is that searches everywhere on the hard drive - not just a specific folder - would there be a way to search for the file only in the selected folder?

11OLSEN's icon

@dupecom really no need to give me credits for this kind of things. i for myself may have learned this detail about [absolutepath] on the forums.
@drkoss if you feed it an already absolute path it will just check if the file/folder is there and not search anywhere else on the disk. O.

11OLSEN's icon
Max Patch
Copy patch and select New From Clipboard in Max.

no wait, you're right. it will match with other files with the same name in the max search path if the file is not found. just noticed it with a dict loading a file that wasn't in that path.
you can get around that if you compare in and output path. the file only exist in exactly that path if input == output. like that:

i don't know if i like that behaviour with other objects.
A dict, in this case, is loading even if the file doesn't exist because the file is present somewhere else in the search path. that would be ok for relative filenames, but I'm sendind the abs. path with the "read message".

Mattijs's icon

For the archives, here is an alternative method to check if a file exists:

Max Patch
Copy patch and select New From Clipboard in Max.

When trying to check if a package exists, the absolutepath method doesn't work for me since it doesn't seem to be able to handle the Package:/ path well. Instead I use the folder object to list the containing files of the parent folder and compare the incoming file name to the files in the folder.

Simon O'Rorke's icon

A minor disadvantage of @dupecom's all-Max solution is that absolutepath adds an error message to the Max console when the file is not found. But I cannot see the Javascript file in @dhjdhjdhj 's solution. And @mattijs's solution does not work for me in some cases.

So I've developed a Javascript solution myself. The attached patcher is a Max abstraction that is just a wrapper for the attached Javascript file. Save the Javascript file to the same folder as the patcher file.

I'm a beginner at Javascript for Max (though an experienced C# programmer, which is somewhat similar). So I would welcome any tips for making my Javascript code more elegant.

sor.FileExists.js
application/x-javascript 0.71 KB

sor.FileExists.maxpat
text/plain 3.48 KB

Source Audio's icon

"absolutepath adds an error message to the Max console when the file is not found "
never heard of that.
probably you connected print object to it

Simon O'Rorke's icon

Thanks for the comment, Source Audio. It has prompted me to do some more investigation with the absolutepath solution.
I'm not connecting to absolutepath to a print object. It's an error message with a red background, sent to the Max console by absolutepath itself.
The error message, about which I cannot find any useful info, is
"absolutepath: could not convert to pathname"
But it turns out that, at least in the context of the application I'm developing, sometimes the error message appears, sometimes it does not, even when checking for the existence of the same file.

Roman Thilenius's icon

what about reading the first 2 bits with filein?

Simon O'Rorke's icon

Thanks, Roman . I can imagine that using filein along those lines instead of absolutepath would work as the basis for a file existence check. I don't know if it would eliminate the intermittent error message for me. As I have a robust (so far!) Javascript solution, you will understand that I don't plan to try filein myself.

Source Audio's icon

that error I think is not result of not found file,
but because something in the path is not correct.
You can provoke an error by sending a number or float to absolute path.
It would be interesting to see the path that makes it post that error you describe.

Simon O'Rorke's icon

It would be interesting to see the path that makes it post that error you describe.

"E:/Simon/OneDrive/Documents/Music/Software/Max/Projects/Chiffchaff/data/Massive X KK.maxsnap".
This is a Windows file path, but formatted in a way that I understand should be safe for Max, i.e. with / instead of the \ used natively in Windows.
If the file with that path exists, absolutepath always finds it and never writes the error message. If the file does not exists, absolutepath always outputs "notfound", as expected, and only sometimes writes the error message "absolutepath: could not convert to pathname" to the Max console.
The intermittent occurrence of the problematic error message is puzzling. I cannot see what in my Max project or on my computer might account for the variation.

Source Audio's icon

could it be related to OneDrive?

Simon O'Rorke's icon
Source Audio's icon

I never used OneDrive, being Mac user, and for my windows needs,
allways removed it from the system, to keep everything simple as possible.
But I remember that resolving path to OneDrive, in first place
if not located in default location, caused some problems, not to operating system,
but some other backup apps or similar, can't remember really.

Simon O'Rorke's icon

Yes, I've heard of problems with interactions between various applications and not only OneDrive but also other cloud storage services such as Dropbox, when synchronised with computer file systems. So it is plausible that OneDrive might have something to do with the problem I've reported.

Damien Jacquet's icon

Hello all,
I'm experiencing the same problem with absolutepath, when compiled as a standalone, it gives me the following error : absolutepath: could not convert to pathname.
I just use it to open with the browser the documentation of the application I'm building in a html format.
I just give the name of the file, not the path to absolutepath.
The file I'm looking for is located near the .exe.
I don't have the issue when executing the patch within max/MSP, only with the compiled standalone.
- patch + file in the same folder => no problem
- standalone + file in the same folder => error.

what is strange is that I already use the absolutepath object in the same path to locate and open text files that are located in the same folder and it works in the standalone.
It works great with a text file, but not with a .html file. @Cycling74, any idea ?

The workaround for me was the following :
- get the path of the exe with the messages ";max getruntime" and ";max sendapppath"
- store the path of the exe in a "v mainpath" object
- open the html documents by giving the path of the file to the ";max launchbrowser $1" message

Andy Maskell's icon

Don't forget that OneDrive and Dropbox (and most others probably) now support "offline" storeage as a means of saving local disk space. It means that files can be physically removed from your hard drive to save space and only reside in the Cloud, with the cloud manager replacing the directory entry with something that only it understands. OneDrive and Dropbox will then retrieve the file on demand if you try to open it. However, I know that Max will flag up errors if a cloud file is just "probed" to check it's existance rather than opened. I make sure sure that all my Max files are permanently retained locally as well as backed up to the cloud and I have no problems with it.