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

copy all files from one computer on to disk [and vice-versa]

Started by AndreWalia, 19 November 2012 - 09:26 AM
AndreWalia #1
Posted 19 November 2012 - 10:26 AM
can someone teach me the basics of for loops? When i look it up they dont make sence to me.


first of all how do I get every program, every directory, every program in every directory ect.

second of all the for loop comes in. how would i take all of those and put them in the same place they were on my computer but onto the disk.


pretty much taking EVERYTHING in the computer and putting it the way it was on the computer, but on the disk
Orwell #2
Posted 19 November 2012 - 10:37 AM
So you're asking for the actual code?

local destination = '/disk/', you could make it search for a disk as well
local lst = fs.list('') -- takes all files and directories in the root directory
for i,v in pairs(lst) do -- iterates over each entry in 'lst', so v takes on the value of each string and i takes on the value of the index in the table (1,2,3,...)
  local toDir = fs.combine( destination, v) -- combines two paths in a smart way (keeps '/' in mind)
  if v ~= 'rom' then    -- optional (ignores /rom)
    local success = pcall( fs.copy, v, toDir) -- returns false if an error occurred (I often get issues while copying some files in /rom for example)
    if success then
      print("Copied '"..v.."'.")
    else
      printError("Couldn't copy '"..v.."'.")
    end
  end
end
print("Finished copying.")

Please try to understand it and ask every question that pops up. :D/>/> We try to actually help learning, rather than giving code away. :)/>/>
AndreWalia #3
Posted 19 November 2012 - 10:41 AM
So you're asking for the actual code?

local destination = '/disk/', you could make it search for a disk as well
local lst = fs.list('') -- takes all files and directories in the root directory
for i,v in pairs(lst) do -- iterates over each entry in 'lst', so v takes on the value of each string and i takes on the value of the index in the table (1,2,3,...)
  local toDir = fs.combine( destination, v) -- combines two paths in a smart way (keeps '/' in mind)
  local success = pcall( fs.copy, v, toDir) -- returns false if an error occurred (I often get issues while copying some files in /rom for example)
  if success then
	print("Copied '"..v.."'.")
  else
	printError("Couldn't copy '"..v.."'.")
  end
end
print("Finished copying.")

Please try to understand it and ask every question that pops up. :D/>/> We try to actually help learning, rather than giving code away. :)/>/>

Actually, I was not asking for the actual code. I was asking how for loops work. But since you told me it i guess i cant learn:/
Thanks anyway
Orwell #4
Posted 19 November 2012 - 10:43 AM
I know you didn't :D/>/> that's why I asked first. Then I decided to post it anyways. :)/>/> If you actually went through some documentation about for loops before asking this question, I figured an example that does what you eventually wanted would help understanding. (hence the overkill on comments)
AndreWalia #5
Posted 19 November 2012 - 10:59 AM
Now could you teach me the for loop?
AndreWalia #6
Posted 19 November 2012 - 11:02 AM
and also it doesn't work for moving from disk to computer :/
Orwell #7
Posted 19 November 2012 - 12:17 PM
Try not to double post, there's an edit button. :)/>/>
I'll just stick to the for loop for now. Loops in general enable you to repeat a block of code several times.
The while loop is the easiest to understand:

while <condition> do
  -- code here
end
The condition is in the same form as it's used in if statements (if you're not sure what I mean, be sure to ask :D/>/> ).
A typically way this is used (I'm sure you know it) is as an eternal running loop:

while true do
  -- stuff
  sleep(0)
end

But, you can also use it with a counter. To do this, you use a variable that keeps count. This is an easy example:

local count = 1  -- lowest number
while count <= 10 do  -- 10 is highest number
  print( count )
  count = count + 1
end

Okay, you got that? :D/>/> This way of using while loops is used so often that it has been simplified into the for loop. Usage of a for loop is like this:

for i=<lowest number>, <highest number> do
  -- do stuff
end
With numbers instead of the < > of course. It is exactly the same (from 1 up to 10 inclusive) as the while loop I just showed.
So to replicate the above while loop, you'd have:

for count=1,10 do
  print(count)
end

I won't mention pairs and ipairs for now, you should get the grasp of a regular for loop. I'm also not sure if you're familiar with table. And I'm sort of hoping that after this short explanation, you'll find your way in the documentation on lua. :D/>/>