Check file name and extension
Hi,
I need to design a patcher that will check a file name and extension typed by the user via a savedialog object. Here are the features I want to achieve :
• the input file name must be terminated by a ".txt" extension and should be composed of standard chars only (authorized by the system, cross-plateform if possible)
• if the extension is different from ".txt" (eg. "MyFile.png"), the patcher will replace it (becomes "MyFile.txt")
• if the input file name has no extension (eg. "MyFile" or "MyFile."), the patcher will append it (becomes "MyFile.txt")
• if no file name is specified but extension exists (eg. ".txt" or even simply "."), the patcher will output "NoName.txt"
I'm pretty sure this job can be achieved quite easily using a regexp object, but I'm not familiar enough with the syntax to do it right.
I have design a classic patcher that seems to work (it contains some simple regexp) but I would appreciate any advice for a better solution. I have tested my patcher with these entries :
• toto.txt (becomes "toto.txt")
• tata.png (becomes "tata.txt")
• tutu. (becomes "tutu.txt")
• titi (becomes "titi.txt")
• .txt (becomes "NoName.txt")
• .jpg (becomes "NoName.txt")
• . (becomes "NameOfTheCurrentFolder.txt" in my case, which is acceptable...)
Thank you in advance !
Guillaume
If you can't rely on user naming the file properly,
than you need even more restrictions, like filtering all
chars that might break the path, like slash or colon and similar problematic
chars in max. ( I would use atoi and zl filter or something similar)
And instead of accepting such input as a single dot or nothing entered
I would pop up alert telling user to input something that makes sense.
file extension counts on the end, so a simple
sprintf symout %s.txt at the end of full path would do.
It also depends if user is allowed to choose the path,
or folder to store files is dictated, in which case save dialog should be replaced
by dialog or something similar.
Thank you for these suggestions, it makes sense and I'm sure it will help !
Another question : I use a folder object to populate a UMenu with all the .txt files contained in the choosen folder. I want to filter ".txt" files strictly, so I have setup my folder object with a "types TEXT" message in order to get the right files list. The problem is that if for example a pdf or a html file is present in the choosen folder, it will appear in the UMenu list, and I don't want that ! By the way, I have noticed a strange thing since with a "types TEXT" filter, pdf files will appear in my UMenu, but at runtime only (in Ableton Live 10, I use Max for Live 8.1.5), in Max it seems to be ok. Is it a bug ?
Do you know any trick in Max to filter file types strictly like I want it to be ? Thx !
This may help you :
Thank you very much PDELGES ! It works like a charm and it's really a more elegant, simple and robust solution than mine. ;)
So instead of proposing an "Open" (file) button to the user, I will implement a "Select Folder" in my GUI and the user will then be able to browse his txt files (hardware synth data indeed) with a UMenu and a couple of Prev/Next buttons. Merci beaucoup ! :)
It seems that email alerts don't get sent in last few days...
--------
Live is a separated app and does not have Max file formats
definitions as Max standalone does.
In init folder inside of resources/C74 there are among others filetype definitions
which are used by Max objects.
Live is probably dumb in that respect, so one can't expect that filetype
definitions work in m4Live devices. (except maybe media files like AIFF or Wave etc)
But as I am not using Live it is just a guess.
You could check the extension using regexp and pass
the filepath only if it is .txt.
----
Regarding input to save dialog - I would make a coll
containing allowed ascii letters and numbers and run user input using
iter - atoi - coll - zl group - itoa to build filtered name.
http://www.asciitable.com/
Than you Source Audio for that points and your suggestions. You are probably right concerning Max for Live, I don't think neither it's a bug. I think I must spend a bit of time to master the regexp syntax, I have found a cool web site for the beginners like me ! :) https://regexone.com/
almost all of that can be done by combinations of
atoi
logic and comparison operators to filter and replace numbers in lists
itoa
concatenate
but it is getting more difficult when - for example a mac user - tries to enter an extension with more or less than 3 characters, or something with two .. points
(and did you know that the 3-character "limit" comes from the filesystem´s auto-segregation and not from the OS?)
illegal in windows are:/
\
?
%*
: (only colon is illegal, ratio and letter colon are not)|
<
space
>
(allowed but only as seperator in cmd.exe)
,;
(allowed but only as seperator in cmd.exe)
(allowed but only as seperator in cmd.exe)
=
find filename extension in string even if it contains more than one fullstop
remove file name extension of unknown lenght (if it has one)
add extension ".txt"

yes, regexp is beside if, expr, vexpr and few more objects rather undocumented
in max and one has to find it's way using that.
Relaying on pure expressions as in other programs is also not guaranteed to work,
because max has it's own rules and limitations.
Similar situation if one tries to run shell scripts...
Thank you guys for this inputs, I will do some tests.
Regards,
Guillaume