Creating a text file from livecode

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
monki
Posts: 59
Joined: Tue Dec 13, 2011 10:56 pm

Creating a text file from livecode

Post by monki » Fri Sep 19, 2014 5:37 am

I know it's possible to read from, and write to, existing text files, but is it possible to take text from a field and use it to create a new text file? If not, could a standalone stack copy an existing text file to the save location and write to that? Would that be the way to create a "new" file?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Creating a text file from livecode

Post by Simon » Fri Sep 19, 2014 5:46 am

Hi monki,
Easy peasy!
put fld "myText" into url "file:myNewFile.txt"
Now that will put it in the current default directory.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

monki
Posts: 59
Joined: Tue Dec 13, 2011 10:56 pm

Re: Creating a text file from livecode

Post by monki » Fri Sep 19, 2014 6:33 am

I never would've thought of "URL". Saw it in the user doc, but thought it only pertained to servers :oops: Anyway, that got it working, thanks

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Creating a text file from livecode

Post by FourthWorld » Fri Sep 19, 2014 3:03 pm

In addition to the URL syntax, you can also work with files using open, write/read, and close, e.g.:

Code: Select all

on SaveFile pData, pFile
   open file pFile for write
   if the result is not empty then
       answer "Couldn't create file "&quote& pFile &quote& "(" & sysError() &")"
       exit to top
   end if
   write pData to file pFile
   close file pFile
end SaveFile
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

monki
Posts: 59
Joined: Tue Dec 13, 2011 10:56 pm

Re: Creating a text file from livecode

Post by monki » Fri Sep 19, 2014 4:15 pm

So, opening, and writing to, a non-existing text file will create a new file?
I actually tried the open, write, close thing for creating files first and it didn't work. Maybe I got some of the syntax wrong.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Creating a text file from livecode

Post by FourthWorld » Fri Sep 19, 2014 5:34 pm

See the error handling block in the example I posted. Many times the issue is an invalid file path, or incorrect permissions for the destination folder. Checking the result will let you know if something went wrong, and using sysError() will provide the OS code related to the issue.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply