Mathematica has a plethora of commands for working with files. For example,
returns a list of file names in the top-level directory in which Mathematica system files are installed. Note that the location and the form of file and directory names vary among computer systems.
Here is the (short) list of notebooks that are in subdirectories of the SystemFiles directory, up to three levels down.
Referencing a file using its full path is cumbersome. The name of the file can be extracted by StringDrop, using DirectoryName to obtain the directory name from the specification for a file.
Functions like ReadList are most often used for processing text and data from external files. In some cases, however, you may find it convenient to use these same functions to process strings. You can do this by using the function StringToStream, which opens an input stream that takes characters not from an external file, but instead from a string (see 2.11.10 of The Mathematica Book).
Then ReadList can be used to extract data from the stream, and convert it to a list. For example, we extract the names of all the palettes in subdirectories of the SystemFiles directory using Cases.
Using produces a separate record for each file, and has the side effect of shortening the file name by dropping the .nb extension. Also, breaking the path into individual components eliminates the need for a (machine-dependent) delimiter character and makes further manipulation easy, since the individual elements of the path can be addressed as elements of a list.
Failure to close streams after you are done with them will result in a slowdown in the kernel's performance so the stream should be closed after the operation is complete.
It is quite easy to produce a button that, when clicked, opens a specified file in a subdirectory of the system files directory ($TopDirectory).
Machine-dependent issues with paths are minimized by using to construct the file name when the button is clicked. This approach is strongly encouraged for users who wish to make their work easily transportable to other systems. Clicking the button
will open the AlgebraicManipulation.nb palette in the front end under any operating system.
A straightforward extension is to generate a palette with buttons for each file in a directory.
Partition is used here to produce a palette of manageable size with two items on each row. The following simple example demonstrates how this is done.
Padding with an empty string, "", is used to make the palette rectangular without repetition.
Copyright © 2002 Wolfram Media, Inc. All rights reserved.