Creating and analyzing several text files

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
Josh1910
Posts: 19
Joined: Tue Jan 20, 2015 7:30 am

Creating and analyzing several text files

Post by Josh1910 » Wed Jan 18, 2017 4:01 am

Hi all,

I used to use code like the following to create data files:

put "File:PilotData.txt" into myFile
put url "File:PilotData.txt" into myData
put "test" into myData
put myData into URL myFile

but now nothing seems to work. I can't create a file regardless of how I order or work these commands. Any help for creating or retrieving data would be most appreciated!

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Creating and analyzing several text files

Post by jmburnod » Wed Jan 18, 2017 10:26 am

Hi Josh,
This script create if not exists, read and set the content of a file of documents folder

Code: Select all

   put "PilotData2.txt" into myFile
   put specialfolderPath("documents") into tPathFol
   put tPathFol & "/" &  myFile into  tPathFile
   if there is a file tPathFile then
      put url ("File:" & tPathFile) into myData 
   end if
   put "test" into myData
   put myData into url ("File:" & tPathFile)
Best regards
Jean-Marc
https://alternatic.ch

Josh1910
Posts: 19
Joined: Tue Jan 20, 2015 7:30 am

Re: Creating and analyzing several text files

Post by Josh1910 » Wed Jan 18, 2017 11:24 am

Thanks for the help! I've tried this and no file still shows up. I've tried refreshing the folder and there is no error (compile or runtime), so I'm still not sure what the problem is... I'll try making a text file and seeing if the data writes to that.

Josh1910
Posts: 19
Joined: Tue Jan 20, 2015 7:30 am

Re: Creating and analyzing several text files

Post by Josh1910 » Wed Jan 18, 2017 11:48 am

This is so puzzling :( None of my file code works (reading or writing).

Josh1910
Posts: 19
Joined: Tue Jan 20, 2015 7:30 am

Re: Creating and analyzing several text files

Post by Josh1910 » Wed Jan 18, 2017 12:00 pm

IMPORTANT: I think my program was embedded into too many subdirectories. Hopefully, this isn't premature celebration, but it seems to work if I put the program on the desktop.

Josh1910
Posts: 19
Joined: Tue Jan 20, 2015 7:30 am

Re: Creating and analyzing several text files

Post by Josh1910 » Wed Jan 18, 2017 12:20 pm

Final update (unless someone has two cents to add). It work near the desktop (avoiding subdirectories) and, unfortunately, it only works once (i.e., LC needs to be restarted every time for the file to be created). A bit lame, but hopefully it'll suffice.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Creating and analyzing several text files

Post by Klaus » Wed Jan 18, 2017 12:52 pm

Hi Josh,

what part exactly does not work here?

Code: Select all

...
put "File:PilotData.txt" into myFile
put url "File:PilotData.txt" into myData 
put "test" into myData
put myData into URL myFile
...
Did you check "the result" or answer the variables right after the part that does not work?

Code: Select all

...
put "File:PilotData.txt" into myFile
put url "File:PilotData.txt" into myData 

## Dialog should show content of the file
ANSWER myData

put "test" into myData
put myData into URL myFile
if the result <> EMPTY then
   answer "Error:" && the result
end if
...
And since you did not specify a complete pathname, make sure "the directory" is really set
to the folder with thaé stack whichj is executing these commands!

Or use -> specialfolderpath("resources")
which makes sure that LC is looking for the file in the same folder as the stack,
no matter in how many subdirectories the stack is "buried"!

Code: Select all

...
put "File:" & specialfolderpath("resources") & "/PilotData.txt" into myFile
put url myFile into myData 
...
Best

Klaus

Josh1910
Posts: 19
Joined: Tue Jan 20, 2015 7:30 am

Re: Creating and analyzing several text files

Post by Josh1910 » Wed Jan 18, 2017 2:45 pm

Thanks, Klaus. This is useful. My only lingering question is why I can't write new files using the same code if I run the program a second time (i.e., I have to quit the program and then restart it). Any thoughts on that?

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Creating and analyzing several text files

Post by Klaus » Wed Jan 18, 2017 2:52 pm

Sorry, Josh, I have no idea! :(
I could offer to take a look at your stack, if you like.

If yes, send the stack and a "PilotData.txt" file to: klaus AT major-k.de

Post Reply