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

newb question (involves fs.delete() trying to delete rom)

Started by deeredman1991, 23 February 2015 - 10:23 PM
deeredman1991 #1
Posted 23 February 2015 - 11:23 PM
Hey guys it's me again. :)/> so here is my code;

tl;dr : I need something like fs.delete() that will not crash on an error message.


local FileList = fs.list("") --Table with all the files and directories available

for _, file in ipairs(FileList) do --Loop. Underscore because we don't use the key, ipairs so it's in order
  if file ~= "startup" then
   fs.delete(file) --Delete the file
  end
end --End the loop

so the problem I am running into is that when it goes to delete rom it throws me "Access Denied" in the terminal.

I could EASILY add


if file ~= "startup" and file ~= "rom" then
  fs.delete(file) --Delete the file
end

but I would like to avoid that as I would also like to dynamically skip over trying to delete disk drives as well and because I do not know the system that I will by interacting with I cannot just hard code if file ~= "Disk" or "Drive_0"

essentially like I said earlier I need something like fs.delete() that will not crash on receiving an error message. I tried


if fs.delete(file) then --Delete the file
end

but that didn't work either. :(/>
Lemmmy #2
Posted 23 February 2015 - 11:46 PM
Even your previous version that throws the 'Access Denied' error would try to delete the contents of drives. My first thought is that you could try checking if it contains 'disk' or 'drive', but it could easily be overridden by folders with the same name. You could also do the same suggested check on only immediate directories, e.g. /*drive*/ and not /folder/*drive*. Those are the only things I can think of as of now - I'm not fully acquainted with the filesystem API.
Edit: I have another idea, but it's slightly overkill for what it is. You could loop through all the peripherals connected to the computer, and, if it's a drive, omit it from the 'to delete' file list.
Edited on 23 February 2015 - 10:48 PM
Bomb Bloke #3
Posted 24 February 2015 - 12:11 AM
fs.isReadOnly()
GopherAtl #4
Posted 24 February 2015 - 12:15 AM
@bombbloke, re: fs.isReadOnly(), disks aren't necessarily read-only, only treasure disks return true for fs.readOnly, but attempting to delete the actual virtual /disk directory for any of them throws an error.

There is another fs function, fs.getDrive(), which you could use, though. When passed a path, it returns a string indicating where the file or folder in the path is stored. For paths in rom, including the rom folder itself, it returns "rom" For disk drives, it returns the name of the side the disk drive is on, and for everything else - the normal files on your hard drive - it returns "hdd."

So, something like this should work:


if fs.getDrive(file) == "hdd" and file ~= "startup" then
  fs.delete(file) --Delete the file
end
Edited on 23 February 2015 - 11:16 PM
deeredman1991 #5
Posted 24 February 2015 - 12:20 AM
@bombbloke, re: fs.isReadOnly(), disks aren't necessarily read-only, only treasure disks return true for fs.readOnly, but attempting to delete the actual virtual /disk directory for any of them throws an error.

There is another fs function, fs.getDrive(), which you could use, though. When passed a path, it returns a string indicating where the file or folder in the path is stored. For paths in rom, including the rom folder itself, it returns "rom" For disk drives, it returns the name of the side the disk drive is on, and for everything else - the normal files on your hard drive - it returns "hdd."

So, something like this should work:


if fs.getDrive(file) == "hdd" and file ~= "startup" then
  fs.delete(file) --Delete the file
end

HOT DOG!!! It worked! lol ty :)/>
Edited on 23 February 2015 - 11:23 PM
GopherAtl #6
Posted 24 February 2015 - 12:22 AM
@bombbloke, re: fs.isReadOnly(), disks aren't necessarily read-only, only treasure disks return true for fs.readOnly, but attempting to delete the actual virtual /disk directory for any of them throws an error.

There is another fs function, fs.getDrive(), which you could use, though. When passed a path, it returns a string indicating where the file or folder in the path is stored. For paths in rom, including the rom folder itself, it returns "rom" For disk drives, it returns the name of the side the disk drive is on, and for everything else - the normal files on your hard drive - it returns "hdd."

So, something like this should work:


if fs.getDrive(file) == "hdd" and file ~= "startup" then
  fs.delete(file) --Delete the file
end

HOT DOG!!! It worked! lol ty :)/> Just had to make a slight adjustment. :)/>

if fs.getDrive(file) == "hdd" and file ~= "startup" and file ~= "rom" then
   fs.delete(file) --Delete the file
  end

O_o you shouldn't have? I had cc open and tested at a lua prompt before I submitted it, and for me, fs.getDrive("rom") returns "rom", not "hdd"
deeredman1991 #7
Posted 24 February 2015 - 12:25 AM
@bombbloke, re: fs.isReadOnly(), disks aren't necessarily read-only, only treasure disks return true for fs.readOnly, but attempting to delete the actual virtual /disk directory for any of them throws an error.

There is another fs function, fs.getDrive(), which you could use, though. When passed a path, it returns a string indicating where the file or folder in the path is stored. For paths in rom, including the rom folder itself, it returns "rom" For disk drives, it returns the name of the side the disk drive is on, and for everything else - the normal files on your hard drive - it returns "hdd."

So, something like this should work:


if fs.getDrive(file) == "hdd" and file ~= "startup" then
  fs.delete(file) --Delete the file
end

HOT DOG!!! It worked! lol ty :)/> Just had to make a slight adjustment. :)/>

if fs.getDrive(file) == "hdd" and file ~= "startup" and file ~= "rom" then
   fs.delete(file) --Delete the file
  end

O_o you shouldn't have? I had cc open and tested at a lua prompt before I submitted it, and for me, fs.getDrive("rom") returns "rom", not "hdd"

haha yeah, I edited my old post in hopes no one would see it lol; I didn't actually need that code I just automatically assumed I did XD oops. :)/>
Edited on 23 February 2015 - 11:26 PM
Hayden_Almeida #8
Posted 07 June 2015 - 01:35 AM
Hey guys it's me again. :)/> so here is my code;

tl;dr : I need something like fs.delete() that will not crash on an error message.


local FileList = fs.list("") --Table with all the files and directories available

for _, file in ipairs(FileList) do --Loop. Underscore because we don't use the key, ipairs so it's in order
  if file ~= "startup" then
   fs.delete(file) --Delete the file
  end
end --End the loop

so the problem I am running into is that when it goes to delete rom it throws me "Access Denied" in the terminal.

I could EASILY add


if file ~= "startup" and file ~= "rom" then
  fs.delete(file) --Delete the file
end

but I would like to avoid that as I would also like to dynamically skip over trying to delete disk drives as well and because I do not know the system that I will by interacting with I cannot just hard code if file ~= "Disk" or "Drive_0"

essentially like I said earlier I need something like fs.delete() that will not crash on receiving an error message. I tried


if fs.delete(file) then --Delete the file
end

but that didn't work either. :(/>

I solved this problem!
ACESS DENIED occurs when before in your program or in usage of the computer, you are playing to changing or setting other directories..
Look at this example:

if fs.exists("/contas/"..conta.."/emails/"..assunto) then
When i use this, the WORKING directory is now inside the variable "assunto"!
So when you try to use

fs.delete("/contas/"..conta.."/emails/"..assunto)
It will try to go into more folders in who are already open!

How to Solve? Look:


shell.setDir("/contas/"..conta.."/emails/")
fs.delete("assunto")


Hope this works with you too :D/>