|REBOL 3 Docs||Guide||Concepts||Functions||Datatypes||Errors|
|TOC < Back Next >||Updated: 6-Feb-2009 Edit History|
There are several easy-to-use functions for reading directories, managing subdirectories, making new directories, renaming files, and deleting files. In addition, there are standard functions for getting, changing, and listing the current directory. For more information on direct access to directories, see the ports Chapter.
Directories are read in the same manner as files. The read function returns a block of file names rather than text or binary data.
To read all the file names from the current directory, use the following line of code:
The above example reads the entire directory and returns a block of file names.
To print the names of all files in a directory, use the following line of code:
print read %intro/ CVS/ history.t intro.t overview.t quick.t
Within the returned block, names of directories are indicated with a trailing forward slash. To print each file name on a separate line, use:
foreach file read %intro/ [print file] CVS/ history.t intro.t overview.t quick.t
Here is an easy way to print just the directories that were found:
foreach file read %intro/ [ if #"/" = last file [print file] ] CVS/
If you want to read a directory from the network, be sure to include a forward slash at the end of the URL to indicate to the protocol that you are referring to a directory:
print read ftp://ftp.rebol.com/
The make-dir function makes a new directory. The new name for the directory can be relative to either the current directory or an absolute path.
make-dir %new-dir make-dir %local-dir/ make-dir %/work/docs/old-docs/
The trailing slash is optional for this function.
close open/new %local-dir/
also creates a new directory. The trailing slash is important in this example, indicating that a directory is to be created rather than a file.
If you use the make-dir function to create a directory that already exists, an error will occur. The error can be caught with the try function. The directory can be checked in advance with the exists? function.
To rename a file, use the rename function:
rename %old-file %new-file
The old file name may include a complete path to the file, but the new file name must not include a path. This is because the rename function is not intended to move files between directories (various operating systems do not provide this function).
rename %../docs/intro.txt %conclusion.txt
If the old file name is a directory (indicated by a trailing slash), the rename function renames the directory:
rename %../docs/ %manual/
If the file cannot be renamed, an error will occur. The error can be caught with the try function.
Files can be deleted using the delete function:
The file to delete can include a path:
A block of files within the same directory can also be deleted:
delete [%file1 %file2 %file3]
A group of files can be deleted using a wildcard character and the /any refinement:
delete/any %file* delete/any %secret.?
The asterisk () wildcard character matches all characters, and the question mark (?) wildcard character matches a single character.
To delete a directory, provide a trailing forward slash:
delete %dir/ delete %../docs/old/
If the file cannot be deleted, an error will occur. The error can be caught with the try function.
Use the what-dir function to determine the current directory:
print what-dir /work/REBOL/
The what-dir function refers to the current script's directory path, as found in system/script/path.
To change the current directory, use:
If the trailing slash is not included, the function adds it.
To list the contents of the current directory, use:
The number of columns used to show the directory is dependent on the console window size and the maximum file name length.
|TOC < Back Next >||REBOL.com - WIP Wiki||Feedback Admin|