Summary:
See also: Built-in Functions
The Path class provides functions to manipulate files and directories on the machine where the BDL program executes.
This API is provided as a Dynamic C Extension library; it is part of the standard package.
To use this extension, you must import the os package in your program:
IMPORT os
Note that in order to manipulate files, this API give you access to low-level system functions. Pay attention to operating system specific conventions like path separators. Some functions are OS specific, like rwx() which works only on UNIX systems.
The Path class provides an interface to manipulate files and directories.
os.Path
Class Methods | |
Name | Description |
separator | Returns the character used to separate path segments. |
pathseparator | Returns the character used in environment variables to separate path elements. |
basename | Returns the last element of a path. |
dirname | Returns all components of a path excluding the last one. |
rootname | Returns the file path without the file extension of the last element of the file path. |
join | Joins two path segments adding the platform-dependent separator. |
pathtype | Checks whether a path is a relative path or an absolute path. |
exists | Checks if a file exists. |
extension | Returns the file extension. |
readable | Checks if a file is readable. |
writable | Checks if a file is writable. |
executable | Checks if a file is executable. |
isfile | Checks if a file is a regular file. |
isdirectory | Checks if a file is a directory. |
ishidden | Checks if a file is hidden. |
islink | Checks if a file is a UNIX symbolic link. |
isroot | Checks if a file is a root path. |
type | Returns the file type as a string. |
size | Returns the file size. |
atime | Returns the time of the last file access. |
chown | Changes the UNIX owner and group of a file. |
uid | Returns the UNIX user id of the file. |
gid | Returns the UNIX group id of the file. |
rwx | Returns the UNIX permissions on the file. |
chrwx | Changes the UNIX permissions of a file. |
mtime | Returns the time of the last file modification. |
homedir | Returns the path to the HOME directory of the current user. |
rootdir | Returns the root directory of the current path. |
dirfmask | Defines the filter mask for a diropen call |
dirsort | Defines the sort criteria and sort order for a diropen call |
diropen | Opens a directory and returns an integer handle to this directory. |
dirclose | Closes the directory referenced by the directory handle. |
dirnext | Reads the next entry of the directory referenced by the directory handle. |
pwd | Returns the current working directory. |
chdir | Changes the current working directory |
volumes | Returns the list of available volumes. |
chvolume | Changes the current working volume. |
mkdir | Creates a new directory. |
delete | Deletes a file or a directory. |
rename | Renames a file or a directory. |
copy | Copies a regular file. |
Returns the character used to separate path segments.
CALL os.Path.separator() RETURNING separator STRING
Returns the character used in environment variables to separate path elements.
CALL os.Path.pathseparator() RETURNING separator STRING
You typically use this method to build a path from two components.
This method returns the last element of a path.
CALL os.Path.basename(filename STRING) RETURNING basename STRING
This method extracts the last component of a path. For example, if you pass "/root/dir1/file.ext" as the parameter, it will return "file.ext".
See Example 1 for more examples.
Returns all components of a path excluding the last one.
CALL os.Path.dirname(filename STRING) RETURNING dirname STRING
This method removes the last component of a path. For example, if you pass "/root/dir1/file.ext" as the parameter, it will return "/root/dir1".
See Example 1 for more examples.
Returns the file path without the file extension of the last element of the file path.
CALL os.Path.rootname(filename STRING) RETURNING rootname STRING
This method removes the file extension from the path. For example, if you pass "/root/dir1/file.ext" as the parameter it will return "/root/dir1/file".
See Example 1 for more examples.
Joins two path segments adding the platform-dependent separator.
CALL os.Path.join(begin STRING, end STRING) RETURNING newpath STRING
You typically use this method to construct a path with no system-specific code to use the correct path separator:
01
LET path = os.Path.join(os.Path.homedir(), name)
This method returns the ending path segment if it is an absolute path.
Checks if a path is a relative path or an absolute path.
CALL os.Path.pathtype(path STRING) RETURNING pathtype STRING
Checks if a file exists.
CALL os.Path.exists(fname STRING) RETURNING result INTEGER
Returns the file extension.
CALL os.Path.extension(fname STRING) RETURNING extension
STRING
Checks if a file is readable.
CALL os.Path.readable(fname STRING) RETURNING result INTEGER
Checks if a file is writable.
CALL os.Path.writable(fname STRING) RETURNING result INTEGER
Checks if a file is executable.
CALL os.Path.executable(fname STRING) RETURNING result INTEGER
Checks if a file is a regular file.
CALL os.Path.isfile(fname STRING) RETURNING result INTEGER
Checks if a file is a directory.
CALL os.Path.isdirectory(fname STRING) RETURNING result INTEGER
Checks if a file is hidden.
CALL os.Path.ishidden(fname STRING) RETURNING result INTEGER
Checks if a file is UNIX symbolic link.
CALL os.Path.islink(fname STRING) RETURNING result INTEGER
This method can only be used on UNIX!
Checks if a file path is a root path.
CALL os.Path.isroot(path STRING) RETURNING result STRING
Returns the file type as a string
CALL os.Path.type(fname STRING) RETURNING ftype STRING
On UNIX, this method follows symbolic links. You must use the islink() method to identify symbolic links.
Returns the size of a file.
CALL os.Path.size(fname STRING) RETURNING size INTEGER
Returns the time of the last file access.
CALL os.Path.atime(fname STRING) RETURNING atime STRING
Returns the time of the last file modification.
CALL os.Path.mtime(fname STRING) RETURNING mtime STRING
Returns the UNIX file permissions of a file.
CALL os.Path.rwx(fname STRING) RETURNING mode INTEGER
This method can only be used on UNIX!
The mode is returned as a decimal value which is the combination of read, write and execution bits for the user, group and other part of the UNIX file permission. For example, if a file has the -rwxr-xr-x permissions, you get ( (4+2+1) * 64 + (4+1) * 8) + (4+1) ) = 493 from this method.
Changes the UNIX permissions of a file.
CALL os.Path.chrwx(fname STRING, mode INTEGER) RETURNING res
INTEGER
This method can only be used on UNIX!
The mode must be a decimal value which is the combination of read, write and execution bits for the user, group and other part of the UNIX file permission. Make sure to pass the mode as the decimal version of permissions, not as octal (the chrwx UNIX command takes an octal value as parameter). For example, to set -rw-r--r-- permissions, you must pass ( ((4+2) * 64) + (4 * 8) + 4 ) = 420 to this method.
Changes the UNIX owner and group of a file.
CALL os.Path.chown(fname STRING, uid INT, gui INT) RETURNING res
INTEGER
This method can only be used on UNIX!
Returns the UNIX user id of a file.
CALL os.Path.uid(fname STRING) RETURNING id INTEGER
This method can only be used on UNIX!
Returns the UNIX group id of a file.
CALL os.Path.gid(fname STRING) RETURNING id INTEGER
This method can only be used on UNIX!
Returns the path to the HOME directory of the current user.
CALL os.Path.homedir() RETURNING homedir STRING
Returns the root directory of the current working path.
CALL os.Path.rootdir() RETURNING rootdir STRING
Defines a filter mask for diropen.
CALL os.Path.dirfmask(mask INTEGER)
When you call this function, you define the filter mask for any subsequent diropen call.
By default, all kinds of directory entries are selected by the diropen function. You can restrict the number of entries by using a filter mask.
The parameter of the dirfmask function must be a combination of the following bits:
For example, to retrieve only regular files, you must call:
01
CALL os.Path.dirfmask( 1 + 2 + 4 )
Defines the sort criteria and sort order for diropen.
CALL os.Path.dirsort(criteria STRING, order INTEGER)
When you call this function, you define the sort criteria and sort order for any subsequent diropen call.
The criteria parameter must be one of the following strings:
When sorting by name, directory entries will be ordered according to the current locale.
When sorting by any criteria other than the file name, entries having the same value for the given criteria are ordered by name following the value of the order parameter.
Opens a directory and returns an integer handle to this directory.
CALL os.Path.diropen(dname STRING) RETURNING dirhandle INTEGER
This function creates a list of directory
Closes the directory referenced by the directory handle dirhandle.
CALL os.Path.dirclose(dirhandle INTEGER)
Reads the next entry in the directory.
CALL os.Path.dirnext(dirhandle INTEGER) RETURNING direntry STRING
Returns the current working directory.
CALL os.Path.pwd() RETURNING cwd STRING
Changes the current working directory.
CALL os.Path.chdir(newdir STRING) RETURNING result INTEGER
Returns the available volumes.
CALL os.Path.volumes() RETURNING volumes STRING
Changes the current working volume.
CALL os.Path.chvolume(newvolume STRING) RETURNING result INTEGER
Creates a new directory.
CALL os.Path.mkdir(dname STRING) RETURNING result INTEGER
Deletes a file or a directory.
CALL os.Path.delete(dname STRING) RETURNING result INTEGER
Renames a file or a directory.
CALL os.Path.rename(oldname STRING, newname STRING) RETURNING result INTEGER
Creates a new file by copying an existing file.
CALL os.Path.copy(source STRING, dest STRING) RETURNING result INTEGER
This program uses the file functions to extract the directory name, the base name, the root name, and the file extension:
01
IMPORT os02
MAIN03
DISPLAY "Dir name = ", os.Path.dirname(arg_val(1))04
DISPLAY "Base name = ", os.Path.basename(arg_val(1))05
DISPLAY "Root name = ", os.Path.rootname(arg_val(1))06
DISPLAY "Extension = ", os.Path.extension(arg_val(1))07
END MAIN
Example results:
Path | os.Path.dirname | os.Path.basename | os.Path.rootname | os.Path.extension |
. | . | . | NULL | |
.. | . | .. | . | NULL |
/ | / | / | / | NULL |
/usr/lib | /usr | lib | /usr/lib | NULL |
/usr/ | / | usr | /usr/ | NULL |
usr | . | usr | usr | NULL |
file.xx | . | file.xx | file | xx |
/tmp.yy/file.xx | /tmp.yy | file.xx | /tmp.yy/file | xx |
/tmp.yy/file.xx.yy | /tmp.yy | file.xx.yy | /tmp.yy/file.xx | yy |
/tmp.yy/ | / | tmp.yy | /tmp.yy/ | NULL |
/tmp.yy/. | /tmp.yy | . | /tmp.yy/ | NULL |
Note that the above examples use Unix file names. On Windows the result would be different, as the file name separator is '\'.
This program takes a directory path as an argument and scans the content recursively:
01
IMPORT os02
MAIN03
CALL showDir(arg_val(1))04
END MAIN05
FUNCTION showDir(path)06
DEFINE path STRING07
DEFINE child STRING08
DEFINE h INTEGER09
IF NOT os.Path.exists(path) THEN10
RETURN11
END IF12
IF NOT os.Path.isdirectory(path) THEN13
DISPLAY " ", os.Path.basename(path)14
RETURN15
END IF16
DISPLAY "[", path, "]"17
CALL os.Path.dirsort("name", 1)18
LET h = os.Path.diropen(path)19
WHILE h > 020
LET child = os.Path.dirnext(h)21
IF child IS NULL THEN EXIT WHILE END IF22
IF child == "." OR child == ".." THEN CONTINUE WHILE END IF23
CALL showDir( os.Path.join( path, child ) )24
END WHILE25
CALL os.Path.dirclose(h)26
END FUNCTION