Files
Functions for working with files and directories are described here.

The finfo is used for getting information about a file and has the following fields:
  • str Name - base name of the file
  • int Size - length in bytes for regular files
  • int Mode - file's mode and permission bits
  • time Time - last modification time
  • bool IsDir - true if it is a directory
  • str Dir - directory where the file is located. This field is only filled when calling the function ReadDir(str, int, str)

The file type is used in functions that work with the open file descriptor.

The AppendFile function appends data of buf variable or a string to a file named by filename. If the file does not exist, AppendFile creates it with 0644 permissions.

The ChDir function changes the current directory.

The ChMode function changes the attributes of the file.

The CloseFile function closes the file descriptor that was opened with the OpenFile function.

The CopyFile function copies src file to dest file. If dest file exists it is overwritten. The file attributes are preserved when copying. The function returns the number of copied bytes.

The CreateDir function creates a directory named dirname, along with any necessary parents. If dirname is already a directory, CreateDir does nothing.

The CreateFile function creates a file with the specified name. If the trunc parameter is true and the file already exists, its size becomes 0.

The ExistFile function returns true if the specified file or directory exists. Otherwise, it returns false.

The FileInfo function receives information about the specified file and returns the finfo structure. The file must be opened using the OpenFile function.

The FileInfo function gets information about the named file and returns finfo structure.

The FileMode function returns the file attributes.

The GetCurDir function returns the current directory.

The IsEmptyDir function returns true if the specified directory is empty. Otherwise, it returns false.

The Md5File function returns the MD5 hash of the specified file as a hex string.

The obj function converts a variable of finfo type into an object. The resulting object has fields: name, size, mode, time, isdir, dir.

The OpenFile function opens the specified file and returns a variable of file type with an open file descriptor. After working with the file, the open file descriptor must be closed using the CloseFile function. The flags parameter may be zero or a combination of the following flags:
  • CREATE - if the file does not exist, it will be created.
  • TRUNC - file will be truncated to zero length after opening.
  • READONLY - the file will be open for reading only.
file f = OpenFile(fname, CREATE)
Write(f, buf("some test string"))
SetPos(f, -15, 1)
buf b &= Read(f, 5)
CloseFile(f)

The Read function reads the size number of bytes from the current position in the file that was opened using the OpenFile function. The function returns a variable of the buf type, which contains the read data.

The ReadDir function reads the directory named by dirname and returns a list of directories and files entries.

The ReadDir function reads the dirname directory with the specified name and returns the list of its subdirectories and files according to the specified parameters. The flags parameter can be a combination of the following flags:
  • RECURSIVE - In this case there will be a recursive search for all subdirectories.
  • ONLYFILES - The returned array will contain only files.
  • ONLYDIRS - The returned array will contain only directories.
  • REGEXP - The pattern parameter contains a regular expression for matching file names.
If you specify the ONLYFILES and ONLYDIRS flags at the same time, the files and directories will be searched.
The pattern parameter can contain a wildcard for files or a regular expression. In this case, the files and directories that match the specified pattern will be returned. The wildcard can contain the following characters:
  • '*' - matches any sequence of non-separator characters
  • '?' - matches any single non-separator character
for item in ReadDir(ftemp, RECURSIVE, `*fold*`) {
ret += item.Name
}
for item in ReadDir(ftemp, RECURSIVE | ONLYFILES | REGEXP, `.*\.pdf`) {
ret += item.Name
}

The ReadDir function reads the dirname directory with the specified name and returns the list of its subdirectories and files according to the specified parameters. The parameter flags is described above. The patterns parameter is an array of strings and may contain file masks or regular expressions. The ignore parameter also contains file wildcards or regular expressions, but such files or directories will be skipped. If you want to specify a regular expression in these arrays, enclose it between '/' characters.
arr.str aignore = {`/txt/`, `*.pak`}
arr.str amatch = {`/\d+/`, `*.p?`, `/di/`}.
for item in ReadDir(ftemp, RECURSIVE, amatch, aignore) {
ret += item.Name
}

The ReadFile function reads the specified file and returns the contents as a string.

The ReadFile function reads the file named by filename to the buf variable out and returns it.

The ReadFile function reads data from the filename file starting at offset offset and length length. If offset is less than zero, then the offset is counted from the end to the beginning of the file.

The Remove function removes a file or an empty directory.

The RemoveDir function removes dirname directory and any children it contains.

The Rename function renames (moves) oldpath to newpath. If newpath already exists and is not a directory, Rename replaces it.

The SetFileTime function changes the modification time of the named file.

The SetPos function sets the current position in the file for read or write operations. The file must be opened with the OpenFile function. The function returns the offset of the new position. The whence parameter can take the following values:
  • 0 - the off offset is specified from the beginning of file.
  • 1 - the off offset is specified from the current position.
  • 2 - the off offset is specified from the end of a file.

The Sha256File function returns the SHA256 hash of the specified file as a hex string.

The TempDir function returns the default temporary directory.

The TempDir function creates a new temporary directory in the directory path with a name beginning with prefix and returns the path of the new directory. If path is the empty string, TempDir uses the default temporary directory.

The Write function writes data from a variable of the buf type to a file that was opened using the OpenFile function. The function returns the f parameter.

The WriteFile function writes data of buf variable or a string to a file named by filename. If the file does not exist, WriteFile creates it with 0777 permissions, otherwise the file is truncated before writing.
Copy link
On this page
Types
finfo
file
Functions
AppendFile(str filename, buf|str data)
ChDir(str dirname)
ChMode(str name, int mode)
CloseFile(file f)
CopyFile(str src, str dest) int
CreateDir(str dirname)
CreateFile(str name, bool trunc)
ExistFile(str name) bool
FileInfo(file f) finfo
FileInfo(str name) finfo
FileMode(str name) int
GetCurDir() str
IsEmptyDir(str path) bool
Md5File(str filename) str
obj(finfo fi) obj
OpenFile(str filename, int flags) file
Read(file f, int size) buf
ReadDir(str dirname) arr.finfo
ReadDir(str dirname, int flags, str pattern) arr.finfo
ReadDir(str dirname, int flags, arr.str patterns, arr.str ignore) arr.finfo
ReadFile(str filename) str
ReadFile(str filename, buf out) buf
ReadFile(str filename, int offset, int length) buf
Remove(str name)
RemoveDir(str dirname)
Rename(str oldpath, str newpath)
SetFileTime(str name, time modtime)
SetPos(file f, int off, int whence) int
Sha256File(str filename) str
TempDir() str
TempDir(str path, str prefix) str
Write(file f, buf b) file
WriteFile(str filename, buf|str data)