File.parent

From MiniScript Wiki
Revision as of 04:21, 17 July 2021 by JoeStrout (talk | contribs) (Created page with "The <c>file.parent</c> method returns the parent path of the given file or directory, that is, the path of the directory that contains it. === Arguments === {| class="wi...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The file.parent method returns the parent path of the given file or directory, that is, the path of the directory that contains it.

Arguments

Parameter Name Default Value Meaning
path "" file name, partial path, or absolute path to file or directory to get the parent of

If given a file name or partial path, it will be interpreted relative to the current working directory (see file.curdir).

Result

This method returns a string containing the parent directory of the given file or directory. Note: if given the root path, "/", it returns "/" again — this is the only directory that is its own parent.

Example

The following attempts to delete a file called "test.txt" in the current directory.

s = "/sys/pics/food/Sushi.png"
while true
    print file.name(s)
    s = file.parent(s)
    if s == "/" then break
    print "...which is contained in..."
end while

The above, if run on Mini Micro (or any system with a similar directory structure), prints:

Sushi.png
...which is contained in...
food
...which is contained in...
pics
...which is contained in...
sys