Genero 技转没教的事 (01) – 文件系统处理
File Management Class
link to 4js.com:
See also: Built-in Functions
Basics
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
Warning: 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.
Syntax
The Path class provides an interface to manipulate files and directories.
Syntax:
os.Path
Notes:
- This class does not have to be instantiated; it provides class methods for the current program.
Methods:
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. |
os.Path.separator
Purpose:
Returns the character used to separate path segments.
Syntax:
CALL os.Path.separator() RETURNING separator STRING
Notes:
- separator contains the separator.
- On Unix, the separator is ‘/’
- On Windows, the separator is ‘\’
os.Path.pathseparator
Purpose:
Returns the character used in environment variables to separate path elements.
Syntax:
CALL os.Path.pathseparator() RETURNING separator STRING
Notes:
- separator contains the path separator.
- On Unix, the separator is ‘:’
- On Windows, the separator is ‘;’
Usage:
You typically use this method to build a path from two components.
os.Path.basename
Purpose:
This method returns the last element of a path.
Syntax:
CALL os.Path.basename(filename STRING) RETURNING basename STRING
Notes:
- filename is the name of the file.
- basename is the last element of the path.
Usage:
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.
os.Path.dirname
Purpose:
Returns all components of a path excluding the last one.
Syntax:
CALL os.Path.dirname(filename STRING) RETURNING dirname STRING
Notes:
- filename is the name of the file.
- dirname contains all the elements of the path excluding the last one.
Usage:
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.
os.Path.rootname
Purpose:
Returns the file path without the file extension of the last element of the file path.
Syntax:
CALL os.Path.rootname(filename STRING) RETURNING rootname STRING
Notes:
- filename is the file path.
- rootname contains the file path without the file extension of the last element.
Usage:
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.
os.Path.join
Purpose:
Joins two path segments adding the platform-dependent separator.
Syntax:
CALL os.Path.join(begin STRING, end STRING) RETURNING newpath STRING
Notes:
- begin is the beginning path segment.
- end is the ending path segment.
- newpath contains the joined path segments.
Usage:
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.
os.Path.pathtype
Purpose:
Checks if a path is a relative path or an absolute path.
Syntax:
CALL os.Path.pathtype(path STRING) RETURNING pathtype STRING
Notes:
- path is the path to check.
- pathtype can be “absolute” if the path is an absolute path, or “relative” if the path is a relative path.
os.Path.exists
Purpose:
Checks if a file exists.
Syntax:
CALL os.Path.exists(fname STRING) RETURNING result INTEGER
Notes:
- fname is the file name.
- result is TRUE if the file exists, FALSE otherwise.
os.Path.extension
Purpose:
Returns the file extension.
Syntax:
CALL os.Path.extension(fname STRING) RETURNING extension STRING
Notes:
- fname is the file name.
- extension is the string following the last dot found in fname.
- If fname does not have an extension, the function returns an empty string.
os.Path.readable
Purpose:
Checks if a file is readable.
Syntax:
CALL os.Path.readable(fname STRING) RETURNING result INTEGER
Notes:
- fname is the file name.
- result is TRUE if the file is readable, FALSE otherwise.
os.Path.writable
Purpose:
Checks if a file is writable.
Syntax:
CALL os.Path.writable(fname STRING) RETURNING result INTEGER
Notes:
- fname is the file name.
- result is TRUE if the file is writable, FALSE otherwise.
os.Path.executable
Purpose:
Checks if a file is executable.
Syntax:
CALL os.Path.executable(fname STRING) RETURNING result INTEGER
Notes:
- fname is the file name.
- result is TRUE if the file is executable, FALSE otherwise.
os.Path.isfile
Purpose:
Checks if a file is a regular file.
Syntax:
CALL os.Path.isfile(fname STRING) RETURNING result INTEGER
Notes:
- fname is the file name.
- result is TRUE if the file is a regular file, FALSE otherwise.
os.Path.isdirectory
Purpose:
Checks if a file is a directory.
Syntax:
CALL os.Path.isdirectory(fname STRING) RETURNING result INTEGER
Notes:
- fname is the file name.
- result is TRUE if the file is a directory, FALSE otherwise.
os.Path.ishidden
Purpose:
Checks if a file is hidden.
Syntax:
CALL os.Path.ishidden(fname STRING) RETURNING result INTEGER
Notes:
- fname is the file name.
- result is TRUE if the file is hidden, FALSE otherwise.
os.Path.islink
Purpose:
Checks if a file is UNIX symbolic link.
Syntax:
CALL os.Path.islink(fname STRING) RETURNING result INTEGER
Notes:
- fname is the file name.
- result is TRUE if the file is a symbolic link, FALSE otherwise.
Warning: This method can only be used on UNIX!
os.Path.isroot
Purpose:
Checks if a file path is a root path.
Syntax:
CALL os.Path.isroot(path STRING) RETURNING result STRING
Notes:
- path is the path to check.
- result is TRUE if the path is a root path, FALSE otherwise.
- On Unix the root path is ‘/’
- On Windows the root path matches “[a-zA-Z]:\”
os.Path.type
Purpose:
Returns the file type as a string
Syntax:
CALL os.Path.type(fname STRING) RETURNING ftype STRING
Notes:
- fname is the file name.
- ftype can be one of:
- file : the file is a regular file
- directory : the file is a directory
- socket : the file is a socket
- fifo : the file is a fifo
- block : the file is a block device
- char : the file is a character device
Warning: On UNIX, this method follows symbolic links. You must use the islink() method to identify symbolic links.
os.Path.size
Purpose:
Returns the size of a file.
Syntax:
CALL os.Path.size(fname STRING) RETURNING size INTEGER
Notes:
- fname is the file name.
- size is the file size
os.Path.atime
Purpose:
Returns the time of the last file access.
Syntax:
CALL os.Path.atime(fname STRING) RETURNING atime STRING
Notes:
- fname is the name of the file.
- atime is a string containing the last access time for the specified file in the standard format ‘YYYY-MM-DD HH:MM:SS’.
- If the function failed, it returns NULL.
os.Path.mtime
Purpose:
Returns the time of the last file modification.
Syntax:
CALL os.Path.mtime(fname STRING) RETURNING mtime STRING
Notes:
- fname is the name of the file.
- mtime is a string containing the last modification time for the specified file in the standard format ‘YYYY-MM-DD HH:MM:SS’.
- If the function failed, it returns NULL.
os.Path.rwx
Purpose:
Returns the UNIX file permissions of a file.
Syntax:
CALL os.Path.rwx(fname STRING) RETURNING mode INTEGER
Notes:
- fname is the name of the file.
- mode is the combination of permissions for user, group and other.
- Function returns -1 if it fails to get the permissions.
Warning: This method can only be used on UNIX!
Usage:
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.
os.Path.chrwx
Purpose:
Changes the UNIX permissions of a file.
Syntax:
CALL os.Path.chrwx(fname STRING, mode INTEGER) RETURNING res INTEGER
Notes:
- fname is the name of the file.
- mode is the UNIX permission combination in decimal (not octal!).
- Function returns TRUE on success, FALSE otherwise.
Warning: This method can only be used on UNIX!
Usage:
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.
os.Path.chown
Purpose:
Changes the UNIX owner and group of a file.
Syntax:
CALL os.Path.chown(fname STRING, uid INT, gui INT) RETURNING res INTEGER
Notes:
- fname is the name of the file.
- uid is the user id.
- gui is the group id.
- Function returns TRUE on success, FALSE otherwise.
Warning: This method can only be used on UNIX!
os.Path.uid
Purpose:
Returns the UNIX user id of a file.
Syntax:
CALL os.Path.uid(fname STRING) RETURNING id INTEGER
Notes:
- fname is the name of the file.
- id is the user id.
- Function returns -1 if it fails to get the user id.
Warning: This method can only be used on UNIX!
os.Path.gid
Purpose:
Returns the UNIX group id of a file.
Syntax:
CALL os.Path.gid(fname STRING) RETURNING id INTEGER
Notes:
- fname is the name of the file.
- id is the group id.
- Function returns -1 if it fails to get the user id.
Warning: This method can only be used on UNIX!
os.Path.homedir
Purpose:
Returns the path to the HOME directory of the current user.
Syntax:
CALL os.Path.homedir() RETURNING homedir STRING
Notes:
- homedir Path to the HOME directory of the user.
os.Path.rootdir
Purpose:
Returns the root directory of the current working path.
Syntax:
CALL os.Path.rootdir() RETURNING rootdir STRING
Notes:
- rootdir is the root directory of the current working path.
- On Unix, it always returns ‘/’
- On Windows it returns the current working drive as “[a-zA-Z]:\”
os.Path.dirfmask
Purpose:
Defines a filter mask for diropen.
Syntax:
CALL os.Path.dirfmask(mask INTEGER)
Notes:
- mask defines the filter mask (see below for possible values).
Usage:
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:
- 0x01 = Exclude hidden files (.*)
- 0x02 = Exclude directories
- 0x04 = Exclude symbolic links
- 0x08 = Exclude regular files
For example, to retrieve only regular files, you must call:
01 CALL os.Path.dirfmask( 1 + 2 + 4 )
os.Path.dirsort
Purpose:
Defines the sort criteria and sort order for diropen.
Syntax:
CALL os.Path.dirsort(criteria STRING, order INTEGER)
Notes:
- criterie is the sort criteria (see below for possible values).
- order defines ascending (1) or descending (-1) order.
Usage:
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:
- “undefined” = No sort. This is the default. Entries are read as returned by the OS functions.
- “name” = Sort by file name.
- “size” = Sort by file size.
- “type” = Sort by file type (directory, link, regular file).
- “atime” = Sort by access time.
- “ctime” = Sort by modification time.
- “extension” = Sort by file extension.
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 in ascending order.
os.Path.diropen
Purpose:
Opens a directory and returns an integer handle to this directory.
Syntax:
CALL os.Path.diropen(dname STRING) RETURNING dirhandle INTEGER
Notes:
- dname is the name of the directory.
- dirhandle is the directory handle. A dirhandle value of 0 indicates a failure when opening the directory.
Usage:
This function creates a list of directory
os.Path.dirclose
Purpose:
Closes the directory referenced by the directory handle dirhandle.
Syntax:
CALL os.Path.dirclose(dirhandle INTEGER)
Notes:
- dirhandle is the directory handle of the directory to close.
os.Path.dirnext
Purpose:
Reads the next entry in the directory.
Syntax:
CALL os.Path.dirnext(dirhandle INTEGER) RETURNING direntry STRING
Notes:
- dirhandle is the directory handle of the directory to read.
- direntry is the name of the entry read or NULL if all entries have been read.
os.Path.pwd
Purpose:
Returns the current working directory.
Syntax:
CALL os.Path.pwd() RETURNING cwd STRING
Notes:
- cwd is the current working directory.
os.Path.chdir
Purpose:
Changes the current working directory.
Syntax:
CALL os.Path.chdir(newdir STRING) RETURNING result INTEGER
Notes:
- newdir is the directory to select.
- result is TRUE if the current directory could be successfully selected.
os.Path.volumes
Purpose:
Returns the available volumes.
Syntax:
CALL os.Path.volumes() RETURNING volumes STRING
Notes:
- volumes contains the list of all available volumes separated by “|”.
os.Path.chvolume
Purpose:
Changes the current working volume.
Syntax:
CALL os.Path.chvolume(newvolume STRING) RETURNING result INTEGER
Notes:
- newvolume is the volume to select as the new current working volume.
- result is TRUE if the current working volume could be successfully changed.
- Sample : CALL os.Path.chvolume(“C:\\”) RETURNING result
os.Path.mkdir
Purpose:
Creates a new directory.
Syntax:
CALL os.Path.mkdir(dname STRING) RETURNING result INTEGER
Notes:
- dname is the name of the directory to create
- result is TRUE if the directory has been successfully created, FALSE otherwise.
os.Path.delete
Purpose:
Deletes a file or a directory.
Syntax:
CALL os.Path.delete(dname STRING) RETURNING result INTEGER
Notes:
- fname is the name of the file or directory to delete
- result is TRUE if the file or directory has been successfully deleted, FALSE otherwise.
- A directory can only be deleted if it is empty.
os.Path.rename
Purpose:
Renames a file or a directory.
Syntax:
CALL os.Path.rename(oldname STRING, newname STRING) RETURNING result INTEGER
Notes:
- oldname is the current name of the file or directory to be renamed.
- newname is the new name to assign to the file or directory.
- result is TRUE if the file or directory has been successfully renamed, FALSE otherwise.
os.Path.copy
Purpose:
Copies a regular file or.
Syntax:
CALL os.Path.copy(source STRING, dest STRING) RETURNING result INTEGER
Notes:
- source is the name of the file to copy.
- dest is the destination name of the copied file.
- result is TRUE if the file has been successfully copied, FALSE otherwise.
Examples:
Example 1: Extracting the parts of a file name
This program uses the file functions to extract the directory name, the base name, the root name, and the file extension:
IMPORT os
MAIN
DISPLAY “Dir name = “, os.Path.dirname(arg_val(1))
DISPLAY “Base name = “, os.Path.basename(arg_val(1))
DISPLAY “Root name = “, os.Path.rootname(arg_val(1))
DISPLAY “Extension = “, os.Path.extension(arg_val(1))
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 |
Warning: The above examples use Unix file names. On Windows the result would be different, as the file name separator is ‘\’.
Example 2: Browsing directories.
This program takes a directory path as an argument and scans the content recursively:
IMPORT os
MAIN
CALL showDir(arg_val(1))
END MAIN
FUNCTION showDir(path)
DEFINE path STRING
DEFINE child STRING
DEFINE h INTEGER
IF NOT os.Path.exists(path) THEN
RETURN
END IF
IF NOT os.Path.isdirectory(path) THEN
DISPLAY " ", os.Path.basename(path)
RETURN
END IF
DISPLAY "[", path, "]"
CALL os.Path.dirsort("name", 1)
LET h = os.Path.diropen(path)
WHILE h > 0
LET child = os.Path.dirnext(h)
IF child IS NULL THEN EXIT WHILE END IF
IF child == "." OR child == ".." THEN CONTINUE WHILE END IF
CALL showDir( os.Path.join( path, child ) )
END WHILE
CALL os.Path.dirclose(h)
END FUNCTION
转载请注明:赫非域 » Tiptop -Genero 技专没教的事