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

Any way of not specifying a file in a disk?

Started by Corwell, 04 January 2018 - 04:43 AM
Corwell #1
Posted 04 January 2018 - 05:43 AM
Hi,

I'm making a little cinema for my server, and I was wondering if there was a way of running a file in a disk without specifying the filename, so the program will run any file in the disk so I won't have to keep changing the code whenever I want to run a different program. At the moment this is all I have (for running a disk).

shell.run("disk/INSERTFILENAMEHERE")

It's not really a necessary thing to have, I mean I could just rename every program I put in the disk drive to the same name, but if there is a way, I'll be happy.

Thanks in advance.
valithor #2
Posted 04 January 2018 - 06:06 AM
If the disk only contains a single item you could do this:

local files = fs.list("disk") --# get a list of all the files on the disk, fs.list returns a table
if #files > 0 then --# there are files on the disk
  if !fs.isDir("disk/"..files[1]) then --# making sure the entry in the table is not a directory
    shell.run("disk/"..files[1]) --# run the first file (Assuming there is only one on the disk)
  end
end

If there are multiple files you could do this to run them all:

local files = fs.list("disk")
for i = 1, #files do --# loop through each entry in the table
  if !fs.isDir("disk/"..files[i]) then
    shell.run("disk/"..files[i]) --# run each file in the table
  end
end
Edited on 04 January 2018 - 05:09 AM
Corwell #3
Posted 04 January 2018 - 06:11 AM
Thanks for the reply!
I'll give it a try.
Luca_S #4
Posted 04 January 2018 - 06:47 AM
If there are multiple files you could do this to run them all:

local files = fs.list("disk")
for i = 1, #files do --# loop through each entry in the table
  if !fs.isDir("disk/"..files[i]) then
	shell.run("disk/"..files[i]) --# run each file in the table
  end
end
Be aware that this does not recursively execute files.
disk/somefolder/file won't be executed.
Corwell #5
Posted 04 January 2018 - 07:05 AM
If the disk only contains a single item you could do this:

local files = fs.list("disk") --# get a list of all the files on the disk, fs.list returns a table
if #files > 0 then --# there are files on the disk
  if !fs.isDir("disk/"..files[1]) then --# making sure the entry in the table is not a directory
	shell.run("disk/"..files[1]) --# run the first file (Assuming there is only one on the disk)
  end
end

If there are multiple files you could do this to run them all:

local files = fs.list("disk")
for i = 1, #files do --# loop through each entry in the table
  if !fs.isDir("disk/"..files[i]) then
	shell.run("disk/"..files[i]) --# run each file in the table
  end
end

I've tried the code you suggested, and it doesn't work.
It's also kind of hard to know what went wrong because it doesn't give me an error, it doesn't do anything.

Thanks for the help anyway!
Purple #6
Posted 04 January 2018 - 10:12 AM
If you are making a cinema than I assume that for the sake of roleplaying you'd want to have each movie on a different disk. So why not just have them all have the same name? Like, it's always disk/movie and you just insert the correct one.
Bomb Bloke #7
Posted 05 January 2018 - 06:20 AM
It's also kind of hard to know what went wrong because it doesn't give me an error, it doesn't do anything.

That's odd, given that it's not valid Lua… I'd've at least expected an error message. Are you sure you ran the right file? In any case, valithor meant to use "not fs.isDir" instead of "!fs.isDir".