Jump to content




Any way of not specifying a file in a disk?


6 replies to this topic

#1 Corwell

  • Members
  • 3 posts

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.

#2 valithor

  • Members
  • 1,053 posts

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 by valithor, 04 January 2018 - 06:09 AM.


#3 Corwell

  • Members
  • 3 posts

Posted 04 January 2018 - 06:11 AM

Thanks for the reply!
I'll give it a try.

#4 Luca_S

  • Members
  • 407 posts
  • LocationGermany

Posted 04 January 2018 - 06:47 AM

View Postvalithor, on 04 January 2018 - 06:06 AM, said:

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.

#5 Corwell

  • Members
  • 3 posts

Posted 04 January 2018 - 07:05 AM

View Postvalithor, on 04 January 2018 - 06:06 AM, said:

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!

#6 Purple

  • Members
  • 115 posts
  • LocationAlone in the dark, looking at the pretty lights with dreams of things I can not have.

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.

#7 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 05 January 2018 - 06:20 AM

View PostConwell, on 04 January 2018 - 07:05 AM, said:

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".





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users