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

File System for OS

Started by funkey100, 25 August 2012 - 03:24 AM
funkey100 #1
Posted 25 August 2012 - 05:24 AM
I'm wanting to make an operating system and I just don't know something and I really need some help. I need to know how to know which folder I'm in, how to know what files and folders are in that folder, and how to change folders. I tried looked at the wiki but I didn't understand. I also need to know how to make a GUI. For example, how to have a border around and some tools below (copy, paste, delete, move, new file/dir e.g.) and then have the folders and files in the middle, the only part you move unless you want to use the tools. Thanks!
Grim Reaper #2
Posted 25 August 2012 - 07:01 AM
If read around in the wiki I'm sure you'd find what you need, but I'll help you out :D/>/>

For getting your current location in the computer you could use this method:

shell.dir()

To switch folders use this method:

shell.setDir( string )

To get the list of files and directories in a current folder use this method to load a table:

tFiles = fs.list( shell.dir() )

Try looking into a couple files that already exist on the forums. I have already written a file browser that is GUI based if you'd like to check it out.
If you have anymore questions, or need specific examples, I would be happy to answer or provide them.
cant_delete_account #3
Posted 25 August 2012 - 07:51 PM
To get the list of files and directories in a current folder use this method to load a table:

tFiles = fs.list( shell.dir() )
To print them out, you could use something like this:

local files = fs.list(shell.dir())
for i,file in pairs(files) do
if fs.isDir(file) then
print("[Directory] ".file)
else
print("[File] ".file)
end
end
funkey100 #4
Posted 25 August 2012 - 08:31 PM
OK, thanks. But what does string in pairs(table) do? Also, how is the table tFiles ordered? Alphabetically? Alphabetical Folders then files? Also, in the setDir, do you have to set the whole path or a folder in the folder you are in? For example,when you are in games/tetris/ (just example) and you want to go to games/tetris/files do you use this: shell.setDir("games/tetris/files") or do you use this: shell.setDir("files")? Thanks. If you find out any good GUI tutorial, please tell me!
funkey100 #5
Posted 26 August 2012 - 01:31 AM
So what is ipairs() and pairs()? I'm trying to find out.
cant_delete_account #6
Posted 26 August 2012 - 01:42 AM
So what is ipairs() and pairs()? I'm trying to find out.
They go through the table. Like

for i, thestring in pairs({"lol","other"}) do
print(thestring)
end
Will print lol, then other.
funkey100 #7
Posted 26 August 2012 - 01:45 AM
So I find out that the fs.list() makes a sparse array which means it doesn't have an order. Does anyone know how to make it into an order like alphabetically? Also, I still don't understand the difference between in pairs() and in ipairs(). Plus, do they give out false when there is no more numbers because if they give out false in that slot, it stops the loop? Can you please tell me what they are?
Grim Reaper #8
Posted 26 August 2012 - 02:01 AM
So what is ipairs() and pairs()? I'm trying to find out.

A better explanation of

ipairs()
and

pairs()
would be that they both return an iterator function, or a function that is a loop but provides specific output defined by the function.

ipairs() iterates through only values in a table that are indexed by a number.
If you had a table such as this:

t = { [1] = 1, [2] = 2, [3] = 3, ["Four"] = 4 }

And you tried iterating through it with ipairs(), your output might look like this:

t = { [1] = 1, [2] = 2, [3] = 3, ["Four"] = 4 }


for index,value in ipairs( t ) do
   print( value )
end

-- OUTPUT --
1
2
3
----------------
The reason for this difference is that ipairs() is slightly faster than pairs().

If you tried the same thing with pairs, you would get something like this:


t = { [1] = 1, [2] = 2, [3] = 3, ["Four"] = 4 }


for index,value in ipairs( t ) do
   print( value )
end

-- OUTPUT --
1
2
3
4
----------------

In this case both indexes that are numerical and textual are printed. The difference in speed won't really matter unless you're being EXTREMELY conservative of your processing time.

As for

fs.list()
returning an unorganized table, this is not true from my experience.
From my understanding and an experiment I just ran, fs.list() returns an alphabetized list of all the files and directories in the directory passed as a parameter; directories or files take no precedence over each other in terms of alphabetization.


shell.setDir()
The above code's only parameter is a FULL path.
You have to specify the whole path or else it will go from the root directory to try and find the specified directory passed.

However, if you wanted to bypasses this problem and simulate jumping from the current directory to one inside the current directory you could try this:

shell.setDir( shell.dir() .. "/files" )
funkey100 #9
Posted 26 August 2012 - 02:08 AM
Thanks a lot! But it doesn't matter which one is I use for the list because it is not an ordered list, right? So do you know how you can make it an alphabetical list with the fs.list()?
Grim Reaper #10
Posted 26 August 2012 - 02:27 AM
fs.list() returns a list of every file and directory in the directory passed in alphabetical order.

If I had the following files in my root directory:

a
b
c
d
e
f
g
h
rom

If I were to write the following program into file 'a':

tFiles = fs.list( "/" ) -- Get a list of files in the root directory.

for index,value in ipairs( tFiles ) do -- Iterate through each numerically indexed value of tFiles
  print( value ) -- Print the value held by the current index.
end
and ran file 'a', the output would be:

CraftOS 1.4
> a
a
b
c
d
e
f
g
h
rom
> _

As you can see, fs.list() returns an already alphabetized list of the files and directories in the passed directory; in this case we pass the root directory.
It doesn't matter if you use ipairs() or pairs() in this case because there are no textually indexed values returned by fs.list().
funkey100 #11
Posted 26 August 2012 - 02:43 AM
When I use fs.list() it returns them into the most recently modified order. Also, in the for loop, instead of putting index I put _ for the first argument. What is the difference?
Grim Reaper #12
Posted 26 August 2012 - 02:46 AM
That's strange….

_ is typically used as a dummy variable. You can still reference it by using _ from what I understand. It's just another identifier, but it's used as kind of a universal identifier for a dummy variable (a variable that you don't use at all) between LUA developers.
funkey100 #13
Posted 26 August 2012 - 02:49 AM
Oh… and what does index do?
Grim Reaper #14
Posted 26 August 2012 - 03:11 AM
'index' is just an identifier; a name for a variable. You could call 'index' anything you wanted and it would be the same thing.

In this case, index represents the key or index that we are currently in as we iterate through the table.

If we had the following table:

t = { 1, 2, 3, 4, 5 }

Then index 2 would have a value of two.

t[2] = 2
funkey100 #15
Posted 26 August 2012 - 03:35 AM
OK, thanks. I'll try to fix the list ordering in most recently modified instead of alphabetically and if anyone finds out what could be wrong, please tell me. Also, if anyone knows any good tutorial or something of how to make a GUI (what I mean by GUI because there are some different meanings is like having a boarder around my OS and some tools below like copy, paste, delete, move, new file/dir e.g.) please tell me!
funkey100 #16
Posted 26 August 2012 - 05:13 AM
Example of a GUI I want (not gonna be anything like that):

/------------------------------
|          ExampleOS           |
|------------------------------|
| Rom                          |
| Disk                         |
______________________________/
/   Copy | Paste | New | Del   
______________________________/
funkey100 #17
Posted 26 August 2012 - 02:11 PM
I figured out how to make the GUI myself with any help or tutorials but I need some help… How can you rename a file/directory? Thanks! My operating system in turning out awesome on it's Alpha 0.01 :D/>/> I will release the alpha version soon. Should I release the alpha version or wait until I have a more stable, beta version?
cant_delete_account #18
Posted 26 August 2012 - 05:55 PM
I figured out how to make the GUI myself with any help or tutorials but I need some help… How can you rename a file/directory? Thanks! My operating system in turning out awesome on it's Alpha 0.01 :D/>/> I will release the alpha version soon. Should I release the alpha version or wait until I have a more stable, beta version?
To rename a file or directory, do:

fs.move( shell.dir() .. "FILENAME1", shell.dir() .. "FILENAME2")
Where "FILENAME1" would be the original file name, like "system", then "FILENAME2" would be the one you want to rename it to, like "sys".
funkey100 #19
Posted 26 August 2012 - 07:15 PM
Oh… That's very smart… and I'm very stupid…
funkey100 #20
Posted 26 August 2012 - 10:29 PM
I have one problem with the read(). When you create a new file/directory, it makes a box like (example is directory):

|------------------------------------------------|
|New directory's name:				 |
|						 |
|------------------------------------------------|

The 8 |s on the sides were already there. I just used the setCursorPos to put in all the -s, the top line of text and then i used it to go to the bottom. In the bottom, I used read() (and yes is was term.setCursorPos(2,i) and not term.setCursorPos(1,i)) but the right | disappeared because read() reads the whole line after it is used. Does anyone know a solution? Maybe how to make it read 48 lines instead of all?