Writing to a file

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Writing to a file

Post by RossG » Thu Jun 16, 2016 6:51 pm

I can't find much about writing to a file.

In particular, can I "write" the contents of a field
in one "go"?

Coding with LC - most fun you can have without taking
your clothes off.
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

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

Re: Writing to a file

Post by FourthWorld » Thu Jun 16, 2016 7:01 pm

Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Re: Writing to a file

Post by RossG » Thu Jun 16, 2016 8:11 pm

Thanks Richard. I did look through the lessons
but not in the right place.

Next question: to construct a file name that
will be unique.

I thought that "internet time" was
seconds since January 1,1970 so would have
been ideal but can't find anything to confirm.

The LC "internet time" isn't it.

There was also a FoxPro function for "Julian"
date, I think, which would have suited but
can't find that either.
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

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

Re: Writing to a file

Post by FourthWorld » Thu Jun 16, 2016 8:39 pm

See the UUID function.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Re: Writing to a file

Post by RossG » Thu Jun 16, 2016 9:08 pm

Ah!

How about "seconds"?

"Returns the number of seconds since the start of the eon."

eon - a very long or indeterminate period of time.

If it's not defined how can you calculate the seconds?

Thank again Richard.

Stand by for more....

Well now I've divided the seconds and it's around
46 years -- just what I thought. From 00.00 on
January 1, 1970.

So perhaps the Dictionary could say this instead of
the rather vague "eon"?
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Re: Writing to a file

Post by RossG » Sat Jun 18, 2016 9:59 pm

I've constructed a file name which I called fName
from "Data" and the last four characters of "seconds".

So fName = "Data1385.txt" say.

But if I put

put field Data into URL "file:fName"

I get a file named fName.

Tried a few alternatives without success.

No doubt it's simple - except I don't know how.

Help appreciated.
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

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

Re: Writing to a file

Post by FourthWorld » Sat Jun 18, 2016 10:15 pm

Anything between quotes is treated as a literal string. To combine a variable name with the string "file:" use the concatenation operator, "&". And to make sure the concatenation is done before the "url" specifier attempts to use it as a single thing, it helps to put that expression in parentheses as shown in the last example of the Lesson I linked to earlier, e.g.:

Code: Select all

put field Data into URL ("file:"&fName)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Re: Writing to a file

Post by RossG » Sun Jun 19, 2016 3:11 am

Well Richard I have to say I'm not in love with this
"URL" method.

A "URL" is something different as far as I'm concerned and
I much prefer the old (simple) method of writing to a file.

Never-the-less after several hours of trying I've got my data files
going to the right place.

I have the path:

set itemDel to "/"
put item 1 to -2 of the filename of this stack into tSFP

Next step: to check that tSFP/DataFiles exists and if not
make it.

Can't find any info on this - I'm suffering from
a severe case of "search fatigue".

Help, as always, much appreciated.
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

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

Re: Writing to a file

Post by FourthWorld » Sun Jun 19, 2016 5:58 am

RossG wrote:Well Richard I have to say I'm not in love with this
"URL" method.
No worries - you can always use the traditional open/write/close method:

Code: Select all

set itemDel to "/"
put the filename of this stack into tFile
put "SomeFileName.txt" into last item of tFile
open file tFile for write
if the result is not empty then
   answer the result &"(& sysError()&")"
   exit to top
end if
write tData to file tFile
close file tFile
The open/write/close method is a bit more verbose (slightly more so here with the error-checking, but I figured sooner or later you'll want to learn about the sysError function), but ideal if you need to make multiple reads or writes with a file, so it's good to be familiar with even if you wind up using the URL method for straight single-shot read/writes.
Next step: to check that tSFP/DataFiles exists and if not
make it.

Can't find any info on this - I'm suffering from
a severe case of "search fatigue".
That's OK - that's why we're here. :)

Turns out "there is" is an operator which lets you know if there is something. So to see if there is a file at a specified path, you can use:

Code: Select all

if there is a file tFile then
   answer "File already exists!"
   exit to top
end if
You can also use "there is not" to check for non-existence.

Unfortunately "ain't" is not yet a built-in operator, but a request has been submitted for that:
http://quality.livecode.com/show_bug.cgi?id=3157

:)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Re: Writing to a file

Post by RossG » Sun Jun 19, 2016 6:54 pm

Richard

I didn't make it clear above that I want to made a directory
(MD anyone?) or as the feminized world would now have it
"create a folder" (and not on my "desktop" which isn't a desk
and even if it was it's not the top).
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Re: Writing to a file

Post by RossG » Sun Jun 19, 2016 7:49 pm

So...some progress made.

Delete folder doesn't work.
Attachments
Make Directory.zip
(1.19 KiB) Downloaded 251 times
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

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

Re: Writing to a file

Post by FourthWorld » Sun Jun 19, 2016 8:03 pm

RossG wrote:I didn't make it clear above that I want to made a directory
(MD anyone?) or as the feminized world would now have it
"create a folder"
I don't understand "feminized" in this context, but if you want to create a folder in LiveCode you can use the "create folder" command.

If you really need to use a shell language for that, you can call shell scripts with the "shell" function. Though really, "md"? Please. Everyone knows it's "mkdir". :) Time to upgrade to Win10 so you can run the new bash subsystem available for Windows.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Writing to a file

Post by mwieder » Mon Jun 20, 2016 5:28 am

Delete folder doesn't work.
It does if you use it properly.
newSFP is empty. What do you want to delete?

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Writing to a file

Post by jacque » Mon Jun 20, 2016 5:27 pm

Also note that you can't delete a folder until all its contents are deleted.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Re: Writing to a file

Post by RossG » Tue Jun 21, 2016 2:26 am

mwieder wrote:
Delete folder doesn't work.
It does if you use it properly.
newSFP is empty. What do you want to delete?
Wanted to delete the folder just created.
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

Post Reply