Page 1 of 1

File Write Question

Posted: Sun Oct 04, 2015 4:51 pm
by CoreySchroeder
To make a long story short - I've got an idea for a large scale application that I would like to develop, and right now I'm creating little mini apps to learn the code behind the functionality of my big app idea.
I'm currently testing out file creation, and have a few questions -

Right now, I've got a text field that the user can input information, and save it to a file called "test.txt" on the users desktop.
My code is:

Code: Select all

command cmdSave
    open file specialFolderPath("desktop") & "/test.txt" for write
    put field "fldResult" into myText 
    write myText to file specialFolderPath("desktop") & "/test.txt" 
    close file specialFolderPath("desktop") & "/test.txt" 
end cmdSave
A) How can I check to see if there is a specific folder present in the specialFolderPath? - For example, I want to see if there is a folder called "ExampleFolder" on the desktop.
If that folder exists, save the text file in that folder. If the folder doesn't exist, create it, then save the file in that folder.

B) How can I use a variable from within my application as the file name used when saving the file?
Lets say I've got a variable "varName" that contains the string "John" - how can I use this information to save the file as "John.txt"?

C) Is it possible to create a custom file extension? This isn't something vital to my application idea, just something I am curious about.
So instead of creating a text file in a .txt format - maybe I want to save the text as a .xyz file or something like that - have it still be a text file, but appear to have a proprietary file format, if that makes sense.

Re: File Write Question

Posted: Sun Oct 04, 2015 5:04 pm
by Klaus
Hi Corey,
CoreySchroeder wrote:

Code: Select all

command cmdSave
    open file specialFolderPath("desktop") & "/test.txt" for write
    put field "fldResult" into myText 
    write myText to file specialFolderPath("desktop") & "/test.txt" 
    close file specialFolderPath("desktop") & "/test.txt" 
end cmdSave
for smaller files (up to several MBs!) you can use the shorter URL syntax:

Code: Select all

command cmdSave
    put field "fldResult" into URL ("file:" & specialFolderPath("desktop") & "/test.txt") 
end cmdSave
:D

Use file: for simple TEXT files and BINFILE: for everything else that is not text.
And always check the dictionary to get more infos about any unknown term!
CoreySchroeder wrote:A) How can I check to see if there is a specific folder present in the specialFolderPath? - For example, I want to see if there is a folder called "ExampleFolder" on the desktop.
If that folder exists, save the text file in that folder. If the folder doesn't exist, create it, then save the file in that folder.
Since we are talking about Livecode, it is of course straightforward like this:
...
put specialfolderpath("desktop" & "/ExampleFolder" into tTargetFolder
if there is a folder tTargetFolder then ## folder exists!
...
:D
CoreySchroeder wrote:B) How can I use a variable from within my application as the file name used when saving the file?
Lets say I've got a variable "varName" that contains the string "John" - how can I use this information to save the file as "John.txt"?
You should take a look at these stacks to learn more of the basics of Livecode:
http://www.hyperactivesw.com/revscriptc ... ences.html

Simply "build" the correct string, & is your friend here:
...
put specialfolderpath("desktop" & "/" & varName & ".txt" into tFileName
...
CoreySchroeder wrote:C) Is it possible to create a custom file extension? This isn't something vital to my application idea, just something I am curious about.
So instead of creating a text file in a .txt format - maybe I want to save the text as a .xyz file or something like that - have it still be a text file, but appear to have a proprietary file format, if that makes sense.
You can use everything to your saved files as a suffix, that does not change the "nature" of the file :D


Best

Klaus