text stringlengths 0 897 |
|---|
About half of the functions are just for manipulating paths. Try entering |
```repl |
> file.curdir |
``` |
This should print your current working directory. For me, that might be something like `/Users/jstrout/Documents`, but for you it will be different. That's OK. Now try |
```terminal |
> file.children |
``` |
It should return a list of files in your current directory. Hopefully those things look familiar! Note that it may include some entries that you don't see when you look in the same folder graphically, using Finder or File Explorer. Those are "hidden" files which the makers of macOS or Windows don't think you should ... |
Notice that we didn't give `file.children` a path, so it just returned the contents of the working directory. Now try it again, giving it either the name of a folder inside your working directory, or an absolute path to some other folder. For example, on my machine I can do: |
```terminal |
> file.children "/Users/jstrout" |
``` |
to get a list of all the stuff in my home directory. But of course you'll need to change that to some directory on *your* system. Experiment with this a bit. |
Now let's try changing the working directory. Pick some folder you were able to get the children of, then pass its path to `file.setdir`, like this: |
```terminal |
> file.setdir "/Users/jstrout" |
``` |
(again changing the path to something that exists on your machine). If successful, this will return 1 (which means `true` in MiniScript, but is also sometimes used to indicate success, like getting a thumbs-up from the function). Now if you enter `file.curdir`, it will report back the new working directory. Doing `f... |
Now let's consider how to build and take apart paths. Let's introduce a variable, `p` for "path" (using a short name just to save us some typing). Start by setting that to the current directory. |
```terminal |
> p = file.curdir |
> p |
``` |
This should print out the path of your working directory, which is an absolute path that probably contains several parts. Now pick the name of some file in that directory (use `file.children(p)` if you need to review what those files are). To get the path of a file relative to `p`, you can use the `file.child` method... |
```terminal |
> f = file.child(p, "SomeFile.txt") |
> f |
``` |
That function basically just appends the file name you give it to the path, inserting the appropriate path separator for your platform (i.e. a forward or backward slash). |
Now, let's do the reverse: strip the last part of the path off, so that it refers to the parent folder. |
```terminal |
> file.parent(f) |
``` |
This should strip the file name off, and get you back the path `p` that you started with. Of course you could go further, and get the parent folder of `p` itself (`file.parent(p)`), or any other path, at least until you run out of path parts. |
There is another way to split up a path: you might want just the *last* part, stripping off everything before the final file or folder name. |
```terminal |
> file.name(f) |
``` |
## Listing and Searching Files |
Let's combine what we've learned so far with one more function, `file.info`, that gets detailed a map of extra details about a file on disk. These details include `size` in bytes, `isDirectory` (1 for true or 0 for false), `date` that the file was last modified, and `path`, which is the absolute path to the file. Thi... |
{caption:List files with detials} |
```miniscript |
p = file.curdir |
for f in file.children(p).sort |
info = file.info(file.child(p, f)) |
padName = (f + " "*40)[:40] |
padSize = (" "*8 + info.size)[-8:] |
print padName + padSize + " " + info.date |
end for |
``` |
This prints out all the files in the current directory, along with their size and modification date, all in neatly formatted columns. A good demo, but not terribly useful since you can already browse the files with Finder or File Explorer. So how about something that those can't easily do? Let's say you were working... |
{caption:Find a file by any criteria} |
```miniscript |
// Find a file! Run this from some folder you know contains what |
// you're looking for, even if it's buried several folders deeper. |
// Return whether the given file info represents a good candidate. |
isMatch = function(info) |
// Here program whatever you can remember about the file |
// you're looking for. An easy way is to return false for |
// anything that is NOT what you want. |
if info.path[-4:] != ".txt" then return false |
if info.date[:10] != "2020-04-27" then return false |
if info.size < 1024 then return false |
// If none of the above apply, then this might be a good file! |
return true |
end function |
// Print the path of all good candidates in the given directory, |
// or any sub-folder (repeating to any depth). |
printMatches = function(dirPath) |
for f in file.children(dirPath) |
info = file.info(file.child(dirPath, f)) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.