ID:2406186
 
(See the best response by Nadrew.)
Code:
    Text_Export()
set desc = "Importing Text File Test"
set category = "Testing"

var/msg = input("Enter the text you want to make in to a txt file.","Text Export") as null|message
if(!msg) return

text2file(msg)

return


Problem description:

The code above gives an error saying text2file needs 2 arguments. It's asking for a file path for an existing file to append to.

So im trying to play around with text2file and file2text. text2file doesn't seem to work the way I want it to though.

I am trying to take a string and create a txt file out of it. I don't want to append to an existing file, I want to create a new file containing the string and let the user choose the filed name/location. Similar to the ftp() proc but ftp wont let me just run a string through it.

Is there a way to actually do what I'm trying to do here?
Best response
You'd have to create the file with text2file(), send it to ftp() then fdel() it on the server.
    Text_Export()
set desc = "Importing Text File Test"
set category = "Testing"

var/msg = input("Enter the text you want to make in to a txt file.","Text Export") as null|message
if(!msg) return

text2file(msg,"temp_txt.txt")

usr << ftp("temp_txt.txt","Custom Output.txt")

fdel("temp_txt.txt")

return


I've never used fdel() before. This code above works, to create and export the file, but it doesn't delete the file at the end. Do I have the syntax incorrect?
Looks fine to me, all I can think of is that the OS is locking the file until the download finishes.

while(fexists("temp_txt.txt"))
fdel("temp_txt.txt")


Might help, but you'd probably want to add a check to make sure it doesn't run out of control.

You'd probably also want your temporary file to have the person's ckey in there somewhere so you can fdel() before the call too, that way you'll always be writing to an empty file, even if it wasn't properly deleted.
    Text_Export()
set desc = "Importing Text File Test"
set category = "Testing"

var/msg = input("Enter the text you want to make in to a txt file.","Text Export") as null|message
if(!msg) return

fdel("[usr.ckey]_temp_txt.txt")

text2file(msg,"[usr.ckey]_temp_txt.txt")

usr << ftp("[usr.ckey]_temp_txt.txt","Custom Output.txt")

return


I moved the fdel() to the beginning of the proc before the input and it works now. It leaves the file but that's probably for the best, having that extra backup on the server isnt a bad idea.