This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
andrewverry49000's profile picture

fs.open a variable file?

Started by andrewverry49000, 04 October 2012 - 11:42 AM
andrewverry49000 #1
Posted 04 October 2012 - 01:42 PM
I am trying to make a file open and write using a path variable in a string.


so, I have

filename = nil
write"filename: "
input = read()
filename = input
h = fs.open("Mydir/filename")

filename being the variable, i have been at this for 3 hours now, and cannot figure it out.
Any help would be appreciated

Thanks,
Andrew
Fatal_Exception #2
Posted 04 October 2012 - 02:08 PM
One way is to simply concatenate the strings:


local path = "/whatever/your/path/is/"
local filename = "myfile.txt"

file = fs.open(path .. filename, mode) -- where mode is "r", "w" etc.
Mtdj2 #3
Posted 04 October 2012 - 02:14 PM
Try looking at this post. (It is not mine.)
Click me!
jag #4
Posted 04 October 2012 - 02:40 PM
Well to shorten up all of your extra variables, you could simply do
write"Filename: "
filename = read()
h = fs.open("Mydir/"..filename) -- dont have quotes around your variable
Geforce Fan #5
Posted 26 October 2012 - 12:29 AM
Well to shorten up all of your extra variables, you could simply do
write"Filename: "
filename = read()
h = fs.open("Mydir/"..filename) -- dont have quotes around your variable
How do you do the , "w" thing to write to the file?
jag #6
Posted 26 October 2012 - 12:35 AM
Well to shorten up all of your extra variables, you could simply do
write"Filename: "
filename = read()
h = fs.open("Mydir/"..filename) -- dont have quotes around your variable
How do you do the , "w" thing to write to the file?

fs.open(path, mode)
Wiki: http://computercraft.info/wiki/index.php?title=Fs.open
remiX #7
Posted 26 October 2012 - 12:44 AM

write"Filename: "
filename = read()
h = fs.open("Mydir/"..filename, "w")
h.write("rofl")
h.close()