1Filename(3) OCaml library Filename(3)
2
3
4
6 Filename - Operations on file names.
7
9 Module Filename
10
12 Module Filename
13 : sig end
14
15
16 Operations on file names.
17
18
19
20
21
22
23 val current_dir_name : string
24
25 The conventional name for the current directory (e.g. . in Unix).
26
27
28
29 val parent_dir_name : string
30
31 The conventional name for the parent of the current directory (e.g. ..
32 in Unix).
33
34
35
36 val dir_sep : string
37
38 The directory separator (e.g. / in Unix).
39
40
41 Since 3.11.2
42
43
44
45 val concat : string -> string -> string
46
47
48 concat dir file returns a file name that designates file file in direc‐
49 tory dir .
50
51
52
53 val is_relative : string -> bool
54
55 Return true if the file name is relative to the current directory,
56 false if it is absolute (i.e. in Unix, starts with / ).
57
58
59
60 val is_implicit : string -> bool
61
62 Return true if the file name is relative and does not start with an ex‐
63 plicit reference to the current directory ( ./ or ../ in Unix), false
64 if it starts with an explicit reference to the root directory or the
65 current directory.
66
67
68
69 val check_suffix : string -> string -> bool
70
71
72 check_suffix name suff returns true if the filename name ends with the
73 suffix suff .
74
75 Under Windows ports (including Cygwin), comparison is case-insensitive,
76 relying on String.lowercase_ascii . Note that this does not match ex‐
77 actly the interpretation of case-insensitive filename equivalence from
78 Windows.
79
80
81
82 val chop_suffix : string -> string -> string
83
84
85 chop_suffix name suff removes the suffix suff from the filename name .
86
87
88 Raises Invalid_argument if name does not end with the suffix suff .
89
90
91
92 val chop_suffix_opt : suffix:string -> string -> string option
93
94
95 chop_suffix_opt ~suffix filename removes the suffix from the filename
96 if possible, or returns None if the filename does not end with the suf‐
97 fix.
98
99 Under Windows ports (including Cygwin), comparison is case-insensitive,
100 relying on String.lowercase_ascii . Note that this does not match ex‐
101 actly the interpretation of case-insensitive filename equivalence from
102 Windows.
103
104
105 Since 4.08
106
107
108
109 val extension : string -> string
110
111
112 extension name is the shortest suffix ext of name0 where:
113
114
115 - name0 is the longest suffix of name that does not contain a directory
116 separator;
117
118 - ext starts with a period;
119
120 - ext is preceded by at least one non-period character in name0 .
121
122 If such a suffix does not exist, extension name is the empty string.
123
124
125 Since 4.04
126
127
128
129 val remove_extension : string -> string
130
131 Return the given file name without its extension, as defined in File‐
132 name.extension . If the extension is empty, the function returns the
133 given file name.
134
135 The following invariant holds for any file name s :
136
137
138 remove_extension s ^ extension s = s
139
140
141
142 Since 4.04
143
144
145
146 val chop_extension : string -> string
147
148 Same as Filename.remove_extension , but raise Invalid_argument if the
149 given name has an empty extension.
150
151
152
153 val basename : string -> string
154
155 Split a file name into directory name / base file name. If name is a
156 valid file name, then concat (dirname name) (basename name) returns a
157 file name which is equivalent to name . Moreover, after setting the
158 current directory to dirname name (with Sys.chdir ), references to
159 basename name (which is a relative file name) designate the same file
160 as name before the call to Sys.chdir .
161
162 This function conforms to the specification of POSIX.1-2008 for the
163 basename utility.
164
165
166
167 val dirname : string -> string
168
169 See Filename.basename . This function conforms to the specification of
170 POSIX.1-2008 for the dirname utility.
171
172
173
174 val null : string
175
176
177 null is "/dev/null" on POSIX and "NUL" on Windows. It represents a file
178 on the OS that discards all writes and returns end of file on reads.
179
180
181 Since 4.10.0
182
183
184
185 val temp_file : ?temp_dir:string -> string -> string -> string
186
187
188 temp_file prefix suffix returns the name of a fresh temporary file in
189 the temporary directory. The base name of the temporary file is formed
190 by concatenating prefix , then a suitably chosen integer number, then
191 suffix . The optional argument temp_dir indicates the temporary direc‐
192 tory to use, defaulting to the current result of File‐
193 name.get_temp_dir_name . The temporary file is created empty, with
194 permissions 0o600 (readable and writable only by the file owner). The
195 file is guaranteed to be different from any other file that existed
196 when temp_file was called.
197
198
199 Before3.11.2 no ?temp_dir optional argument
200
201
202
203 Raises Sys_error if the file could not be created.
204
205
206
207 val open_temp_file : ?mode:open_flag list -> ?perms:int ->
208 ?temp_dir:string -> string -> string -> string * out_channel
209
210 Same as Filename.temp_file , but returns both the name of a fresh tem‐
211 porary file, and an output channel opened (atomically) on this file.
212 This function is more secure than temp_file : there is no risk that the
213 temporary file will be modified (e.g. replaced by a symbolic link) be‐
214 fore the program opens it. The optional argument mode is a list of ad‐
215 ditional flags to control the opening of the file. It can contain one
216 or several of Open_append , Open_binary , and Open_text . The default
217 is [Open_text] (open in text mode). The file is created with permis‐
218 sions perms (defaults to readable and writable only by the file owner,
219 0o600 ).
220
221
222 Before4.03.0 no ?perms optional argument
223
224
225
226 Before3.11.2 no ?temp_dir optional argument
227
228
229
230 Raises Sys_error if the file could not be opened.
231
232
233
234 val get_temp_dir_name : unit -> string
235
236 The name of the temporary directory: Under Unix, the value of the TM‐
237 PDIR environment variable, or "/tmp" if the variable is not set. Under
238 Windows, the value of the TEMP environment variable, or "." if the
239 variable is not set. The temporary directory can be changed with File‐
240 name.set_temp_dir_name .
241
242
243 Since 4.00.0
244
245
246
247 val set_temp_dir_name : string -> unit
248
249 Change the temporary directory returned by Filename.get_temp_dir_name
250 and used by Filename.temp_file and Filename.open_temp_file .
251
252
253 Since 4.00.0
254
255
256
257 val temp_dir_name : string
258
259 Deprecated. You should use Filename.get_temp_dir_name instead.
260
261
262 The name of the initial temporary directory: Under Unix, the value of
263 the TMPDIR environment variable, or "/tmp" if the variable is not set.
264 Under Windows, the value of the TEMP environment variable, or "." if
265 the variable is not set.
266
267
268 Since 3.09.1
269
270
271
272 val quote : string -> string
273
274 Return a quoted version of a file name, suitable for use as one argu‐
275 ment in a command line, escaping all meta-characters. Warning: under
276 Windows, the output is only suitable for use with programs that follow
277 the standard Windows quoting conventions.
278
279
280
281 val quote_command : string -> ?stdin:string -> ?stdout:string ->
282 ?stderr:string -> string list -> string
283
284
285 quote_command cmd args returns a quoted command line, suitable for use
286 as an argument to Sys.command , Unix.system , and the Unix.open_process
287 functions.
288
289 The string cmd is the command to call. The list args is the list of
290 arguments to pass to this command. It can be empty.
291
292 The optional arguments ?stdin and ?stdout and ?stderr are file names
293 used to redirect the standard input, the standard output, or the stan‐
294 dard error of the command. If ~stdin:f is given, a redirection < f is
295 performed and the standard input of the command reads from file f . If
296 ~stdout:f is given, a redirection > f is performed and the standard
297 output of the command is written to file f . If ~stderr:f is given, a
298 redirection 2> f is performed and the standard error of the command is
299 written to file f . If both ~stdout:f and ~stderr:f are given, with
300 the exact same file name f , a 2>&1 redirection is performed so that
301 the standard output and the standard error of the command are inter‐
302 leaved and redirected to the same file f .
303
304 Under Unix and Cygwin, the command, the arguments, and the redirections
305 if any are quoted using Filename.quote , then concatenated. Under
306 Win32, additional quoting is performed as required by the cmd.exe shell
307 that is called by Sys.command .
308
309
310 Since 4.10.0
311
312
313 Raises Failure if the command cannot be escaped on the current plat‐
314 form.
315
316
317
318
319
320OCamldoc 2022-07-22 Filename(3)