]> gitweb.factorcode.org Git - factor.git/blob - basis/io/directories/directories-docs.factor
6370fdb90d04255def2f25d79270c56c507183e0
[factor.git] / basis / io / directories / directories-docs.factor
1 USING: help.markup help.syntax io.files.private io.pathnames
2 quotations ;
3 IN: io.directories
4
5 HELP: cwd
6 { $values { "path" "a pathname string" } }
7 { $description "Outputs the current working directory of the Factor process." }
8 { $errors "Windows CE has no concept of “current directory”, so this word throws an error there." }
9 { $notes "User code should use the value of the " { $link current-directory } " variable instead." } ;
10
11 HELP: cd
12 { $values { "path" "a pathname string" } }
13 { $description "Changes the current working directory of the Factor process." }
14 { $errors "Windows CE has no concept of “current directory”, so this word throws an error there." }
15 { $notes "User code should use " { $link with-directory } " or " { $link set-current-directory } " instead." } ;
16
17 { cd cwd current-directory set-current-directory with-directory } related-words
18
19 HELP: current-directory
20 { $description "A variable holding the current directory as an absolute path. Words that use the filesystem do so in relation to this variable."
21 $nl
22 "This variable should never be set directly; instead, use " { $link set-current-directory } " or " { $link with-directory } ". This preserves the invariant that the value of this variable is an absolute path." } ;
23
24 HELP: set-current-directory
25 { $values { "path" "a pathname string" } }
26 { $description "Changes the " { $link current-directory } " variable."
27 $nl
28 "If " { $snippet "path" } " is relative, it is first resolved relative to the current directory. If " { $snippet "path" } " is absolute, it becomes the new current directory." } ;
29
30 HELP: with-directory
31 { $values { "path" "a pathname string" } { "quot" quotation } }
32 { $description "Calls the quotation in a new dynamic scope with the " { $link current-directory } " variable rebound."
33 $nl
34 "If " { $snippet "path" } " is relative, it is first resolved relative to the current directory. If " { $snippet "path" } " is absolute, it becomes the new current directory." } ;
35
36 HELP: (directory-entries)
37 { $values { "path" "a pathname string" } { "seq" "a sequence of " { $snippet "{ name dir? }" } " pairs" } }
38 { $description "Outputs the contents of a directory named by " { $snippet "path" } "." }
39 { $notes "This is a low-level word, and user code should call one of the related words instead." } ;
40
41 HELP: directory-entries
42 { $values { "path" "a pathname string" } { "seq" "a sequence of " { $link directory-entry } " objects" } }
43 { $description "Outputs the contents of a directory named by " { $snippet "path" } "." } ;
44
45 HELP: directory-files
46 { $values { "path" "a pathname string" } { "seq" "a sequence of filenames" } }
47 { $description "Outputs the contents of a directory named by " { $snippet "path" } "." } ;
48
49 HELP: with-directory-files
50 { $values { "path" "a pathname string" } { "quot" quotation } }
51 { $description "Calls the quotation with the directory file names on the stack and with the directory set as the " { $link current-directory } ".  Restores the current directory after the quotation is called." }
52 { $examples
53     "Print all files in your home directory which are larger than a megabyte:"
54     { $code
55         """USING: io.directories io.files.info io.pathnames ;
56 home [
57     [
58         dup link-info size>> 20 2^ >
59         [ print ] [ drop ] if
60     ] each
61 ] with-directory-files"""
62     }
63 } ;
64
65 HELP: with-directory-entries
66 { $values { "path" "a pathname string" } { "quot" quotation } }
67 { $description "Calls the quotation with the directory entries on the stack and with the directory set as the " { $link current-directory } ".  Restores the current directory after the quotation is called." } ;
68
69 HELP: delete-file
70 { $values { "path" "a pathname string" } }
71 { $description "Deletes a file." }
72 { $errors "Throws an error if the file could not be deleted." } ;
73
74 HELP: make-directory
75 { $values { "path" "a pathname string" } }
76 { $description "Creates a directory." }
77 { $errors "Throws an error if the directory could not be created." } ;
78
79 HELP: make-directories
80 { $values { "path" "a pathname string" } }
81 { $description "Creates a directory and any parent directories which do not yet exist." }
82 { $errors "Throws an error if the directories could not be created." } ;
83
84 HELP: delete-directory
85 { $values { "path" "a pathname string" } }
86 { $description "Deletes a directory. The directory must be empty." }
87 { $errors "Throws an error if the directory could not be deleted." } ;
88
89 HELP: touch-file
90 { $values { "path" "a pathname string" } }
91 { $description "Updates the modification time of a file or directory. If the file does not exist, creates a new, empty file." }
92 { $errors "Throws an error if the file could not be touched." } ;
93
94 HELP: move-file
95 { $values { "from" "a pathname string" } { "to" "a pathname string" } }
96 { $description "Moves or renames a file." }
97 { $errors "Throws an error if the file does not exist or if the move operation fails." } ;
98
99 HELP: move-file-into
100 { $values { "from" "a pathname string" } { "to" "a directory pathname string" } }
101 { $description "Moves a file to another directory without renaming it." }
102 { $errors "Throws an error if the file does not exist or if the move operation fails." } ;
103
104 HELP: move-files-into
105 { $values { "files" "a sequence of pathname strings" } { "to" "a directory pathname string" } }
106 { $description "Moves a set of files to another directory." }
107 { $errors "Throws an error if the file does not exist or if the move operation fails." } ;
108
109 HELP: copy-file
110 { $values { "from" "a pathname string" } { "to" "a pathname string" } }
111 { $description "Copies a file." }
112 { $notes "This operation attempts to preserve the original file's attributes, however not all attributes may be preserved." }
113 { $errors "Throws an error if the file does not exist or if the copy operation fails." } ;
114
115 HELP: copy-file-into
116 { $values { "from" "a pathname string" } { "to" "a directory pathname string" } }
117 { $description "Copies a file to another directory." }
118 { $errors "Throws an error if the file does not exist or if the copy operation fails." } ;
119
120 HELP: copy-files-into
121 { $values { "files" "a sequence of pathname strings" } { "to" "a directory pathname string" } }
122 { $description "Copies a set of files to another directory." }
123 { $errors "Throws an error if the file does not exist or if the copy operation fails." } ;
124
125 ARTICLE: "current-directory" "Current working directory"
126 "File system I/O operations use the value of a variable to resolve relative pathnames:"
127 { $subsections current-directory }
128 "This variable can be changed with a pair of words:"
129 { $subsections
130     set-current-directory
131     with-directory
132 }
133 "This variable is independent of the operating system notion of “current working directory”. While all Factor I/O operations use the variable and not the operating system's value, care must be taken when making FFI calls which expect a pathname. The first option is to resolve relative paths:"
134 { $subsections absolute-path }
135 "The second is to change the working directory of the current process:"
136 { $subsections
137     cd
138     cwd
139 } ;
140
141 ARTICLE: "io.directories.listing" "Directory listing"
142 "Directory listing:"
143 { $subsections
144     directory-entries
145     directory-files
146     with-directory-entries
147     with-directory-files
148 } ;
149
150 ARTICLE: "io.directories.create" "Creating directories"
151 { $subsections
152     make-directory
153     make-directories
154 } ;
155
156 ARTICLE: "delete-move-copy" "Deleting, moving, and copying files"
157 "The operations for moving and copying files come in three flavors:"
158 { $list
159     { "A word named " { $snippet { $emphasis "operation" } } " which takes a source and destination path." }
160     { "A word named " { $snippet { $emphasis "operation" } "-into" } " which takes a source path and destination directory. The destination file will be stored in the destination directory and will have the same file name as the source path." }
161     { "A word named " { $snippet { $emphasis "operation" } "s-into" } " which takes a sequence of source paths and destination directory." }
162 }
163 "Since both of the above lists apply to copying files, that this means that there are a total of six variations on copying a file."
164 $nl
165 "Deleting files:"
166 { $subsections
167     delete-file
168     delete-directory
169 }
170 "Moving files:"
171 { $subsections
172     move-file
173     move-file-into
174     move-files-into
175 }
176 "Copying files:"
177 { $subsections
178     copy-file
179     copy-file-into
180     copy-files-into
181 }
182 "On most operating systems, files can only be moved within the same file system. To move files between file systems, use " { $link copy-file } " followed by " { $link delete-file } " on the old name." ;
183
184 ARTICLE: "io.directories" "Directory manipulation"
185 "The " { $vocab-link "io.directories" } " vocabulary defines words for inspecting and manipulating directories."
186 { $subsections
187     home
188     "current-directory"
189     "io.directories.listing"
190     "io.directories.create"
191     "delete-move-copy"
192     "io.directories.hierarchy"
193 } ;
194
195 ABOUT: "io.directories"