Jump to content




Writing to random file name


  • You cannot reply to this topic
5 replies to this topic

#1 boudragon

  • Members
  • 107 posts

Posted 02 July 2013 - 03: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!

#2 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 02 July 2013 - 03: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


#3 boudragon

  • Members
  • 107 posts

Posted 02 July 2013 - 03:22 PM

Cool testing now...

#4 boudragon

  • Members
  • 107 posts

Posted 02 July 2013 - 03: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!

#5 boudragon

  • Members
  • 107 posts

Posted 02 July 2013 - 04: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

#6 boudragon

  • Members
  • 107 posts

Posted 02 July 2013 - 04:12 PM

Nevermind got it LOL!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users