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

Writing to random file name

Started by boudragon, 02 July 2013 - 02:14 PM
boudragon #1
Posted 02 July 2013 - 04:14 PM
Haven't found this anywhere on the forums… so I apologize ahead of time it it's out there.

Im trying to generate a random file name and then write to it using a simple math.random where it generates a number say 1-100. Then it opens a file using that random number and writes two lines of text from 2 variables stored in my program.

So if my variables are say "Boudragon" and "computercraft" then it will make a file with the random number as its filename and add those two lines of text in that file. Im sure this is super easy but its just bugging the heck outta me! LMAO!
Cranium #2
Posted 02 July 2013 - 04:19 PM

local save = false
while not save do
	local filename = tostring(math.random(100))
	if not fs.exists(filename) then
		local file = fs.open(filename, "w")
		file.writeLine(var1)
		file.writeLine(var2)
		file.close()
		save = true
	end
end
boudragon #3
Posted 02 July 2013 - 04:22 PM
Cool testing now…
boudragon #4
Posted 02 July 2013 - 04:27 PM
PERFECT… I cleared off the looping from your code cause I have a few other things I want to do with it but otherwise worked flawless. *facepalm* wasn't using tostring… HAHA!! Thanx again!
boudragon #5
Posted 02 July 2013 - 05:10 PM
Ok new issue… the if not fs.exist… I need it to look in a directory other than the one the program is in for the randomly generated file. Example:

pending/(filename)

Can't seem to get that to work… bad day for me :(/> lol
boudragon #6
Posted 02 July 2013 - 05:12 PM
Nevermind got it LOL!