File Handling

For general discussion related FlowStone
S1User
Posts: 58
Joined: Thu Sep 17, 2015 4:05 pm

File Handling

Post 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.
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: File Handling

Post by RJHollins »

Did you try the FIND primitive ?
S1User
Posts: 58
Joined: Thu Sep 17, 2015 4:05 pm

Re: File Handling

Post 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.
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: File Handling

Post 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.
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: File Handling

Post 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
S1User
Posts: 58
Joined: Thu Sep 17, 2015 4:05 pm

Re: File Handling

Post 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
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: File Handling

Post 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. 8-)
S1User
Posts: 58
Joined: Thu Sep 17, 2015 4:05 pm

Re: File Handling

Post by S1User »

Excellent. It works. Thanks a lot RJ.
S1User
Posts: 58
Joined: Thu Sep 17, 2015 4:05 pm

Re: File Handling

Post 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.
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: File Handling

Post 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
Last edited by Nubeat7 on Thu Mar 10, 2016 9:10 pm, edited 2 times in total.
Post Reply