Low Level work with Files and Folders in KOL
Since KOL was developed to create projects primarily for the Windows environment, and the most efficient way to work with files in this environment is to work directly with the corresponding Windows API functions, a number of functions have been created for KOL to work with files at this level. I do not recommend using Pascal functions (Append, Rewrite, Reset, ...), even though they claim to be some kind of platform independence, due to their certain limitations. They also add a few kilobytes of completely useless code to the application. At the same time, working with Windows API functions directly is somewhat inconvenient due to too many parameters that must be specified for almost everything. In my opinion, it is more convenient to work with KOL functions:
FileCreate(s, flags) - creates or opens a file for reading or writing, depending on the flags (for example, ofOpenRead or ofOpenExisting or ofShareDenyWrite - opens an existing file for reading, disallowing writing to this file until the created descriptor is closed. must be concatenated with the or operation):
accessor group - only one flag needs to be selected |
|
group of the way to create or open Existing file - only one flag should be selected |
ofOpenExisting, ofOpenAlways, ofCreateNew, ofTruncateExisting |
(optional) sharing group |
|
attribute group |
ofAttrReadOnly, ofAttrHidden, ofattrSystem, ofAttrTemp, ofAttrArchive, ofAttrOffline |
The result of calling the FileCreate function is a descriptor of type hFile (just an unsigned 32-bit number) that is used in other file functions to identify an open file object. Using the same descriptor, it is also possible to call API functions for working with files.
FileClose(f) - closes the file;
FileExists(s) - checks for the existence of a file at the specified path;
FileRead(f, buffer, n) - reads bytes from a file into memory;
FileWrite(f, buffer, n) - writes bytes from memory to a file;
FileEOF(f) - checks if the end of the file has been reached (while reading);
FileSeek(f, moveto, movemethod) - moves the read / write pointer in the file;
File2Str(f) - reads the rest of the file as a string.
In addition, there are a number of functions for opening, reading or writing, and closing a file - in one call (and other functions for working with a file by name, without creating a descriptor in the program):
StrSaveToFile(fname, s) - creates or overwrites a file from string s in RAM;
StrLoadFromFile(fname) - reads the entire file into a line;
Mem2File(fname, mem, n) - writes a piece of memory to a file;
File2Mem(fname, mem, n) - reads the entire file into a buffer in memory;
FileTimeCompare(fname1, fname2) - compares the time of the last modification of two files and returns, depending on the results, -1, 0 or 1;
FileSize(fname) - returns the file size (64-bit integer);
ChangeFileExt(fname, ext) - changes the extension of the specified file.
Also, there are a number of functions for working with directories (folders), the names of directories and temporary files, and with disks:
GetStartDir - returns the path to the directory in which the application was started (I recommend using this function, not GetWorkDir),
DirectoryExists(s) - checks the existence of a directory;
DirectoryEmpty(s) - checks for the presence of files (and subdirectories) in the specified directory (true is returned if the directory is empty, according to the function name);
DirectoryHasSubdirs(s) - checks for the presence of nested subdirectories in the specified directory;
CheckDirectoryContent(s, subdirsonly, mask) - checks for the presence of files and subdirectories specified by the mask;
CreateDir(s) - creates a directory;
ForceDirectories(s) - creates a directory, ensuring, if necessary, the creation of all overlying directories specified in the path s;
IncludeTrailingPathDelimiter(s)- returns the path s, adds a path separator (character '\') if necessary. In fact, most KOL functions that return a directory path provide a trailing slash ('\'), but sometimes the directory name can be obtained in another way (for example, as a result of manually entering the path by the user in the edit window;
ExcludeTrailingPathDelimiter(s) - in contrast to the previous function, discards the trailing backslash;
FilePathShortened(s, n) - formats the path to the file, shortening it to a maximum of n characters (intermediate directories are discarded from the middle and replaced with ellipsis '...');
FilePathShortenPixels(s, DC, n) - similar to the previous function, but the length of the textual representation of the path "fits" into the size of n pixels on the DC canvas;
ExtractFilePath(s) - extracts only the path to the file directory;
ExtractFileName(s) - extracts the name of the file with the extension;
ExtractFileNameWOExt(s) - extracts file name without extension;
ExtractFileExt(s) - extracts only the extension (the first character in the result string will be '.', except in the case of an empty extension);
GetSystemDir - returns the path to the Windows system directory (Windows \ System32 or another, depending on the Windows version);
GetWindowsDir - returns the path to the directory of Windows itself;
GetWorkDir - returns the path to the "working" directory;
GetTempDir - returns the path to the directory intended for storing temporary files (it is best to create your temporary files in this directory);
CreateTempFile(s, s1) - returns a string that can be used as the name of the temporary file (note that it does not create the file itself);
GetFileListStr(s) - returns a string containing a list (through the symbol with code # 13) of all files in the specified directory;
DeleteFile2Recycle(s) - deletes the specified file (or the list of files listed with delimiter # 13) to the trash (as opposed to the DeleteFile (s) API function);
DeleteFiles(s) - deletes files by mask (as usual, characters '*' and '?' are allowed when specifying a mask template;
CopyMoveFiles(s1, s2, move) - copies or moves the specified (in a list, through the # 13 symbol, template symbols are also allowed) files to the specified directory;
DiskFreeSpace(s) - returns the number of free bytes on the disk (result of type I64);
DirectorySize(s) - returns the size of the directory (along with all subdirectories, the result is also of type I64).
In addition to the low-level file access functions, KOL also contains tools for working with streams, but more on that later (since streams are already objects, and now I'm not touching objects for now).