Create unique filename and write to it

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
tobx
Posts: 16
Joined: Thu Jun 04, 2015 5:17 pm

Create unique filename and write to it

Post by tobx » Mon Aug 17, 2015 6:28 pm

Hi all! I'm trying to figure out file naming while creating and writing to said file.

I have a field where users can input their names. I want to create a new file with their name in it, then write to this file (put their scores into it).

I'm following BYU's "Working with External Files" tutorial and it's getting me most of the way there but only for files that are already named or do not use user input to name the file itself. I know from prior experience if I try to assign their input into a variable, then name a file after that variable, it won't work. For example:

Code: Select all

global gname

put name into gname
open file "gname.txt" for append
This saves their input (name) into a variable (gname) but I know "gname.txt" will just create a new txt file named gname (not their input). I'm sure there's some simple solution I'm just not finding in Google searches so any help would be appreciated!

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

Re: Create unique filename and write to it

Post by FourthWorld » Mon Aug 17, 2015 6:54 pm

You can mix variable values with literals using LiveCode's concatenation operator, "&", e.g.:
open file (gName &".txt") for append
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Create unique filename and write to it

Post by jmburnod » Mon Aug 17, 2015 6:58 pm

Hi tobx,

Define a destination folder for all users is a good way for that.
Something like that:

Code: Select all

global gName
on createMyUserFile
   ask "name"
   put it into gName
   put specialfolderPath("documents") & "/" & "myUsersFolder" into tPathFolUsers
   if there is not a folder tPathFolUsers then
      new folder tPathFolUsers
   end if
   put tPathFolUsers & "/"  &gName & ".txt" into tNameFile
   put "blabala" into  url("file:" & tNameFile)
   open file tNameFile for append
   --do something
   close file tNameFile
end createMyUserFile

--To import current user file:
on openMyUserFile
   global gName
   put specialfolderPath("documents") & "/" & "myUsersFolder" into tPathFolUsers
   put tPathFolUsers & "/"  &gName & ".txt" into tNameFile
   put url("file:" & tNameFile) into tMyScore
   put tMyScore
end openMyUserFile
Best regards
Jean-Marc
https://alternatic.ch

tobx
Posts: 16
Joined: Thu Jun 04, 2015 5:17 pm

Re: Create unique filename and write to it

Post by tobx » Thu Aug 20, 2015 3:24 am

FourthWorld wrote:LiveCode's concatenation operator, "&"
Exactly the function I was looking for but couldn't name; thanks FourthWorld!
jmburnod wrote:

Code: Select all

   put "blabala" into  url("file:" & tNameFile)
Hi Jean-Marc. SUPER helpful suggestion regarding defining the path! I got a little lost on this line, though? Could you help me understand it a bit more?

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

Re: Create unique filename and write to it

Post by jmburnod » Thu Aug 20, 2015 5:02 pm

Sorry for that. That should be clearest.

Code: Select all

put url("file:" & myPath) into myMar
is equivalent to

Code: Select all

open file myPath for read
read from file myPath until eof
close file myPath
But Klaus makes me lazy with his short sentence "lazy Moi") :D

Best regards
Jean-Marc
https://alternatic.ch

Post Reply