Page 1 of 1

Create unique filename and write to it

Posted: Mon Aug 17, 2015 6:28 pm
by tobx
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!

Re: Create unique filename and write to it

Posted: Mon Aug 17, 2015 6:54 pm
by FourthWorld
You can mix variable values with literals using LiveCode's concatenation operator, "&", e.g.:
open file (gName &".txt") for append

Re: Create unique filename and write to it

Posted: Mon Aug 17, 2015 6:58 pm
by jmburnod
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

Re: Create unique filename and write to it

Posted: Thu Aug 20, 2015 3:24 am
by tobx
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?

Re: Create unique filename and write to it

Posted: Thu Aug 20, 2015 5:02 pm
by jmburnod
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