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
copy all files from one computer on to disk [and vice-versa]
Started by AndreWalia, Nov 19 2012 10:26 AM
6 replies to this topic
#1
Posted 19 November 2012 - 10:26 AM
#2
Posted 19 November 2012 - 10:37 AM
So you're asking for the actual code?
Please try to understand it and ask every question that pops up.
/> We try to actually help learning, rather than giving code away.
/>
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.
#3
Posted 19 November 2012 - 10:41 AM
Orwell, on 19 November 2012 - 10:37 AM, said:
So you're asking for the actual code?
Please try to understand it and ask every question that pops up.
/> We try to actually help learning, rather than giving code away.
/>
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.
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
#4
Posted 19 November 2012 - 10:43 AM
I know you didn't
/> 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)
#5
Posted 19 November 2012 - 10:59 AM
Now could you teach me the for loop?
#6
Posted 19 November 2012 - 11:02 AM
and also it doesn't work for moving from disk to computer :/
#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:
/> ).
A typically way this is used (I'm sure you know it) is as an eternal running loop:
But, you can also use it with a counter. To do this, you use a variable that keeps count. This is an easy example:
Okay, you got that?
/> 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:
So to replicate the above while loop, you'd have:
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.
/>
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 endThe 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
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?
for i=<lowest number>, <highest number> do -- do stuff endWith 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.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











