Files

Functions for working with files and directories are described here.

Types

finfo

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)

Functions

AppendFile(str filename, buf|str data)

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.

ChDir(str dirname)

The ChDir function changes the current directory.

ChMode(str name, int mode)

The ChMode function changes the attributes of the file.

CopyFile(str src, str dest) int

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.

CreateDir(str dirname)

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

CreateFile(str name, bool trunc)

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.

ExistFile(str name) bool

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

FileInfo(str name) finfo

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

FileMode(str name) int

The FileMode function returns the file attributes.

GetCurDir() str

The GetCurDir function returns the current directory.

Md5File(str filename) str

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

ReadDir(str dirname) arr.finfo

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

ReadDir(str dirname, int flags, str pattern) arr.finfo

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.

  • REGEXP - The pattern parameter contains a regular expression for matching file names.

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
}

ReadFile(str filename) str

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

ReadFile(str filename, buf out) buf

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

ReadFile(str filename, int offset, int length) buf

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.

Remove(str name)

The Remove function removes a file or an empty directory.

RemoveDir(str dirname)

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

Rename(str oldpath, str newpath)

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

SetFileTime(str name, time modtime)

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

Sha256File(str filename) str

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

TempDir() str

The TempDir function returns the default temporary directory.

TempDir(str path, str prefix) str

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.

WriteFile(str filename, buf|str data)

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.