Creating a text file from livecode
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Creating a text file from livecode
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?
Re: Creating a text file from livecode
Hi monki,
Easy peasy!
put fld "myText" into url "file:myNewFile.txt"
Now that will put it in the current default directory.
Simon
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!
Re: Creating a text file from livecode
I never would've thought of "URL". Saw it in the user doc, but thought it only pertained to servers
Anyway, that got it working, thanks

-
- VIP Livecode Opensource Backer
- Posts: 10052
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Creating a text file from livecode
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 ""e& pFile "e& "(" & 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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Creating a text file from livecode
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 10052
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Creating a text file from livecode
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn