Programmer Guide/Shell Items/File/NEW FILE: Difference between revisions

From STX Wiki
Jump to navigationJump to search
No edit summary
m (adding file template)
 
(17 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE:{{SUBPAGENAME}}}}
{{DISPLAYTITLE:{{SUBPAGENAME}}}}
{{File Item}}
The command <code>NEW FILE</code> creates a new file item. File items are used to read and write files to disk. There are special <code>[[Programmer_Guide/Shell_Items/File/SET_FILE|SET]]</code> commands to deal with different types of files (e.g. XML files or files using the ''section'' format). The availability of these commands is dependant on the <code>NEW FILE</code> command variant which was used to create the item (see below).
The command <code>NEW FILE</code> creates a new file item. File items are used to read and write files to disk. There are special <code>[[Programmer_Guide/Shell_Items/File/SET_FILE|SET]]</code> commands to deal with different types of files (e.g. XML files or files using the ''section'' format). The availability of these commands is dependant on the <code>NEW FILE</code> command variant which was used to create the item (see below).


Line 8: Line 9:
You should surround file names with inverted commas (apostrophes, e.g. <code>NEW FILE * '$#fileName'</code>) if you cannot be absolutely sure that the file name is free of spaces. Otherwise, the command will fail, although the <code>filename</code> itself is valid.
You should surround file names with inverted commas (apostrophes, e.g. <code>NEW FILE * '$#fileName'</code>) if you cannot be absolutely sure that the file name is free of spaces. Otherwise, the command will fail, although the <code>filename</code> itself is valid.


'''General Options'''
== General Options ==


The following options are available for all <code>NEW FILE</code> commands.
The following options are available for all <code>NEW FILE</code> commands.
{|
{|
|-
|-
| <code>/G</code>
| <code>/G</code>
| turns automatic garbage collection on. If specified, the item is automatically deleted when exiting the macro.
| turns automatic garbage collection on. If specified, the item is automatically deleted when exiting the respective execution context (macro).
|-
|-
| <code>/I</code>
| <code>/I</code>
Line 21: Line 21:
|}
|}


==File System Items==
== <span id="File_System_Items">File System Items (Dummy Files)</span> ==


The following file items cannot access files on disk, but are useful for accessing system information about files and directories.
The following command will create a dummy file item, also called file system item. Such dummy files do not represent actual disk files and, hence, cannot be used for reading and writing to disk. Instead, they allow access to system information ''about'' files and directories.


<code>NEW FILE <var>name</var> /File [ /G ]</code>
NEW FILE <var>name</var> /File [ /G ]


Create a file item that can be used for file-system functions (<code>STATUS</code>, <code>RENAME</code> (=move), <code>COPY</code>, <code>DELETE</code>), but cannot access the content of a file.
Create a file item that can be used for file-system functions ([[Programmer_Guide/Shell_Items/File/SET_FILE#STATUS|<code>STATUS</code>]], [[Programmer_Guide/Shell_Items/File/SET_FILE#RENAME_and_COPY|<code>RENAME</code>]] (=move), [[Programmer_Guide/Shell_Items/File/SET_FILE#COPY|<code>COPY</code>]], [[Programmer_Guide/Shell_Items/File/SET_FILE#DELETE|<code>DELETE</code>)]], but cannot access the content of a file.
{|
| <var>name</var>
| The parameter <var>name</var> is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable <code>#new</code>.
|}


;<var>name</var>
== <span id="List_File_Items">List File Items (Searching for Files and Directories)</span> ==


:The parameter <var>name</var> is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable <code>#new</code>.
NEW FILE <var>name</var> /List [ /G ]
 
<code>NEW FILE <var>name</var> /List [ /G ]</code>


Create a file item that can be used to search for files or list directories/files (<code>FIND</code>, <code>NEXT</code>), but cannot access the content of a file.
Create a file item that can be used to search for files or list directories/files (<code>FIND</code>, <code>NEXT</code>), but cannot access the content of a file.
 
{|
;<var>name</var>
|-
 
| <var>name</var>
:The parameter <var>name</var> is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable <code>#new</code>.
| The parameter <var>name</var> is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable <code>#new</code>.
|}


==Text Files==
==Text Files==


<code>NEW FILE <var>name fileName</var> /Text /Read|Write|Append [ /G ] [ /P ] [ /C ]</code>
NEW FILE <var>name</var> <var>fileName</var> /Text /Read|Write|Append [ /G ] [ /P ] [ /C ]


Create a new file item to access a file in text mode.
Create a new file item to access a file in text mode.


;<var>name</var>
{|
|-
| <var>name</var>
| The parameter <var>name</var> is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable <code>#new</code>.
|-
| <var>fileName</var>
| The name of a file on disk containing plain ASCII text. This can be an absolute or a relative path.
|-
| <code>/Text</code>
| This option is mandatory when creating a text file.
|-
| <code>/Read</code>, <code>/Write</code> or <code>/Append</code>
| One of these options is mandatory. The option <code>/Read</code> opens a file in read-only mode. The option <code>/Write</code> creates a new file if <var>fileName</var> does not exist or overwrites the existing file. The option <code>/Append</code> creates a new file if the file <var>fileName</var> does not exist or appends new data to an existing file.
|-
| <code>/P</code>
| If the option <code>/P</code> is specified, the text file is processed by the {{STX}} macro preprocessor. See <code>preprocessed_file_io_example.sts</code> for details.
|-
| <code>/C</code>
| If the option <code>/C</code> is specified, the {{STX}} continuation character "<code>`</code>" will be recognised as a line continuation character. Otherwise, it will be treated like any other character.
|}


:The parameter <var>name</var> is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable <code>#new</code>.
=== Example ===
#f := new file * 'afile.txt' /Text /Append


;<var>fileName</var>
This statement will create a new file item referencing the text file "afile.txt" in the present working directory. The unique internal shell item ID for the file item is to be generated automatically (<code>*</code>). This ID is then assigned to the shell variable <var>#f</var>.
 
:The name of a file on disk containing plain ASCII text. This can be an absolute or a relative path.
 
;<var>/Text</var>
 
:This option is mandatory when creating a text file.
 
;<var>/Read|Write|Append</var>
 
:One of these options is mandatory. The option /Read opens a file in read-only mode. The option /Write creates a new file if <var>fileName</var> does not exist or overwrites the existing file. The option /Append creates a new file if the file <var>fileName</var> does not exist or appends new data to an existing file.
 
;<var>/P</var>
 
:If the option /P is specified, the text file is processed by the {{STX}} macro preprocessor. See <code>preprocessed_file_io_example.sts</code> for details.
 
;<var>/C</var>
 
:If the option /C is specified, the {{STX}} continuation character "<code>`</code>" will be recognised as a line continuation character. Otherwise, it will be treated like any other character.
 
Example:
 
<pre>
#f := new file * 'afile.txt' /Text /Append
</pre>
Create a new file item referencing the text file 'afile.txt' in the present working directory. Save the unique automatically generated shell item id in the variable #f.


==Section Files==
==Section Files==


<code>NEW FILE <var>name fileName</var> /Section [ /G ] [ /P ] [ /C ]</code>
NEW FILE <var>name</var> <var>fileName</var> /Section [ /G ] [ /P ] [ /C ]


Create a file item to access a file in section mode. A section file consists of sections marked by a section header (<code>[sectiontype {sectionname}]</code>). Use the macros <code>SECTIONFILE</code> and <code>SOURCEFILE</code> for section-file access.
Create a file item to access a section file. A section file is a text file consisting of sections each starting with a section header (<code>[sectiontype {sectionname}]</code>), similar to the well-known Windows ".INI" files. Use the macros [[Programmer_Guide/Macro_Library/SECTIONFILE|<code>SECTIONFILE</code>]] and [[Programmer_Guide/Macro_Library/SOURCEFILE|<code>SOURCEFILE</code>]] for operating on section files.


;<var>name</var>
{|
 
|-
:The parameter <var>name</var> is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable <code>#new</code>.
| <var>name</var>
 
| The parameter <var>name</var> is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable <code>#new</code>.
;<var>fileName</var>
|-
 
| <var>fileName</var>
:The name of a file on disk. This can be an absolute or a relative path.
| The name of a file on disk. This can be an absolute or a relative path.
 
|-
;<var>/Section</var>
| <code>/Section</code>
 
| This option is mandatory when creating a section file.
:This option is mandatory when creating a section file.
|-
 
| <code>/Preprocess</code>
;<var>/P</var>
| If the option <code>/P</code> is specified, the section file is processed by the {{STX}} macro preprocessor. See [http://mediawiki.kfs.oeaw.ac.at/stx/script_examples/preprocessed_file_io_example.sts preprocessed_file_io_example.sts] for details.
 
|-
:If the option /P is specified, the section file is processed by the {{STX}} macro preprocessor. See [http://mediawiki.kfs.oeaw.ac.at/stx/script_examples/preprocessed_file_io_example.sts preprocessed_file_io_example.sts] for details.
| <code>/C</code>
 
| If the option <code>/C</code> is specified, the {{STX}} continuation character "<code>`</code>" will be recognised as a line continuation character. Otherwise, it will be treated like any other character.
;<var>/C</var>
|}
 
:If the option /C is specified, the {{STX}} continuation character "<code>`</code>" will be recognised as a line continuation character. Otherwise, it will be treated like any other character.


==XML Files==
==XML Files==


<code>NEW FILE <var>name</var> [ <var>fileName</var> ] /XML [/Load] [/Exclusive] [ /G ]</code>
NEW FILE <var>name</var> [ <var>fileName</var> ] /XML [/Load] [/Exclusive] [ /G ]


Create an XML file item. If a <var>fileName</var> is specified, it must refer to an existing and valid XML file on disk, otherwise the <code>NEW</code> command will fail. If the option /Load is used, the content of the file is loaded and parsed and the root element is selected, otherwise the document is empty.
Create an XML file item. If a <var>fileName</var> is specified, it must refer to an existing and valid XML file on disk, otherwise the <code>NEW</code> command will fail. If the option <code>/Load</code> is used, the content of the file is loaded and parsed and the root element is selected, otherwise the document is empty.


;<var>name</var>
{|
 
|-
:The parameter <var>name</var> is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable <code>#new</code>.
| <var>name</var>
 
| The parameter <var>name</var> is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable <var>#new</var>.
;<var>fileName</var>
|-
 
| <var>fileName</var>
:If a file name is specified, then this is used for loading and saving the file (unless overridden).
| If a file name is specified, then this is used for loading and saving the file (unless overridden).
 
|-
;<var>/XML</var>
| <code>/XML</code>
 
| This option is mandatory when creating an XML file.
:This option is mandatory when creating an XML file.
|-
 
| <code>/Load</code>
;<var>/Load</var>
| Load the contents of the XML file specified by <var>fileName</var> from disk
 
|-
:Load the contents of the XML file specified by <var>fileName</var> from disk
| <code>/Exclusive</code>
 
| If this flag is used, the file will be locked meaning that no other users and programs may access the file until the file item is deleted.
;<var>/Exclusive</var>
|}
 
:If this flag is used, the file will be locked to all other users and programs until the item is deleted.


==Binary Files==
==Binary Files==


<code>NEW FILE <var>name</var> <var>fileName</var> /Binary [ /Read ] [ /Delete ] [ /G ]</code>
NEW FILE <var>name</var> <var>fileName</var> /Binary [ /Read ] [ /Delete ] [ /G ]


Create a new binary file item.
Create a new binary file item.


;<var>name</var>
{|
 
|-
:The parameter <var>name</var> is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable <code>#new</code>.
| <var>name</var>
 
| parameter <var>name</var> is a unique identifier for this item within the shell. If you pass an asterisk, a unique identifier will be generated automatically. This unique identifier will be stored in the local variable <var>#new</var>. Either way, the command returns the identifier of the file item, meaning that you can assign it to an {{STX}} variable.
;<var>fileName</var>
|-
 
| <var>fileName</var>
:The path to the file on disk.
| The path to the file on disk.
 
|-
;<var>/Binary</var>
| <code>/Binary</code>
 
| This option is mandatory when creating a binary file.
:This option is mandatory when creating a binary file.
|-
 
| <code>/Read</code>
;<var>/Read</var>
| Use this option to open the file in read-only mode. Otherwise the file is opened in read/write mode.
 
|-
:Use this option to open the file in read-only mode. Otherwise the file is opened in read/write mode.
| <code>/Delete</code>
 
| Use this option to delete the contents of the file. This option is only possible in read/write mode.
;<var>/Delete</var>
|}
 
:Use this option to delete the contents of the file. This option is only possible in read/write mode.


==GDX Files==
==GDX Files==


<code>NEW FILE /Y <var>name</var> <var>fileName</var> [ <var>format rows columns</var> ] [ /D ]</code>
NEW FILE /Y <var>name</var> <var>fileName</var> [ <var>format rows columns</var> ] [ /D ]


Create a GDX file. If the file does not yet exist, the <var>format</var>, <var>rows</var> and <var>columns</var> parameters are mandatory.
Create a GDX file. If the file does not yet exist, the <var>format</var>, <var>rows</var> and <var>columns</var> parameters are mandatory.


;<var>name</var>
{|
|-
|<var>name</var>
| The parameter <var>name</var> is a unique identifier (i.e., shell item ID) for this item within the shell. If you pass an asterisk, a unique identifier is automatically generated. In this case, it will be stored in the local variable <code>#new</code>. Either way, the statement will return the unique identifier so that you can assign it to a shell variable.
|-
| <var>filePath</var>
| The path to an existing GDX file on disk. This should be quoted and, indeed, must be quoted if containing space characters.
|-
| <var>format</var>
| The following data formats are supported:
{|
|-
| <code>1</code> || short
|-
| <code>2</code> || int
|-
| <code>3</code> || float
|-
| <code>4</code> || double
|}


:The parameter <var>name</var> is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable <code>#new</code>.
Note that if the file <var>fileName</var> does not exist, then this parameter is mandatory.
 
|-
;<var>filePath</var>
| <var>rows</var>
 
| The number of data rows.
:The path to an existing GDX file on disk.
|-
 
| <var>columns</var>
;<var>format</var>
| The number of data columns.
 
|-
:The following data formats are supported:
| <code>/Delete</code>
 
| If specified, the contents of the file are deleted (provided that the file already exists). Note that the contents cannot be deleted if the file is currently being accessed.
:<code>1</code> - short
|}
 
:<code>2</code> - int
 
:<code>3</code> - float
 
:<code>4</code> - double
 
:Note that if the file <var>fileName</var> does not exist, then this parameter is mandatory.
 
;<var>rows</var>
 
:The number of data rows.
 
;<var>columns</var>
 
:The number of data columns.
 
;<var>/D</var>
 
:If specified the contents of the file are deleted (if the file already exists). Note that the contents cannot be deleted if the file is currently being accessed.


Example:
=== Example ===


<code>#gdxfile := new file /Y * 'test.gdx' 4 1024 1000</code>
#gdxfile := new file /Y * 'test.gdx' 4 1024 1000

Latest revision as of 08:00, 5 May 2015

File Item
INTRODUCING NEW SET ATTRIBUTES EXAMPLES

The command NEW FILE creates a new file item. File items are used to read and write files to disk. There are special SET commands to deal with different types of files (e.g. XML files or files using the section format). The availability of these commands is dependant on the NEW FILE command variant which was used to create the item (see below).

See SET file for the SET commands and FILE Item Attributes for a list of attributes.

When you have finished with a file item, you can delete it with the command DELETE.

You should surround file names with inverted commas (apostrophes, e.g. NEW FILE * '$#fileName') if you cannot be absolutely sure that the file name is free of spaces. Otherwise, the command will fail, although the filename itself is valid.

General Options

The following options are available for all NEW FILE commands.

/G turns automatic garbage collection on. If specified, the item is automatically deleted when exiting the respective execution context (macro).
/I if set, error messages are suppressed, and warnings are generated instead.

File System Items (Dummy Files)

The following command will create a dummy file item, also called file system item. Such dummy files do not represent actual disk files and, hence, cannot be used for reading and writing to disk. Instead, they allow access to system information about files and directories.

NEW FILE name /File [ /G ]

Create a file item that can be used for file-system functions (STATUS, RENAME (=move), COPY, DELETE), but cannot access the content of a file.

name The parameter name is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable #new.

List File Items (Searching for Files and Directories)

NEW FILE name /List [ /G ]

Create a file item that can be used to search for files or list directories/files (FIND, NEXT), but cannot access the content of a file.

name The parameter name is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable #new.

Text Files

NEW FILE name fileName /Text /Read|Write|Append [ /G ] [ /P ] [ /C ]

Create a new file item to access a file in text mode.

name The parameter name is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable #new.
fileName The name of a file on disk containing plain ASCII text. This can be an absolute or a relative path.
/Text This option is mandatory when creating a text file.
/Read, /Write or /Append One of these options is mandatory. The option /Read opens a file in read-only mode. The option /Write creates a new file if fileName does not exist or overwrites the existing file. The option /Append creates a new file if the file fileName does not exist or appends new data to an existing file.
/P If the option /P is specified, the text file is processed by the STx macro preprocessor. See preprocessed_file_io_example.sts for details.
/C If the option /C is specified, the STx continuation character "`" will be recognised as a line continuation character. Otherwise, it will be treated like any other character.

Example

#f := new file * 'afile.txt' /Text /Append

This statement will create a new file item referencing the text file "afile.txt" in the present working directory. The unique internal shell item ID for the file item is to be generated automatically (*). This ID is then assigned to the shell variable #f.

Section Files

NEW FILE name fileName /Section [ /G ] [ /P ] [ /C ]

Create a file item to access a section file. A section file is a text file consisting of sections each starting with a section header ([sectiontype {sectionname}]), similar to the well-known Windows ".INI" files. Use the macros SECTIONFILE and SOURCEFILE for operating on section files.

name The parameter name is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable #new.
fileName The name of a file on disk. This can be an absolute or a relative path.
/Section This option is mandatory when creating a section file.
/Preprocess If the option /P is specified, the section file is processed by the STx macro preprocessor. See preprocessed_file_io_example.sts for details.
/C If the option /C is specified, the STx continuation character "`" will be recognised as a line continuation character. Otherwise, it will be treated like any other character.

XML Files

NEW FILE name [ fileName ] /XML [/Load] [/Exclusive] [ /G ]

Create an XML file item. If a fileName is specified, it must refer to an existing and valid XML file on disk, otherwise the NEW command will fail. If the option /Load is used, the content of the file is loaded and parsed and the root element is selected, otherwise the document is empty.

name The parameter name is a unique identifier for this item within the shell. If you pass an asterisk, the identifier is automatically generated and stored in the local variable #new.
fileName If a file name is specified, then this is used for loading and saving the file (unless overridden).
/XML This option is mandatory when creating an XML file.
/Load Load the contents of the XML file specified by fileName from disk
/Exclusive If this flag is used, the file will be locked meaning that no other users and programs may access the file until the file item is deleted.

Binary Files

NEW FILE name fileName /Binary [ /Read ] [ /Delete ] [ /G ]

Create a new binary file item.

name parameter name is a unique identifier for this item within the shell. If you pass an asterisk, a unique identifier will be generated automatically. This unique identifier will be stored in the local variable #new. Either way, the command returns the identifier of the file item, meaning that you can assign it to an STx variable.
fileName The path to the file on disk.
/Binary This option is mandatory when creating a binary file.
/Read Use this option to open the file in read-only mode. Otherwise the file is opened in read/write mode.
/Delete Use this option to delete the contents of the file. This option is only possible in read/write mode.

GDX Files

NEW FILE /Y name fileName [ format rows columns ] [ /D ]

Create a GDX file. If the file does not yet exist, the format, rows and columns parameters are mandatory.

name The parameter name is a unique identifier (i.e., shell item ID) for this item within the shell. If you pass an asterisk, a unique identifier is automatically generated. In this case, it will be stored in the local variable #new. Either way, the statement will return the unique identifier so that you can assign it to a shell variable.
filePath The path to an existing GDX file on disk. This should be quoted and, indeed, must be quoted if containing space characters.
format The following data formats are supported:
1 short
2 int
3 float
4 double

Note that if the file fileName does not exist, then this parameter is mandatory.

rows The number of data rows.
columns The number of data columns.
/Delete If specified, the contents of the file are deleted (provided that the file already exists). Note that the contents cannot be deleted if the file is currently being accessed.

Example

#gdxfile := new file /Y * 'test.gdx' 4 1024 1000

Navigation menu

Personal tools