Jump to content




fs.copy Help!


4 replies to this topic

#1 cooperwey

  • New Members
  • 1 posts
  • LocationAustralia

Posted 07 January 2013 - 05:54 AM

Hello Pros, I am in the very, very, very early stages of learning Lua in ComputerCraft. I am trying to make a program that copies a program from the rom folder to a disk. I have prepared only around 7 lines of code, and am having trouble with defining a variable for the disk folder.

I might have explained it wrong but I hope you understand! (And please don't hassle me because I'm very new to this stuff...)


term.clear()
term.setCursorPos(1,1)
write("What's the program you want to copy?")
name = io.read()
disk = fs.isDir("disk")
fs.copy(" name, disk " )


#2 KaBooMa

  • New Members
  • 29 posts

Posted 07 January 2013 - 06:13 AM

So what I think you are attempting here is to set disk to the disk path, and you want to make it so you can type a name to copy from the name, to the disk. If so, this is a very simple problem to solve! You almost got it just a few errors! Lets fix this :)

name = io.read()
Pretty sure that works, but just for notes, you can also use
name = read()
and do the same :)

disk = fs.isDir("disk")
what you got here is a variable called disk and you are setting it to fs.isDir("disk"). If you look at the isDir() function you can see that it return a boolean. So basically you are saying disk is equal to true if disk is a dir, and false if it is not. A easier way of doing this is just saying
if (fs.exists("disk")) then
  -- fs.copy code
end

fs.copy("name, disk")
a bit wrong but like I said, we all have made mistakes :P
you are calling the function fs.copy() and telling it to copy "name,disk" to blank :P What you really need to be doing is telling it to copy the name to the disk. You can do so very easy like so
fs.copy(name,"disk/"..fs.getName(name))
Basically this would do so: If name is "test/file" then it copies file to disk/file! Nice! Lets see this all wrapped up!

term.clear()
term.setCursorPos(1,1)
term.write("What's the porgram you want to copy?")
local name = read()
if (fs.exists("disk")) then
  fs.copy(name,"disk/"..fs.getName(name))
else
  term.write("Sorry, you seem to not have a disk in the drive! Place it in and try again.")
end

Everything looks good above. Test that and tell me if it works. If not, we can see what was done wrong :) I hope this helped!

#3 Exerro

  • Members
  • 801 posts

Posted 07 January 2013 - 06:14 AM

fs.copy( name, "disk" ) should work

#4 KaBooMa

  • New Members
  • 29 posts

Posted 07 January 2013 - 06:17 AM

actually fs.copy(name,"disk") is going to copy the file he types to root/disk. So the file will be named disk ^.^ Believe that is how it works unless some voodoo magic happens :S

#5 Exerro

  • Members
  • 801 posts

Posted 10 January 2013 - 09:12 AM

View PostKaBooMa, on 07 January 2013 - 06:17 AM, said:

actually fs.copy(name,"disk") is going to copy the file he types to root/disk. So the file will be named disk ^.^ Believe that is how it works unless some voodoo magic happens :S
oh yeah stupid me :P its "disk/"..file sneaky ninja posted a gigantic help thing above so you probably found this anyway





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users