Page 1 of 3
Writing to a file
Posted: Thu Jun 16, 2016 6:51 pm
by RossG
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.
Re: Writing to a file
Posted: Thu Jun 16, 2016 7:01 pm
by FourthWorld
Re: Writing to a file
Posted: Thu Jun 16, 2016 8:11 pm
by RossG
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.
Re: Writing to a file
Posted: Thu Jun 16, 2016 8:39 pm
by FourthWorld
See the UUID function.
Re: Writing to a file
Posted: Thu Jun 16, 2016 9:08 pm
by RossG
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"?
Re: Writing to a file
Posted: Sat Jun 18, 2016 9:59 pm
by RossG
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.
Re: Writing to a file
Posted: Sat Jun 18, 2016 10:15 pm
by FourthWorld
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)
Re: Writing to a file
Posted: Sun Jun 19, 2016 3:11 am
by RossG
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.
Re: Writing to a file
Posted: Sun Jun 19, 2016 5:58 am
by FourthWorld
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

Re: Writing to a file
Posted: Sun Jun 19, 2016 6:54 pm
by RossG
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).
Re: Writing to a file
Posted: Sun Jun 19, 2016 7:49 pm
by RossG
So...some progress made.
Delete folder doesn't work.
Re: Writing to a file
Posted: Sun Jun 19, 2016 8:03 pm
by FourthWorld
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.
Re: Writing to a file
Posted: Mon Jun 20, 2016 5:28 am
by mwieder
Delete folder doesn't work.
It does if you use it properly.
newSFP is empty. What do you want to delete?
Re: Writing to a file
Posted: Mon Jun 20, 2016 5:27 pm
by jacque
Also note that you can't delete a folder until all its contents are deleted.
Re: Writing to a file
Posted: Tue Jun 21, 2016 2:26 am
by RossG
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.