Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
LordNocturnus

deleting file with a running program

Question

Hello

i am trying to download a file use it and delete it again to make space on the hard drive for the next file but i does not delete the file somehow:

local c = require("component")
local computer = require("computer")
local internet = require("internet")
local fs = require("filesystem")
local p = c.printer3d
args = {...}

link = "https://raw.githubusercontent.com/LordNocturnus/sf-"
folder = "/master/"
imax = 0

local TIMEOUT = 4  -- in seconds

for i = 0, imax do
 local url = link .. i .. folder .. "list.txt"
 local status, connection = pcall(internet.request, url)
 if not status or not connection then
  print("Download of " .. url .. " failed.")
 else
  local startTime = computer.uptime()
  while true do
   local response, isFinished = pcall(connection.finishConnect)
   if response and isFinished then
    local f = io.open("list.txt", "wb")
--    print(status)
--    print(response)
    print(isFinished)
--    print(connection)
    for chunk in connection do
     f:write(chunk)
    end
    f:close()
    break
   elseif computer.uptime() - startTime > TIMEOUT then
    print("Request to " .. url .. " timed out")
    os.exit()
   else
    os.sleep(0.25)
   end
  end
 end
 for line in io.lines('list.txt') do
  print(line)
  local url = link .. i .. folder .. line .. ".mb3d"
  local status, connection = pcall(internet.request, url)
  if not status or not connection then
   print("Download of " .. url .. " failed.")
  else
   local startTime = computer.uptime()
   while true do
    local response, isFinished = pcall(connection.finishConnect)
    if response and isFinished then
     local f = io.open(line .. ".mb3d", "wb")
--     print(status)
--     print(response)
     print(isFinished)
--     print(connection)
     for chunk in connection do
      f:write(chunk)
     end
     f:close()
     break
    elseif computer.uptime() - startTime > TIMEOUT then
     print("Request to " .. url .. " timed out")
     os.exit()
    else
     os.sleep(0.25)
    end
   end
  end
  cords = {}
  for i in string.gmatch(line,"([^-]+)") do
   cords[#cords+1] = tonumber(i)
  end
  print(cords[1],cords[2],cords[3])
--  printer(cords[1],cords[2],cords[3]) -- do something with the file
  file = string.format("%d-%d-%d.mb3d", cords[1],cords[2],cords[3])
  fs.remove(file)
 end
end

Link to post
Share on other sites

12 answers to this question

Recommended Posts

  • 0
  • Solution

terms:

the working directory: "the path that the shell is currently in (eg: if in the shell it says: "/home #" the working directory whould be "/home")

file-root (root): "is the root foldire of the virtuel computers filesystem "/" if not chanched in the mods code. all you need to know is that root means "/"

 

your program install/downloads the file on the working directory say for instane i am in "/home" when i start the program the files is going to be installed in that foldire then (eg "/home/0-41-0.mb3d") altho when you are deleteing the files you do not delete them from the working directory (eg: the program is trying to delete: "0-41-0.mb3d" insted of "/home/0-41-0.mb3d".

toavoid OC to write the files to the working directory we write "/" infront of all the paths when downloading so we tell the mod to write the files at the root ("/") insted of just the working directory a good practese is to all ways incluce a "/" first sense its not all functions who uses the working direcrtory forintance "fs.remove()" doesn't

 

this is a working version:

local c = require("component")
local computer = require("computer")
local internet = require("internet")
local fs = require("filesystem")
local p = c.printer3d
args = {...}

link = "https://raw.githubusercontent.com/LordNocturnus/sf-"
folder = "/master/"
imax = 0

local TIMEOUT = 4  -- in seconds

for i = 0, imax do
 local url = link .. i .. folder .. "list.txt"
 local status, connection = pcall(internet.request, url)
 if not status or not connection then
  print("Download of " .. url .. " failed.")
 else
  local startTime = computer.uptime()
  while true do
   local response, isFinished = pcall(connection.finishConnect)
   if response and isFinished then
    local f = io.open("list.txt", "wb")
--    print(status)
--    print(response)
    print(isFinished)
--    print(connection)
    for chunk in connection do
     f:write(chunk)
    end
    f:close()
    break
   elseif computer.uptime() - startTime > TIMEOUT then
    print("Request to " .. url .. " timed out")
    os.exit()
   else
    os.sleep(0.25)
   end
  end
 end
 for line in io.lines('list.txt') do
  print(line)
  local url = link .. i .. folder .. line .. ".mb3d"
  local status, connection = pcall(internet.request, url)
  if not status or not connection then
   print("Download of " .. url .. " failed.")
  else
   local startTime = computer.uptime()
   while true do
    local response, isFinished = pcall(connection.finishConnect)
    if response and isFinished then
     local f = io.open("/"..line .. ".mb3d", "wb")
--     print(status)
--     print(response)
     print(isFinished)
--     print(connection)
     for chunk in connection do
      f:write(chunk)
     end
     f:close()
     break
    elseif computer.uptime() - startTime > TIMEOUT then
     print("Request to " .. url .. " timed out")
     os.exit()
    else
     os.sleep(0.25)
    end
   end
  end
  cords = {}
  for i in string.gmatch(line,"([^-]+)") do
   cords[#cords+1] = tonumber(i)
  end
  print(cords[1],cords[2],cords[3])
--  printer(cords[1],cords[2],cords[3]) -- do something with the file
  file = "/"..string.format("%d-%d-%d.mb3d", cords[1],cords[2],cords[3])
  fs.remove(file)
  print(file)
 end
end 

i added the tihng i was talking about:

adding a "/".. on line 77 and on line 53

 

am happy to help :D

 

 

btw, the 3d files that the program downloads sems to be curupted.

Edited by noahthegame
happy message
Link to post
Share on other sites
  • 0

Hi, next time please post code inside:

--code windows like this one witch makes it 10 times easyer to read (by useing the </> icon while also chanceing html to lua in the drop down menu when useing the </> button)

for my convines (and for others am going to do this for you):

 local c = require("component")
local computer = require("computer")
local internet = require("internet")
local fs = require("filesystem")
local p = c.printer3d
args = {...}

link = "https://raw.githubusercontent.com/LordNocturnus/sf-"
folder = "/master/"
imax = 0

local TIMEOUT = 4  -- in seconds

for i = 0, imax do
 local url = link .. i .. folder .. "list.txt"
 local status, connection = pcall(internet.request, url)
 if not status or not connection then
  print("Download of " .. url .. " failed.")
 else
  local startTime = computer.uptime()
  while true do
   local response, isFinished = pcall(connection.finishConnect)
   if response and isFinished then
    local f = io.open("list.txt", "wb")
--    print(status)
--    print(response)
    print(isFinished)
--    print(connection)
    for chunk in connection do
     f:write(chunk)
    end
    f:close()
    break
   elseif computer.uptime() - startTime > TIMEOUT then
    print("Request to " .. url .. " timed out")
    os.exit()
   else
    os.sleep(0.25)
   end
  end
 end
 for line in io.lines('list.txt') do
  print(line)
  local url = link .. i .. folder .. line .. ".mb3d"
  local status, connection = pcall(internet.request, url)
  if not status or not connection then
   print("Download of " .. url .. " failed.")
  else
   local startTime = computer.uptime()
   while true do
    local response, isFinished = pcall(connection.finishConnect)
    if response and isFinished then
     local f = io.open(line .. ".mb3d", "wb")
--     print(status)
--     print(response)
     print(isFinished)
--     print(connection)
     for chunk in connection do
      f:write(chunk)
     end
     f:close()
     break
    elseif computer.uptime() - startTime > TIMEOUT then
     print("Request to " .. url .. " timed out")
     os.exit()
    else
     os.sleep(0.25)
    end
   end
  end
  cords = {}
  for i in string.gmatch(line,"([^-]+)") do
   cords[#cords+1] = tonumber(i)
  end
  print(cords[1],cords[2],cords[3])
--  printer(cords[1],cords[2],cords[3]) -- do something with the file
  file = string.format("%d-%d-%d.mb3d", cords[1],cords[2],cords[3])
  fs.remove(file)
 end
end 

 

ok then when thats done let me see...

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.