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

[LUA] How to copy all files from computer to floppy.

Started by Arauron, 20 February 2013 - 09:38 AM
Arauron #1
Posted 20 February 2013 - 10:38 AM
Hi, how would I go about copying all files from a computer into its own folder on a floppy with a LUA program? I was thinking about using the fs.copy action, but I see no way to copy all files with it. Sorry for the nooby question, still new to LUA. Thanks in advance :)/>/>
Lyqyd #2
Posted 20 February 2013 - 11:14 AM
Split into new topic.

You'll want to iterate through the table returned by fs.list() and use fs.copy() on each file.
GopherAtl #3
Posted 20 February 2013 - 11:19 AM
fs.copy will copy a file or directory, and you can loop over the files in a directory with fs.list, so…


--ignore list - keys in this table cover what won't be copied
--the "true" is meaningless, this is just a handy way to lookup keys quickly
local ignore={
  disk=true,
  rom=true,
  --anything else you don't want copied add here.
}

--list everything in root folder...
for _,filename in fs.list("")
  --only stuff not in ignore list
  if not ignore[filename] then
	--copy
	fs.copy(filename,"disk/"..filename)
  end
end

This assumes a single disk drive, with disk already inserted, and won't overwrite if same-named files exist on the disk already.
remiX #4
Posted 20 February 2013 - 06:02 PM
fs.copy will copy a file or directory, and you can loop over the files in a directory with fs.list, so…


--ignore list - keys in this table cover what won't be copied
--the "true" is meaningless, this is just a handy way to lookup keys quickly
local ignore={
  "disk"=true,
  "rom"=true,
  --anything else you don't want copied add here.
}

--list everything in root folder...
for _,filename in fs.list("")
  --only stuff not in ignore list
  if not ignore[filename] then
	--copy
	fs.copy(filename,"disk/"..filename)
  end
end

This assumes a single disk drive, with disk already inserted, and won't overwrite if same-named files exist on the disk already.


local ignore = {
    ['disk'] = true,
    ['rom'] = true,
}

I don't think you can do it like "disk"=true
GopherAtl #5
Posted 20 February 2013 - 06:10 PM
ahem. You're right, thanks. Should've been "["disk"]=true" or just "disk=true". Fixed.
Nujugi #6
Posted 14 November 2013 - 02:04 PM
13: 'do' expected

What did I do wrong?
TheOddByte #7
Posted 14 November 2013 - 02:16 PM
The line with a for loop should have a do

for _, filename in fs.list("") do
Nujugi #8
Posted 14 November 2013 - 04:55 PM

--ignore list - keys in this table cover what won't be copied
--the "true" is meaningless, this is just a handy way to lookup keys quickly
local ignore={
  ["disk"]=true,
  ["rom"]=true,
  ["cpdf"]=true,
  --anything else you don't want copied add here.
}

--list everything in root folder...
for _,filename in fs.list("") do
  --only stuff not in ignore list
  if not ignore[filename] then
	    --copy
	    fs.copy("disk/"..filename,filename)
  end
end

Tryed copy from disk to root like this:
but got: 11: attempt to call table
TheOddByte #9
Posted 14 November 2013 - 06:23 PM
Well change the line with the for loop to this

for _, filename in ipairs(fs.list("")) do
That should fix it ;)/>