Naming File with Entered Variables

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
ldblalock
Posts: 2
Joined: Mon Jul 18, 2011 5:16 pm
Contact:

Naming File with Entered Variables

Post by ldblalock » Wed Jul 20, 2011 8:29 pm

I'm using LiveCode to program a memory experiment in which each individual will be assigned a unique ID. Currently, my first card is one with a field to enter the ID and a 'start' button that will launch the instructions. I'd like to have the program also take the number entered into the field and use it to name a txt file where I will store the data from the task (for example, if I enter 23 in the field, I'd like the data file to be called "23.txt"). I've figured out how to write variables to the file okay, but not how to name the file based on the entered variable. I'm very new to LiveCode so I could be missing something very obvious. Is there a way to do this?

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

Re: Naming File with Entered Variables

Post by jmburnod » Wed Jul 20, 2011 9:17 pm

Hi ldblalock,
Welcome to this forum
Is there a way to do this?
yes. something like that :

Code: Select all

on savemyID
  put fld "MYID" into tTheID --•• the fld "MYID" contains the ID user
   put tTheID&".txt" into tNameFile
   ask file "Save" with tNameFile --••Æor e path you builded
   if it = empty then exit savemyID
   open file it for write
   write mydata to file it --•• mydata = the data to write
   close file it
end savemyID
Best regards

Jean-Marc
https://alternatic.ch

ldblalock
Posts: 2
Joined: Mon Jul 18, 2011 5:16 pm
Contact:

Re: Naming File with Entered Variables

Post by ldblalock » Wed Jul 20, 2011 9:36 pm

That works brilliantly, thank you!

Lisa

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

Re: Naming File with Entered Variables

Post by Klaus » Thu Jul 21, 2011 12:32 pm

Hi Lisa,

in case you are as lazy as moi, I can recommend the shorter URL syntax :D
Reading/writing is a one-liner!

Write text to disk:
...
put fld "your field with text here" into url("file:" & "name of file.txt")
## or
## put your_variable_with_text into url...
...

Read text from disk:
...
put url("file:" & "name of file.txt") into fld "your field here"
...

If you are dealing with BINARY data, like images etc., use "binfile:" instead of "file:" .
Check URL in the dictionary for more info.


Best from germany

Klaus

Post Reply