Functions to work with files and directories
This package contains functions to work with files and directories. As a general convention of this package, '/' is used as directory separator both for input and output arguments of all functions. For example:
exist("Modelica/Mechanics/Rotational.mo");
The functions provide the mapping to the directory separator of the underlying operating system. Note, that on Windows system the usage of '\' as directory separator would be inconvenient, because this character is also the escape character in Modelica and C Strings.
In the table below an example call to every function is given:
Function/type | Description |
---|---|
list(name) | List content of file or of directory. |
copy(oldName, newName) copy(oldName, newName, replace=false) |
Generate a copy of a file or of a directory. |
move(oldName, newName) move(oldName, newName, replace=false) |
Move a file or a directory to another place. |
remove(name) | Remove file or directory (ignore call, if it does not exist). |
removeFile(name) | Remove file (ignore call, if it does not exist) |
createDirectory(name) | Create directory (if directory already exists, ignore call). |
result = exist(name) | Inquire whether file or directory exists. |
assertNew(name,message) | Trigger an assert, if a file or directory exists. |
fullName = fullPathName(name) | Get full path name of file or directory name. |
(directory, name, extension) = splitPathName(name) | Split path name in directory, file name kernel, file name extension. |
fileName = temporaryFileName() | Return arbitrary name of a file that does not exist and is in a directory where access rights allow to write to this file (useful for temporary output of files). |
Extends from Modelica.Icons.Package (Icon for standard packages).
Name | Description |
---|---|
list | List content of file or directory |
copy | Generate a copy of a file or of a directory |
move | Move a file or a directory to another place |
remove | Remove file or directory (ignore call, if it does not exist) |
removeFile | Remove file (ignore call, if it does not exist) |
createDirectory | Create directory (if directory already exists, ignore call) |
exist | Inquire whether file or directory exists |
assertNew | Trigger an assert, if a file or directory exists |
fullPathName | Get full path name of file or directory name |
splitPathName | Split path name in directory, file name kernel, file name extension |
temporaryFileName | Return arbitrary name of a file that does not exist and is in a directory where access rights allow to write to this file (useful for temporary output of files) |
loadResource | Return the absolute path name of a URI or local file name |
List content of file or directory
Files.list(name);
If "name" is a regular file, the content of the file is printed.
If "name" is a directory, the directory and file names in the "name" directory are printed in sorted order.
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
name | If name is a directory, list directory content. If it is a file, list the file content |
Generate a copy of a file or of a directory
Files.copy(oldName, newName); Files.copy(oldName, newName, replace = true);
Function copy(..) copies a file or a directory to a new location. Via the optional argument replace it can be defined whether an already existing file may be replaced by the required copy.
If oldName/newName are directories, then the newName directory may exist. In such a case the content of oldName is copied into directory newName. If replace = false it is required that the existing files in newName are different from the existing files in oldName.
copy("C:/test1/directory1", "C:/test2/directory2"); -> the content of directory1 is copied into directory2 if "C:/test2/directory2" does not exist, it is newly created. If "replace=true", files in directory2 may be overwritten by their copy copy("test1.txt", "test2.txt") -> make a copy of file "test1.txt" with the name "test2.txt" in the current directory
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
oldName | Name of file or directory to be copied |
newName | Name of copy of the file or of the directory |
replace | = true, if an existing file may be replaced by the required copy |
Move a file or a directory to another place
Files.move(oldName, newName); Files.move(oldName, newName, replace = true);
Function move(..) moves a file or a directory to a new location. Via the optional argument replace it can be defined whether an already existing file may be replaced.
If oldName/newName are directories, then the newName directory may exist. In such a case the content of oldName is moved into directory newName. If replace = false it is required that the existing files in newName are different from the existing files in oldName.
move("C:/test1/directory1", "C:/test2/directory2"); -> the content of directory1 is moved into directory2. Afterwards directory1 is deleted. if "C:/test2/directory2" does not exist, it is newly created. If "replace=true", files in directory2 may be overwritten move("test1.txt", "test2.txt") -> rename file "test1.txt" into "test2.txt" within the current directory
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
oldName | Name of file or directory to be moved |
newName | New name of the moved file or directory |
replace | = true, if an existing file or directory may be replaced |
Remove file or directory (ignore call, if it does not exist)
Files.remove(name);
Removes the file or directory "name". If "name" does not exist, the function call is ignored. If "name" is a directory, first the content of the directory is removed and afterwards the directory itself.
This function is silent, i.e., it does not print a message.
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
name | Name of file or directory to be removed |
Remove file (ignore call, if it does not exist)
Files.removeFile(fileName);
Removes the file "fileName". If "fileName" does not exist, the function call is ignored. If "fileName" exists but is no regular file (e.g., directory, pipe, device, etc.) an error is triggered.
This function is silent, i.e., it does not print a message.
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
fileName | Name of file that should be removed |
Create directory (if directory already exists, ignore call)
Files.createDirectory(directoryName);
Creates directory "directoryName". If this directory already exists, the function call is ignored. If several directories in "directoryName" do not exist, all of them are created. For example, assume that directory "E:/test1" exists and that directory "E:/test1/test2/test3" shall be created. In this case the directories "test2" in "test1" and "test3" in "test2" are created.
This function is silent, i.e., it does not print a message. In case of error (e.g., "directoryName" is an existing regular file), an assert is triggered.
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
directoryName | Name of directory to be created (if present, ignore call) |
Inquire whether file or directory exists
result = Files.exist(name);
Returns true, if "name" is an existing file or directory. If this is not the case, the function returns false.
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
name | Name of file or directory |
Name | Description |
---|---|
result | = true, if file or directory exists |
Trigger an assert, if a file or directory exists
Files.assertNew(name); Files.assertNew(name, message="This is not allowed");
Triggers an assert, if "name" is an existing file or directory. The error message has the following structure:
File "<name>" already exists. <message>
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
name | Name of file or directory |
message | Message that should be printed after the default message in a new line |
Get full path name of file or directory name
fullName = Files.fullPathName(name);
Returns the full path name of a file or directory "name".
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
name | Absolute or relative file or directory name |
Name | Description |
---|---|
fullName | Full path of 'name' |
Split path name in directory, file name kernel, file name extension
(directory, name, extension) = Files.splitPathName(pathName);
Function splitPathName(..) splits a path name into its parts.
(directory, name, extension) = Files.splitPathName("C:/user/test/input.txt") -> directory = "C:/user/test/" name = "input" extension = ".txt"
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
pathName | Absolute or relative file or directory name |
Name | Description |
---|---|
directory | Name of the directory including a trailing '/' |
name | Name of the file without the extension |
extension | Extension of the file name. Starts with '.' |
Return arbitrary name of a file that does not exist and is in a directory where access rights allow to write to this file (useful for temporary output of files)
fileName = Files.temporaryFileName();
Return arbitrary name of a file that does not exist and is in a directory where access rights allow to write to this file (useful for temporary output of files).
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
fileName | Full path name of temporary file |
Return the absolute path name of a URI or local file name
fileReference = FileSystem.loadResource(uri);
The function call "FileSystem.loadResource(uri)
" returns the
absolute path name of the file that is either defined by an URI or by a local
path name. With the returned file name it is possible to
access the file with function calls of the C standard library.
If the data or file is stored in a data-base,
this might require copying the resource to a temporary folder and referencing that.
The implementation of this function is tool specific. However, at least Modelica URIs (see "chapter 13.2.3 External Resources" of the Modelica Specification), as well as absolute local file path names are supported.
file1 = loadResource("modelica://Modelica/Resources/Data/Utilities/Examples_readRealParameters.txt") // file1 is the absolute path name of the file file2 = loadResource("C:\\data\\readParameters.txt") file2 = "C:/data/readParameters.txt"
Extends from Modelica.Utilities.Internal.PartialModelicaServices.ExternalReferences.PartialLoadResource (Interface for tool specific function to return the absolute path name of a URI or local file name), ModelicaServices.ExternalReferences.loadResource (Return the absolute path name of a URI or local file name (in this default implementation URIs are not supported, but only local file names)).
Name | Description |
---|---|
uri | URI or local file name |
Name | Description |
---|---|
fileReference | Absolute path name of file |