SPL: Operation on the directory of a file
SPL provides abundant functions for the file system to operate the directories and files conveniently.
1. Directory
SPL uses directory function to list the directories of files or the files themselves and return them as a sequence. For example, the file structures of the first layer under the directory d:/test on local machine is as follows:
And the entire subdirectory structure under the text directory is as follows:
1.1. List file names
The directory function provides many options for listing the results under different conditions. By default, that is, no option, it lists only the files under the specified directory(without subdirectories). The following expression:
=directory("d:/test")
returns only the files under the current directory as follows:
1.2. List subdirectories
The directory function lists only the first-layer subdirectories under the specified directory if option @d is applied. For example, the expression:
=directory@d("d:/test")
returns only the first layer subdirectories as follows:
1.3. List all the files or subdirectories
The directory function lists only first-layer files or subdirectories in Section 1.1 and 1.2. Option @s should be used in order to list all nested files and subdirectories. The following expression :
=directory@s("d:/test")
returns files under all subdirectories. Here only first 10 items are listed below because there are too many files:
The above expression lists all the files under the specified directory. Wildcard characters like * and ? could be used in the expression to list files of certain types. For example, to list the image files with PNG extension, the following expression:
=directory@s("d:/test/*.png")
works. It returns the file names with PNG format only, and here are some of the results:
Similar to list all subdirectories, option @d could be used to list the directories and option @s to search all subdirectories recursively, and the following expression:
=directory@ds("d:/test")
returns all the subdirectories, shown in the picture below:
1.4. List absolute paths
The directory function returns the name of the files only with option @s when listing all the files or subdirectories in Section 1.3. These files cannot be operated directly just with the file names but the absolute paths of the files.
The option @p could be used to concatenate the file name with an absolute path, and the following expression:
=directory@sp("d:/test")
will return file names with absolute paths, and some of the results are in the picture below:
1.5. Create a directory
Option @m is used to create directories as follow:
=directory@m("d:/test/sub1/sub2")
Directories will be created on multiple levels continuously. For example, the sub1 in the above example may be absent, the function will create all the nonexistent parent directories.
1.6. Delete a directory
The specified directory can be deleted with option @r:
=directory@r("d:/test/sub1/sub2")
The above expression will delete the sub2 directory.
Please note that the directory to be deleted must be empty. Otherwise, the above expression will return false instead of deleting the specified directory.
To delete a non-empty directory by force, another function can be employed:
=movefile@y("d:/test/sub1/sub2")
The option @y of this function is to remove the specified non-empty directory by force.
2. File
The filename function in SPL can be used to split the parts of the file name, and the movefile function to move, copy, or delete files.
Here are some examples of operations related to the file name.
2.1. Split the file name
The filename function splits the file name from a full file name with an absolute path:
=filename("d:/test/MyApp/icon.png")
Execute the code to get the file name as shown in the picture:
There are many other options for split, such as:
Option @e is used to return the extension only.
Option @n is used to return the file name without the extension.
Option @d is used to return the path d:/test/MyApp only.
2.2 Move and copy files
The movefile function moves the file to the destination path. For example, move the d:/test/MyApp/icon.png file to d:/node as follows:
=movefile("d:/test/MyApp/icon.png","d:/node")
Option @c could be used to copy the file to the destination path, for example:
=movefile@c("d:/test/MyApp/icon.png","d:/node")
2.3. Delete files
The movefile function also deletes files without destination path filled in at this time:
=movefile("d:/test/MyApp/icon.png")
The above expression deletes the specified file.
Please note that these operations will fail if a file with the same name exists in the destination path in Section 2.2, or if the file to be deleted in this section is read-only. They can be executed by force with option @y.
2.4. Rename files
The movefile function renames files with the destination being the new file name to be named rather than a path. For example:
=movefile("d:/test/MyApp/icon.png","newname.png")
The code renames the d:/test/MyApp/icon.png file as newname.png.
3. File object
The file function is used to open the specified file and return the file object.
3.1. Open files
The file function opens the specified file as follows:
=file("d:/test/MyApp/config/ideconfig.json")
It returns the file object of the json file.
Please note: the default charset of the operating system is used to read the files when they are opened. A designated charset should be separated with a colon. For example, the charset of UTF-8 is used to open the above file:
=file("d:/test/MyApp/config/ideconfig.json":"UTF-8")
3.2. Create temporary files
The @t option of file function is used to create a temporary file with a random name in a specified directory. For example:
=file@t("d:/tmp")
Execute to get a temporary file with a random name as follows:
The directory where the temporary file is located can be nonexistent in advance; the directories with corresponding structures are created automatically when the temporary files are created.
Please note that the temporary files should be maintained by yourself. You should take the initiative to delete these files once the operations are finished. The movefile function in Section 2.3 deletes files with the file object directly passed to movefile. The expression is as follows:
=movefile(f)
in which f is the opened temporary file object.
However, the file will be deleted automatically when all data has been fetched out from a cursor if the cursor is created on the temporary file, and @x option is used during the creation, that is, the syntax of f.cursor@x().
3.3. Check the existence of a file
The exists function of the file object, which is obtained in Section 3.1 or 3.2, returns the existence of the file. The expression is as follows:
=f.exists()
in which f is the file object and the function returns a boolean value to indicate whether the file exists.
3.4. Return the file size
The size function of the file object returns the size of the current file:
=f.size()
where f is the file object and the function returns an integer value to indicate the number of bytes the file occupies.
3.5. Return the modified data of a file
The date function of the file object returns the last modified date of the current file:
=f.date()
in which f is the file object and the function returns a date value to indicate the last modified date of the file.
4. Application
4.1. Main path
The SPL's environment can set the main path where subdirectories or files are placed, and reference to the file in SPL simply designate a relative path relative to the main path. For example, as the main path of the current environment is set as d:/test/MyApp, you can write only the file name in file function to open d:/test/MyApp/icon.png as follows:
=file("icon.png")
Each host can set the main path under different drivers or directories according to its own environment. SPL script can be easily migrated to different hosts because of its reference to the relative path.
Select option menu in IDE to set the main path of the application (the attribute in the red box):
Execute the following code in Section 1.4 once again after the main path is set:
=directory@sp("d:/test")
The results are changed, and some of them as shown:
As you can see, the files after the fourth member are in the main path. Consequently, the function still returns the relative path relative to the main path, even though the @p option is used to return the absolute path.
The advantage of this method is that you can just use the @p option to get the absolute path and open the "absolute path" file to operate in the script, regardless of whether to operate differently for correct execution in different environments or main paths.
The filename function splits the file name. However, the @p option can be specified to get the absolute path concatenated with the main path, if the file name is just a relative path. The expression is as follows:
=filename@p("icon.png")
It returns the absolute path concatenated with the main path, and the result is shown as:
When the path is not specified, for example, execute:
=filename@p()
It directly returns the main path of the current environment, and the result of this example is d:/test/MyApp.
4.2. Searching path
A project tends to have more files when being large. These project files, especially the developed SPL script files, often require classification management. The main path will be bloated with all the files in it.
Therefore, the SPL's environment particularly set the searching path, which can be separated by semicolons, allowing multiple values to be set in order to better classify the script files.
The setting of the searching path is similar to that of the main path as the attribute in the red box:
The relative path is used to refer to the files in the main path or searching path with slight differences:
1. Only one main path can be set, while searching paths can be multiple.
2. The default option is only relative to the main path when the file function opens the file of relative path. The default option will be relative to searching path with specified option @s, that is, the file@s syntax.
3. The directory@p function will still return the relative path relative to main path rather than absolute path, if the file is in main path
4.3. Temporary path
Temporary files are needed in application development. SPL application environment also set the temporary path to centralize those temporary files. The temporary paths, like searching path settings, are separated by semicolons and multiple paths can be set. Set the temporary path as the attribute in the red box:
You don't need to specify which path to create the temporary file in file@t function like Section 3.2 once the temporary path is set. Instead, the path parameter should be left empty, and then the system creates the temporary file in the first temporary path. The expression is as follows:
=file@t()
Execute to create the temporary file which is in the temporary path specified by the application environment, and the result is as follows:
The temporary file at this time is seen in the temporary path circled by the red box.
Please note that the temporary path of the application environment doesn't have to be set. The above function, which creates temporary files without path, will cause an error if the temporary path is not set.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL