Page 1 of 3
File Handling
Posted: Wed Mar 09, 2016 5:01 am
by S1User
Is there a way ti parse the existance of a file with the built in primatives?
Using the text prims to read and write files, I feel the simplicity of it is great but it doesn't seem to return anything. Am I to assume that if it doesn't trigger that keans the file doesn't exist? Would be nice if they returned some useful info.
Any help appreciated.
Re: File Handling
Posted: Wed Mar 09, 2016 9:11 am
by RJHollins
Did you try the FIND primitive ?
Re: File Handling
Posted: Thu Mar 10, 2016 4:24 am
by S1User
Not sure if that would work or not. I need to look to see if a file exists to know if to create it first, without potentially overwriting it. In other languages trying to load a file that doesn't exist returns an error so you can act on it.
I don't see a way to do that here.
Re: File Handling
Posted: Thu Mar 10, 2016 5:19 am
by RJHollins
I think it would ...
The INPUT STRING is a PATH, there is a TRIGGER to start it.
The OUTPUT is an ARRAY [which you probably don't need]
The 'I' output can have a BOOLEAN attached to yield True/False if file exists.
Re: File Handling
Posted: Thu Mar 10, 2016 5:45 am
by Tronic
Can Ruby help?
Code: Select all
if File.exist?("log.txt")
watch 'error', "Sorry - I would be overwriting a file"
else
file = File.open("log.txt","w")
file.write "your text"
file.close
end
Re: File Handling
Posted: Thu Mar 10, 2016 6:40 pm
by S1User
@ RJ: Thanks, I'll give that a try to see if it returns a 0 if the file doesn't exist.
@Tronc: That looks exactly like what I need. I haven't done much Ruby coding but I suppose I'll have to dig in using your example above. Thanks.
FWIW, I'm porting a standalone app to a VST Plugin. So far so good. Screen shot below...
http://i.imgur.com/4al6Yfo.png
Re: File Handling
Posted: Thu Mar 10, 2016 6:50 pm
by RJHollins
@ RJ: Thanks, I'll give that a try to see if it returns a 0 if the file doesn't exist.
That's exactly what it should do.
Thanks to Tronic for the RUBY version.

Re: File Handling
Posted: Thu Mar 10, 2016 7:01 pm
by S1User
Excellent. It works. Thanks a lot RJ.
Re: File Handling
Posted: Thu Mar 10, 2016 7:35 pm
by S1User
Question: Is there any way to set a 'default' button for message boxes? Typically when I code I do that on messages like this, maybe set the default button as "No" (button 2 in this case with Yes/No) so the user doesn't hit ENTER on the keyboard and accidentally overwrite a file.
Here the message box appears to always default to the first button, YES in this case.
Thanks again for helping. You guys are great.
Re: File Handling
Posted: Thu Mar 10, 2016 8:57 pm
by Nubeat7
maybe this could be of interest for you
Code: Select all
File.exists?(file_path) # returns true/false whether a path exists or not.
File.file?(file_path) # Return true if the path exists AND is a file.
File.directory?(file_path) # Returns true if the path exists AND is a folder.
File.delete(file_path1, file_path2, file_path3...) # delete files
File.join(string1, string2, string3...) # Join strings, inserting the backslashes to make a path.
File.size(file_path) #returns filesize
File.extname(file_path) #returns the fileextension
File.rename("old_name", "new_name") #Renames the given file to the new name. Raises a SystemCallError if the file cannot be renamed.
Dir.mkdir(folder_path) # Create a new folder
Dir.entries(folder_path) # Gets an array of all the file names in a given folder
Dir.delete(folder_path) # Delete a folder (but only if it is empty!)
Dir.exists?(folder_path) # returns true if directory exists, otherwise false