how to get text from a file on a server

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: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
karmacomposer
Posts: 355
Joined: Wed Apr 27, 2011 2:12 pm

how to get text from a file on a server

Post by karmacomposer » Wed Jul 22, 2020 5:54 pm

I have tried 10 ways to sunday using the file: command, the url command, the ftp:// command, etc.

I am simply trying to read a number from a text file located on a web server without having to download it at all.

How is this done?

I am sure it is easy, but for some odd reason I just cannot get the coding right.

Thank you.

Mike

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7405
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: how to get text from a file on a server

Post by dunbarx » Wed Jul 22, 2020 7:21 pm

Hi.

Generically, something like this does not work:

Code: Select all

   open file yourFile for read
   read from file yourFile until EOF
   close file yourFile
Are you sure you know the pathName to the file? If not, you can use the "open file" command to get that information directly.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8418
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: how to get text from a file on a server

Post by FourthWorld » Wed Jul 22, 2020 7:47 pm

Being on a web server limits your options, since HTTP is designed to deliver whole resources and provides no built-in means to extract specific portions of a resource.

If this is your own server, you could consider having your client call an LC Server script which reads the desired portion of the file and returns only that.

If it's not a server you manage your options will be more limited, and you may even need to just download the whole thing - how large is it?
Richard Gaskin
LiveCode Community Liaison

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

karmacomposer
Posts: 355
Joined: Wed Apr 27, 2011 2:12 pm

Re: how to get text from a file on a server

Post by karmacomposer » Wed Jul 22, 2020 8:04 pm

File is tiny. 1k to 2k.

Right now it’s on a server I control but in the future I will not have explicit access. No way to read it?

It’s just a simple text file.

Mike

karmacomposer
Posts: 355
Joined: Wed Apr 27, 2011 2:12 pm

Re: how to get text from a file on a server

Post by karmacomposer » Wed Jul 22, 2020 8:06 pm

dunbarx wrote:
Wed Jul 22, 2020 7:21 pm
Hi.

Generically, something like this does not work:

Code: Select all

   open file yourFile for read
   read from file yourFile until EOF
   close file yourFile
Are you sure you know the pathName to the file? If not, you can use the "open file" command to get that information directly.

Craig
No. Something like that does not work.

Currently, I download the file and then read it but I’d rather read it in place and not touch it.

I do use tsnet with sftp to read and write to it. But I was hoping to read it in place and store the 1st line in a variable.

Mike

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8418
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: how to get text from a file on a server

Post by FourthWorld » Wed Jul 22, 2020 8:14 pm

1k-2k? Unless there's something wrong with the connection, or some unusual constraint in the problem (e.g. real-time gaming*) where the difference between 2k and a few bytes makes a noticeable difference, just download it. The overall throughput difference probably isn't much.

My own rule of thumb is that if it's smaller than a file system's block size (usually 4k these days), I don't spend wetware cycles thinking about it for local or remote IO.

* I was recently tickled to learn that many modern game devs have been using UDP for real-time updates to avoid the overhead of TCP, where apparently interpolating between missed packets is less impactful than the cost of TCP's assured delivery for certain types of game play:
https://www.gafferongames.com/post/udp_vs_tcp/
Richard Gaskin
LiveCode Community Liaison

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8418
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: how to get text from a file on a server

Post by FourthWorld » Wed Jul 22, 2020 8:18 pm

Thinking about this more, you could modify libURL to cut off the HTTP read after a few bytes. But that's a lot of work, with minimal payoff unless the problem you're solving has specialized needs.
Richard Gaskin
LiveCode Community Liaison

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

karmacomposer
Posts: 355
Joined: Wed Apr 27, 2011 2:12 pm

Re: how to get text from a file on a server

Post by karmacomposer » Wed Jul 22, 2020 8:23 pm

Richard,

I agree with you. In fact, I already do this. However, I am having a very WEIRD problem.

I cannot discuss it fully, but in a nutshell, I keep track of what is being written and track it at all times.

Say I have a number, say 50. Now I add one to it and write it. Now it's, ta dah, 51.

Right? Not right. The next time I d/l the file, it's either blank or 1.

WTF.

I have no idea how to track this, i'll call it, bug. It's making my system very difficult to deal with as a result of not being reliable.

I use a lot of encryption and I am wondering if the time it takes to encrypt large files is somehow superceeding writing this tiny little file
and zeroing it out? I'm just not sure.

Therefore, i'd like a way to just read the number from a distance from a source that is not yet corrupted.

Instead of downloading it, I just want to read the number and store it in a new variable so that if the file becomes corrupted, I can write it back to it.

That's the best I can explain it due to being patents pending.

Mike

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8418
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: how to get text from a file on a server

Post by FourthWorld » Wed Jul 22, 2020 11:35 pm

I like weird problems. If you want to discuss it via email, feel free to send an NDA. I can't guarantee I can fix it but sometimes having a second pair of eyes on something can help.
Richard Gaskin
LiveCode Community Liaison

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

karmacomposer
Posts: 355
Joined: Wed Apr 27, 2011 2:12 pm

Re: how to get text from a file on a server

Post by karmacomposer » Thu Jul 23, 2020 1:16 am

I made a work around today.

I am writing to a .ini file at the very beginning and then checking against it at any write time.

It works as error correction but is more like a bandage and not a fix.

Mike

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7405
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: how to get text from a file on a server

Post by dunbarx » Thu Jul 23, 2020 2:24 am

I did not see that a server was the source.

So, er, never mind... :oops:

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5837
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: how to get text from a file on a server

Post by jacque » Thu Jul 23, 2020 5:00 pm

I need to update data on the server a lot, and I send the updated info to a server script which writes it to the correct file. Same for retrieval, the script reads the file and returns the info I need. This gets around permissions errors and has been reliable.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

jiml
Posts: 242
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: how to get text from a file on a server

Post by jiml » Thu Jul 23, 2020 5:53 pm

I am simply trying to read a number from a text file located on a web server without having to download it at all.

How is this done?
...
It’s just a simple text file.
If that's actually what you want to do have you tried this?

Code: Select all

put url "https://mysite.com/mysimplenumberfile.txt" into theNumberIWanted
Jim Lambert

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8418
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: how to get text from a file on a server

Post by FourthWorld » Thu Jul 23, 2020 6:22 pm

jiml wrote:
Thu Jul 23, 2020 5:53 pm
I am simply trying to read a number from a text file located on a web server without having to download it at all.

How is this done?
...
It’s just a simple text file.
If that's actually what you want to do have you tried this?

Code: Select all

put url "https://mysite.com/mysimplenumberfile.txt" into theNumberIWanted
What needs to be in place to allow writes to the server? How is authentication handled so those writes can't be made by others?
Richard Gaskin
LiveCode Community Liaison

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

karmacomposer
Posts: 355
Joined: Wed Apr 27, 2011 2:12 pm

Re: how to get text from a file on a server

Post by karmacomposer » Thu Jul 23, 2020 10:48 pm

Not writing to the server in this case, just wanting to read a text file's contents.

I have reading and writing through SFTP using TSNet working perfectly by downloading/transferring the file and reading it on the node.

I was hoping to avoid all that extra code and just 'take it's temperature' essentially.

I made a work around that works.

Thanks everyone.

Mike

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”