text
stringlengths
0
897
if info.isDirectory then
// We've found a subdirectory. Print matches for THAT!
printMatches info.path
else if isMatch(info) then
// This file looks like a good match.
print info.path
end if
end for
end function
// Main program. Let's just operate on the current directory
// (wherever this script was run).
printMatches file.curdir
```
Notice the trick here where the `printMatches` function calls itself for any subfolder it runs across. This trick is called *recursion*.
recursion
: a programming technique where a function calls itself (directly or indirectly)
Recursion works because variables are local, so the variable `f` (used in the `for` loop) in one call is not the same as the variable `f` in the next call. Every call of the function gets its own local variables, so MiniScript never gets confused or loses track of what it was doing.
Note that if you type in the above exactly as it is, and run it, it probably won't print anything. That's because you probably don't have a text file created on April 27, 2020 somewhere under the folder where you run this. So adjust the criteria in the `isMatch` function to fit some file you know you have, and be sur...
## Creating, Reading, and Altering Files
The rest of the methods in the `file` module are various ways to read or change the contents of your disk. Most of these assume that you're working with text files, which includes specialized kinds of text like HTML (web pages), MiniScript script files, and so on.
To begin, let's create a new file with MiniScript. You can do this in the command-line REPL:
```terminal
> f = "Test.txt"
> file.writeLines f, ["Hello world!", "This is a test."]
29
```
D> Remember, when examples are given for the REPL, you only type what's next to the `>` prompt. Lines without a prompt are output from MiniScript; you don't need to type those.
Line 1 in the example above assigns a file name to `f`, and then the `file.writeLines` call on line 2 creates a file with that name in the default directory. The contents of the file are the second parameter to `file.writeLines`. This should be either a string, or a list of strings. The return value (29 in this exam...
To verify that it worked, switch over to File Explorer or Finder, and look in the folder you were using as your current directory. You should see a new file there called "Test.txt", and if you open it up, you'll find it contains two lines, "Hello world!" and "This is a test."
Switching back to MiniScript, you can verify that the file is there with `file.children`. Then, let's read it from disk back into memory:
```terminal
> data = file.readLines(f)
> data
["Hello world!", "This is a test."]
```
Isn't that easy? You already know how to work with strings and lists, so now that you can read and write a file as a list of strings, you can do almost anything with text files on disk! Keep in mind that our file specifier here ("Test.txt") was just a file name, which is a minimal kind of partial path. You can also ...
Now let's wrap up with other file operations you might need to do from time to time. To make a copy of the file, just call `file.copy` with the path (partial or absolute) to the original file, and where you want the copy to be:
```terminal
> file.copy f, "Test Copy.txt"
1
```
And now delete that copy using — you guessed it — `file.delete`:
```terminal
> file.delete "Test Copy.txt"
1
```
If you want to move or rename a file, use `file.move`. These sound like two different operations, but to the computer, they're really the same thing: changing the path at which a file is stored. You can think of it as "moving" the file if you specify a path to a different directory, or merely "renaming" the file if y...
```terminal
> file.move f, "Toast.txt"
1
```
Finally, if you need to create a directory in MiniScript, you can use `file.makedir`.
```terminal
> file.makedir "TestStuff"
1
> file.move "Toast.txt", file.child("TestStuff", "Toast.txt")
1
```
In this example, the `file.makedir` call on line 1 creates a new folder, and then on line 3 we move the "Toast.txt" file into it, using `file.child` to construct a proper partial path for the destination. That would be `TestStuff\\Toast.txt` on Windows, or `TestStuff/Toast.txt` on other platforms.
## Using Your New Superpower
As you become a programmer, you will be working with lots of files. When you make games, you will have folders full of sprite images and sounds. If you manipulate scientific data, you'll have files of data points that need summarized, analyzed, and collated. If you do business programming, you'll have files of sales...
When most people realize they need to rename, sort through, or otherwise work on hundreds or thousands of files, they have to either find some specialized tool that does that, or else they simply give up and abandon the idea. But you're not most people. You are learning how to code, and that makes tasks possible for ...
I can't tell you exactly what those tasks will be and how to do them, because they're likely to be very specialized to your needs. If they were *general* needs that lots of people have, there would probably already be some easy solution programmed by somebody else. The real power of programming is that you can solve ...
So, start thinking about it. When you encounter a problem on a computer, especially if it involves some repetitive task that would have to be done a mind-numbingly large number of times, consider whether you could write a program to do it for you. It might take you an hour or two to write and debug the program — but ...
Just to get those juices flowing, let's work through a small example. Let's start by creating 14 fake data files called `Data1.dat` through `Data14.dat`:
```terminal
> for i in range(1,14)
>>> file.writeLines "Data" + i + ".dat", "42"
>>> end for