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

fs api, need help on a new topic

Started by cheekycharlie101, 31 October 2012 - 11:55 AM
cheekycharlie101 #1
Posted 31 October 2012 - 12:55 PM
ok, so thanks to all the awesome people here my disk manager is now almost complete. i can clone a disk, re label a disk and now i just need to be able to wipe a disk.
ok so the only way i can think of doing this is putting fs.list() into a variable then delete the variable. this probably wont work as i have not tested it yet but would this be any good? if not can someone help me out please ?:P/>/>

here is what i thought up:


local a = fs.list("disk/"!)
sleep(1)
print("Wiping DIsk...")
fs.delete(a)
print("Disk Cleared")
if this does not work can someone help me out and give me a few pointers?
thanks -Cheeky
1lann #2
Posted 31 October 2012 - 01:51 PM
Sadly computers cannot read our minds and tell exactly what we want it to do, so you have to be ultra specific and take note of what each function actually "does". Fs.list returns a "table" of the files and folders in "string" form in a specified directory. Fs.delete deletes a file and accept only "string" as arguments. What you're doing is like trying to stick money notes into a coin slot. It won't work. So what you need to do is go through every entry of the table and fs.delete the entry. (Not deleting the entry, deleting the file the entry is referring to). We can do this by using a for statement. The code would look something like this

for k, v in pairs(fs.list("disk")) do fs.delete(v) end

The variable k represents the index of the table you're at, the variable v represents the value of that index.
cheekycharlie101 #3
Posted 31 October 2012 - 08:31 PM
thanks, iv'e seen that for loop example in pairs a lot before but dont really under stand it. could you explain further? that would be great.
thanks -Cheeky
remiX #4
Posted 31 October 2012 - 08:40 PM
Maybe this will be a good example :?


They loop through a table
tbl = { apples = 2, oranges = 3 }
for k, v in pairs(tbl) do    -- k = "Key", v = "Variable"
  print(k, v) -- K will print "apples, oranges" V will print the numbers - 2, 3
end
cheekycharlie101 #5
Posted 31 October 2012 - 11:41 PM
er ignore this, i fucked up and could not delete it read post below


|
|
|
|
|
|
|
cheekycharlie101 #6
Posted 31 October 2012 - 11:45 PM
Sadly computers cannot read our minds and tell exactly what we want it to do, so you have to be ultra specific and take note of what each function actually "does". Fs.list returns a "table" of the files and folders in "string" form in a specified directory. Fs.delete deletes a file and accept only "string" as arguments. What you're doing is like trying to stick money notes into a coin slot. It won't work. So what you need to do is go through every entry of the table and fs.delete the entry. (Not deleting the entry, deleting the file the entry is referring to). We can do this by using a for statement. The code would look something like this for k, v in pairs(fs.list("disk")) do fs.delete(v) end The variable k represents the index of the table you're at, the variable v represents the value of that index.
ok so i got this code, it works perfectly, accept it dont wipe the disk. there is no errors or anything. here is the code. il then go through the steps underneath so you know what the code is surposed to do:

here is the code:

write("Please Enter Drive Side: ")
local a = io.read()
  if a == "top" or a == "bottom" or a == "left" or a == "right" or a == "front" or a == "back" then
    print("Checking for drive...")
	 local b = peripheral.isPresent(a)
	   local function dclean()
		 print("Please Enter Disk")
		  while true do
		    local c = disk.isPresent(a)
			 sleep(1)
			 if c then
			   print("Disk Found")
			   sleep(1)
			   print("Wiping The Disk...")
			   sleep(1)
			   for k,v in pairs(fs.list("disk/")) do
			   fs.delete(v)
			   print("Disk Wiped")
			   disk.eject(a)
			  sleep(1)
			   break
			 end
		   end
		 end
	   end
			   
	 if b then
	   sleep(1)
	   print("Drive Found")
	   dclean()
	 else
	   sleep(1)
	   print("No Drive Found")
	 end
   else
	 sleep(1)
	 print("Invalid Drive Side")
   end

1. input the drive side – if no drive side is found then it prints that and ends the program. it does the same for a invalid side
2. it asks for a disk, if the disk is found it then prints wiping disk then ejects it. it ejects it but does not wipe it.
Kingdaro #7
Posted 01 November 2012 - 12:18 AM
I think fs.list only returns filenames, and not paths.

In this part of the code:

for k,v in pairs(fs.list("disk/")) do
fs.delete(v)

You would do

for k,v in pairs(fs.list("disk/")) do
fs.delete("disk/"..v)
end

I added the end there because I think you're missing it, but i might be wrong. If the program breaks for whatever reason, remove the end.
cheekycharlie101 #8
Posted 01 November 2012 - 12:47 AM
I think fs.list only returns filenames, and not paths.

In this part of the code:

for k,v in pairs(fs.list("disk/")) do
fs.delete(v)

You would do

for k,v in pairs(fs.list("disk/")) do
fs.delete("disk/"..v)
end

I added the end there because I think you're missing it, but i might be wrong. If the program breaks for whatever reason, remove the end.
thanks, il go test this now
cheekycharlie101 #9
Posted 01 November 2012 - 12:50 AM
I think fs.list only returns filenames, and not paths.

In this part of the code:

for k,v in pairs(fs.list("disk/")) do
fs.delete(v)

You would do

for k,v in pairs(fs.list("disk/")) do
fs.delete("disk/"..v)
end

I added the end there because I think you're missing it, but i might be wrong. If the program breaks for whatever reason, remove the end.
thanks sooooo much bro :P/>/> it worked :DDDD thanks so much :P/>/>
remiX #10
Posted 01 November 2012 - 01:21 AM
Check this out :P/>/>

Spoiler
sidesTable = {"left", "right", "top", "bottom", "front", "back"}

function cls(c, x, y)
    if c == 1 then term.clear() end
    term.setCursorPos(x,y)
end

function checkForDisk()
    cls(1, 1, 1) print("Checking for a disk in drive on the "..side_driveFound..".") sleep(1)
    for i = 1, #sidesTable do
        if disk.isPresent(side_driveFound) then
            print("nDisk found.")
            return checkForData()
        end
    end
    print("nPlease insert a disk into the drive on the "..side_driveFound..".")
    repeat
        event, side = os.pullEvent("disk")
    until event == "disk"
    print("You inserted a disk!") sleep(2)
    if disk.isPresent(side_driveFound) then
        print("Insert a disk and leave it there!")
        sleep(2) return checkForDisk()
    end
    return checkForData()
end

function checkForData()
    cls(1, 1, 1) print("Checking for data...")
    diskFiles = fs.list("disk")
    sleep(1)
    if diskFiles[1] then
        print("nFound ["..#diskFiles.."] items.n")
        sleep(1)
        for i = 1, #diskFiles do
            sleep(0.2)
            write("Deleting...  ")
            sPath = "disk/"..diskFiles[i]
            sleep(1)
            fs.delete(sPath)
            print(diskFiles[i])
        end
    else
        print("No data found on the disk!")
    end
    print("Ejecting...") sleep(1) disk.eject(side_driveFound) return checkForDrive()
end

function checkForDrive()
    cls(1, 1, 1) print("Checking for a drive...") sleep(1)
    for i = 1, #sidesTable do
        if peripheral.getType(sidesTable[i]) == "drive" then
            side_driveFound = sidesTable[i]
            print("Drive found on the "..side_driveFound..".") sleep(2)
            return checkForDisk()
        end
    end
    print("Please place a drive on any side.")
    repeat
        event, side = os.pullEvent("peripheral")
    until peripheral.getType(side) == "drive"
    print("You place a drive on the "..side..".") sleep(2)
    return checkForDisk()
end

checkForDrive()