Package Modelica.​Utilities.​Files
Functions to work with files and directories

Information

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/typeDescription
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).
fileReference = loadResource(uri) Return the absolute path name of a URI or local file name.

Extends from Modelica.​Icons.​FunctionsPackage (Icon for packages containing functions).

Package Contents

NameDescription
assertNewTrigger an assert, if a file or directory exists
copyGenerate a copy of a file or of a directory
createDirectoryCreate directory (if directory already exists, ignore call)
existInquire whether file or directory exists
fullPathNameGet full path name of file or directory name
listList content of file or directory
loadResourceReturn the absolute path name of a URI or local file name
moveMove a file or a directory to another place
removeRemove file or directory (ignore call, if it does not exist)
removeFileRemove file (ignore call, if it does not exist)
splitPathNameSplit path name in directory, file name kernel, file name extension
temporaryFileNameReturn 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)

Function Modelica.​Utilities.​Files.​list
List content of file or directory

Information

Syntax

Files.list(name);

Description

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).

Inputs

TypeNameDescription
StringnameIf name is a directory, list directory content. If it is a file, list the file content

Function Modelica.​Utilities.​Files.​copy
Generate a copy of a file or of a directory

Information

Syntax

Files.copy(oldName, newName);
Files.copy(oldName, newName, replace = true);

Description

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.

Example

  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).

Inputs

TypeNameDescription
StringoldNameName of file or directory to be copied
StringnewNameName of copy of the file or of the directory
Booleanreplace= true, if an existing file may be replaced by the required copy

Function Modelica.​Utilities.​Files.​move
Move a file or a directory to another place

Information

Syntax

Files.move(oldName, newName);
Files.move(oldName, newName, replace = true);

Description

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.

Example

  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).

Inputs

TypeNameDescription
StringoldNameName of file or directory to be moved
StringnewNameNew name of the moved file or directory
Booleanreplace= true, if an existing file or directory may be replaced

Function Modelica.​Utilities.​Files.​remove
Remove file or directory (ignore call, if it does not exist)

Information

Syntax

Files.remove(name);

Description

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).

Inputs

TypeNameDescription
StringnameName of file or directory to be removed

Function Modelica.​Utilities.​Files.​removeFile
Remove file (ignore call, if it does not exist)

Information

Syntax

Files.removeFile(fileName);

Description

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).

Inputs

TypeNameDescription
StringfileNameName of file that should be removed

Function Modelica.​Utilities.​Files.​createDirectory
Create directory (if directory already exists, ignore call)

Information

Syntax

Files.createDirectory(directoryName);

Description

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).

Inputs

TypeNameDescription
StringdirectoryNameName of directory to be created (if present, ignore call)

Function Modelica.​Utilities.​Files.​exist
Inquire whether file or directory exists

Information

Syntax

result = Files.exist(name);

Description

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).

Inputs

TypeNameDescription
StringnameName of file or directory

Outputs

TypeNameDescription
Booleanresult= true, if file or directory exists

Function Modelica.​Utilities.​Files.​assertNew
Trigger an assert, if a file or directory exists

Information

Syntax

Files.assertNew(name);
Files.assertNew(name, message="This is not allowed");

Description

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).

Inputs

TypeNameDescription
StringnameName of file or directory
StringmessageMessage that should be printed after the default message in a new line

Function Modelica.​Utilities.​Files.​fullPathName
Get full path name of file or directory name

Information

Syntax

fullName = Files.fullPathName(name);

Description

Returns the full path name of a file or directory "name".

Extends from Modelica.​Icons.​Function (Icon for functions).

Inputs

TypeNameDescription
StringnameAbsolute or relative file or directory name

Outputs

TypeNameDescription
StringfullNameFull path of 'name'

Function Modelica.​Utilities.​Files.​splitPathName
Split path name in directory, file name kernel, file name extension

Information

Syntax

(directory, name, extension) = Files.splitPathName(pathName);

Description

Function splitPathName(..) splits a path name into its parts.

Example

  (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).

Inputs

TypeNameDescription
StringpathNameAbsolute or relative file or directory name

Outputs

TypeNameDescription
StringdirectoryName of the directory including a trailing '/'
StringnameName of the file without the extension
StringextensionExtension of the file name. Starts with '.'

Function Modelica.​Utilities.​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)

Information

Syntax

fileName = Files.temporaryFileName();

Description

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).

The created temporary file is not automatically deleted when closed, but needs to be explicitly deleted, e.g. by removeFile(fileName).

Warning: The underlying C implementation of ModelicaInternal_temporaryFileName calls the standard C function tmpnam, which has a race condition security problem in the case another process creates a file with the same fileName just after tmpnam generated the full path name.

Example

  fileName = Files.temporaryFileName();
     -> fileName is the absolute path name of the temporary file
  Streams.print(String(System.getPid()), fileName);
     -> Create the temporary file
        Warning: Possible race condition on file access
  Files.removeFile(fileName);
     -> Explicitly delete the temporary file (after use)

Extends from Modelica.​Icons.​Function (Icon for functions).

Outputs

TypeNameDescription
StringfileNameFull path name of temporary file

Function Modelica.​Utilities.​Files.​loadResource
Return the absolute path name of a URI or local file name

Information

Syntax

fileReference = Files.loadResource(uri);

Description

The function call "Files.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.

Example

  file1 = loadResource("../../../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) and 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)).

Inputs

TypeNameDescription
StringuriURI or local file name

Outputs

TypeNameDescription
StringfileReferenceAbsolute path name of file

Generated 2018-12-12 12:14:36 EST by MapleSim.