Difference between revisions of "Documentation/BASIC Guide/Files and Directories (Runtime Library)"
m |
|||
(19 intermediate revisions by 8 users not shown) | |||
Line 1: | Line 1: | ||
− | |||
{{Documentation/BASICGuideTOC/v2 | {{Documentation/BASICGuideTOC/v2 | ||
|ShowPrevNext=block | |ShowPrevNext=block | ||
Line 7: | Line 6: | ||
|runtime=block | |runtime=block | ||
}} | }} | ||
+ | {{DISPLAYTITLE:Files and Directories ({{AOo}} Runtime Library)}} | ||
+ | __NOTOC__ | ||
+ | Working with files is one of the basic tasks of an application. The {{AOo}} API provides you with a whole range of objects with which you can create, open and modify Office documents. These are presented in detail in the [[Documentation/BASIC Guide/API Intro|Introduction to the {{AOo}} API]]. Regardless of this, in some instances you will have to directly access the file system, search through directories or edit text files. The runtime library from {{AOo}} Basic provides several fundamental functions for these tasks. | ||
− | + | {{Note|Some DOS-specific file and directory functions are no longer provided in {{AOo}}, or their function is only limited. For example, support for the <tt>ChDir</tt>, <tt>ChDrive</tt> and <tt>CurDir</tt> functions is not provided. Some DOS-specific properties are no longer used in functions that expect file properties as parameters (for example, to differentiate from concealed files and system files). This change became necessary to ensure the greatest possible level of platform independence for {{AOo}}.}} | |
− | + | == Administering Files == | |
+ | |||
+ | === Compatibility Mode === | ||
+ | |||
+ | The <tt>CompatibilityMode</tt> statement and function provide greater compatibility with VBA, by changing the operation of certain functions. The effect on any particular function is described with that function, below. | ||
+ | |||
+ | As a statement, <tt>CompatibilityMode( value )</tt> takes a Boolean value to set or clear the mode. As a function, <tt>CompatibilityMode()</tt> returns the Boolean value of the mode. | ||
− | == | + | <syntaxhighlight lang="oobas"> |
+ | CompatibilityMode( True ) 'set mode | ||
+ | CompatibilityMode( False) 'clear mode | ||
+ | |||
+ | Dim bMode as Boolean | ||
+ | bMode = CompatibilityMode() | ||
+ | </syntaxhighlight> | ||
=== Searching Through Directories === | === Searching Through Directories === | ||
− | The <tt>Dir</tt> function in {{ | + | The <tt>Dir</tt> function in {{AOo}} Basic is responsible for searching through directories for files and sub-directories. When first requested, a string containing the path of the directories to be searched must be assigned to <tt>Dir</tt> as its first parameter. The second parameter of <tt>Dir</tt> specifies whether files or directories are to be searched for. {{AOo}} Basic returns the name of the first directory entry found. To retrieve the next entry, the <tt>Dir</tt> function should be requested without parameters. If the <tt>Dir</tt> function finds no more entries, it returns an empty string. |
The following example shows how the <tt>Dir</tt> function can be used to request all files located in one directory. The procedure saves the individual file names in the <tt>AllFiles</tt> variable and then displays this in a message box. | The following example shows how the <tt>Dir</tt> function can be used to request all files located in one directory. The procedure saves the individual file names in the <tt>AllFiles</tt> variable and then displays this in a message box. | ||
− | + | <syntaxhighlight lang="oobas"> | |
− | + | Sub ShowFiles | |
− | + | Dim NextFile As String | |
− | + | Dim AllFiles As String | |
− | + | ||
− | + | AllFiles = "" | |
− | + | NextFile = Dir("C:\", 0) | |
− | + | ||
− | + | While NextFile <> "" | |
− | + | AllFiles = AllFiles & Chr(13) & NextFile | |
− | + | NextFile = Dir | |
− | + | Wend | |
− | + | ||
− | + | MsgBox AllFiles | |
+ | End Sub | ||
+ | </syntaxhighlight> | ||
− | The <tt>0</tt> (zero) used as the second parameter in the <tt>Dir</tt> function ensures that <tt>Dir</tt> only returns the names of files | + | The <tt>0</tt> (zero) used as the second parameter in the <tt>Dir</tt> function ensures that <tt>Dir</tt> only returns the names of files; directories are ignored. The following parameters can be specified here: |
* <tt>0</tt> : returns normal files | * <tt>0</tt> : returns normal files | ||
Line 42: | Line 58: | ||
The following example is virtually the same as the preceding example, but the <tt>Dir</tt> function transfers the value 16 as a parameter, which returns the sub-directories of a folder rather than the file names. | The following example is virtually the same as the preceding example, but the <tt>Dir</tt> function transfers the value 16 as a parameter, which returns the sub-directories of a folder rather than the file names. | ||
− | + | <syntaxhighlight lang="oobas"> | |
− | + | Sub ShowDirs | |
− | + | Dim NextDir As String | |
− | + | Dim AllDirs As String | |
− | + | ||
− | + | AllDirs = "" | |
− | + | NextDir = Dir("C:\", 16) | |
− | + | ||
− | + | While NextDir <> "" | |
− | + | AllDirs = AllDirs & Chr(13) & NextDir | |
− | + | NextDir = Dir | |
− | + | Wend | |
− | + | ||
− | + | MsgBox AllDirs | |
+ | End Sub | ||
+ | </syntaxhighlight> | ||
− | {{Documentation/ | + | {{Documentation/VBAnote|When requested in {{AOo}} Basic, the <tt>Dir</tt> function, using the parameter 16, only returns the sub-directories of a folder. In VBA, the function also returns the names of the standard files so that further checking is needed to retrieve the directories only. When using the <tt>CompatibilityMode ( true )</tt> function, {{AOo}} Basic behaves like VBA and the Dir function, using parameter 16, returns sub-directories and standard files.}} |
− | {{Documentation/ | + | {{Documentation/VBAnote|The options provided in VBA for searching through directories specifically for files with the '''concealed''', '''system file''', '''archived''', and '''volume name''' properties does not exist in {{AOo}} Basic because the corresponding file system functions are not available on all operating systems.}} |
− | {{Documentation/ | + | {{Documentation/VBAnote|The path specifications listed in <tt>Dir</tt> may use the * and ? place holders in both VBA and {{AOo}} Basic. In {{AOo}} Basic, the * placeholder may however only be the last character of a file name and/or file extension, which is not the case in VBA. }} |
=== Creating and Deleting Directories === | === Creating and Deleting Directories === | ||
− | {{ | + | {{AOo}} Basic provides the <tt>MkDir</tt> function for creating directories. |
− | + | <syntaxhighlight lang="oobas"> | |
+ | MkDir ("C:\SubDir1") | ||
+ | </syntaxhighlight> | ||
This function creates directories and sub-directories. All directories needed within a hierarchy are also created, if required. For example, if only the <tt>C:\SubDir1</tt> directory exists, then a call | This function creates directories and sub-directories. All directories needed within a hierarchy are also created, if required. For example, if only the <tt>C:\SubDir1</tt> directory exists, then a call | ||
− | + | <syntaxhighlight lang="oobas"> | |
+ | MkDir ("C:\SubDir1\SubDir2\SubDir3\") | ||
+ | </syntaxhighlight> | ||
creates both the <tt>C:\SubDir1\SubDir2</tt> directory and the <tt>C:\SubDir1\SubDir2\SubDir3</tt> directory. | creates both the <tt>C:\SubDir1\SubDir2</tt> directory and the <tt>C:\SubDir1\SubDir2\SubDir3</tt> directory. | ||
Line 77: | Line 99: | ||
The <tt>RmDir</tt> function deletes directories. | The <tt>RmDir</tt> function deletes directories. | ||
− | + | <syntaxhighlight lang="oobas"> | |
+ | RmDir ("C:\SubDir1\SubDir2\SubDir3\") | ||
+ | </syntaxhighlight> | ||
If the directory contains sub-directories or files, these are '''also deleted.''' You should therefore be careful when using <tt>RmDir</tt>. | If the directory contains sub-directories or files, these are '''also deleted.''' You should therefore be careful when using <tt>RmDir</tt>. | ||
− | {{Documentation/ | + | {{Documentation/VBAnote|In VBA, the <tt>MkDir</tt> and <tt>RmDir</tt> functions only relate to the current directory. In {{AOo}} Basic on the other hand, <tt>MkDir</tt> and <tt>RmDir</tt> can be used to create or delete levels of directories.}} |
− | {{Documentation/ | + | {{Documentation/VBAnote|In VBA, <tt>RmDir</tt> produces an error message if a directory contains a file. In {{AOo}} Basic, the directory '''and all its files''' are deleted. If you use the <tt>CompatibilityMode ( true )</tt> function, {{AOo}} Basic will behave like VBA.}} |
=== Copying, Renaming, Deleting and Checking the Existence of Files === | === Copying, Renaming, Deleting and Checking the Existence of Files === | ||
Line 89: | Line 113: | ||
The following call creates a copy of the <tt>Source</tt> file under the name of <tt>Destination</tt>: | The following call creates a copy of the <tt>Source</tt> file under the name of <tt>Destination</tt>: | ||
− | + | <syntaxhighlight lang="oobas"> | |
+ | FileCopy(Source, Destination) | ||
+ | </syntaxhighlight> | ||
With the help of the following function you can rename the <tt>OldName</tt> file with <tt>NewName</tt>. The <tt>As</tt> keyword syntax, and the fact that a comma is not used, goes back to the roots of the Basic language. | With the help of the following function you can rename the <tt>OldName</tt> file with <tt>NewName</tt>. The <tt>As</tt> keyword syntax, and the fact that a comma is not used, goes back to the roots of the Basic language. | ||
− | + | <syntaxhighlight lang="oobas"> | |
+ | Name OldName As NewName | ||
+ | </syntaxhighlight> | ||
The following call deletes the <tt>Filename</tt> file. If you want to delete directory (including its files) use the <tt>RmDir</tt> function. | The following call deletes the <tt>Filename</tt> file. If you want to delete directory (including its files) use the <tt>RmDir</tt> function. | ||
− | + | <syntaxhighlight lang="oobas"> | |
+ | Kill(Filename) | ||
+ | </syntaxhighlight> | ||
The <tt>FileExists</tt> function can be used to check whether a file exists: | The <tt>FileExists</tt> function can be used to check whether a file exists: | ||
− | + | <syntaxhighlight lang="oobas"> | |
− | + | If FileExists(Filename) Then | |
− | + | MsgBox "file exists." | |
+ | End If | ||
+ | </syntaxhighlight> | ||
=== Reading and Changing File Properties === | === Reading and Changing File Properties === | ||
Line 111: | Line 143: | ||
The following call returns some properties about a file. | The following call returns some properties about a file. | ||
− | + | <syntaxhighlight lang="oobas"> | |
− | + | Dim Attr As Integer | |
+ | Attr = GetAttr(Filename) | ||
+ | </syntaxhighlight> | ||
The return value is provided as a bit mask in which the following values are possible: | The return value is provided as a bit mask in which the following values are possible: | ||
Line 121: | Line 155: | ||
The following example determines the bit mask of the <tt>test.txt</tt> file and checks whether this is read-only whether it is a directory. If neither of these apply, <tt>FileDescription</tt> is assigned the "normal" string. | The following example determines the bit mask of the <tt>test.txt</tt> file and checks whether this is read-only whether it is a directory. If neither of these apply, <tt>FileDescription</tt> is assigned the "normal" string. | ||
− | + | <syntaxhighlight lang="oobas"> | |
− | + | Dim FileMask As Integer | |
− | + | Dim FileDescription As String | |
− | + | ||
− | + | FileMask = GetAttr("test.txt") | |
− | + | ||
− | + | If (FileMask AND 1) > 0 Then | |
− | + | FileDescription = FileDescription & " read-only " | |
− | + | End IF | |
− | + | ||
− | + | If (FileMask AND 16) > 0 Then | |
− | + | FileDescription = FileDescription & " directory " | |
− | + | End IF | |
− | + | ||
− | + | If FileDescription = "" Then | |
− | + | FileDescription = " normal " | |
− | + | End IF | |
− | + | ||
+ | MsgBox FileDescription | ||
+ | </syntaxhighlight> | ||
− | {{Documentation/ | + | {{Documentation/VBAnote|The flags used in VBA for querying the '''concealed''', '''system file,archived''' and '''volume name''' file properties are not supported in {{AOo}} Basic because these are Windows-specific and are not or are only partially available on other operating systems.}} |
The <tt>SetAttr</tt> function permits the properties of a file to be changed. The following call can therefore be used to provide a file with read-only status: | The <tt>SetAttr</tt> function permits the properties of a file to be changed. The following call can therefore be used to provide a file with read-only status: | ||
− | + | <syntaxhighlight lang="oobas"> | |
+ | SetAttr("test.txt", 1) | ||
+ | </syntaxhighlight> | ||
An existing read-only status can be deleted with the following call: | An existing read-only status can be deleted with the following call: | ||
− | + | <syntaxhighlight lang="oobas"> | |
+ | SetAttr("test.txt", 0) | ||
+ | </syntaxhighlight> | ||
The date and time of the last amendment to a file are provided by the <tt>FileDateTime</tt> function. The date is formatted here in accordance with the country-specific settings used on the system. | The date and time of the last amendment to a file are provided by the <tt>FileDateTime</tt> function. The date is formatted here in accordance with the country-specific settings used on the system. | ||
− | + | <syntaxhighlight lang="oobas"> | |
+ | FileDateTime("test.txt") ' Provides date and time of the last file amendment. | ||
+ | </syntaxhighlight> | ||
The <tt>FileLen</tt> function determines the length of a file in bytes (as long integer value). | The <tt>FileLen</tt> function determines the length of a file in bytes (as long integer value). | ||
− | + | <syntaxhighlight lang="oobas"> | |
+ | FileLen("test.txt") ' Provides the length of the file in bytes | ||
+ | </syntaxhighlight> | ||
== Writing and Reading Text Files == | == Writing and Reading Text Files == | ||
− | {{ | + | {{AOo}} Basic provides a whole range of methods for reading and writing files. The following explanations relate to working with text files ('''not''' text documents). |
=== Writing Text Files === | === Writing Text Files === | ||
Line 166: | Line 210: | ||
Before a text file is accessed, it must first be opened. To do this, a free '''file handle''' is needed, which clearly identifies the file for subsequent file access. | Before a text file is accessed, it must first be opened. To do this, a free '''file handle''' is needed, which clearly identifies the file for subsequent file access. | ||
− | The <tt>FreeFile</tt> function is used to create a free file handle. The handle is used as a parameter for the <tt>Open</tt> instruction, which opens the file. To open a file so that it can be | + | The <tt>FreeFile</tt> function is used to create a free file handle: |
+ | |||
+ | <syntaxhighlight lang="oobas"> | ||
+ | FileNo = FreeFile | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | <tt>FileNo</tt> is an integer variable that receives the file handle. The handle is then used as a parameter for the <tt>Open</tt> instruction, which opens the file. | ||
+ | |||
+ | To open a file so that it can be written as a text file, the <tt>Open</tt> call is: | ||
− | + | <syntaxhighlight lang="oobas"> | |
+ | Open Filename For Output As #FileNo | ||
+ | </syntaxhighlight> | ||
<tt>Filename</tt> is a string containing the name of the file. <tt>FileNo</tt> is the handle created by the <tt>FreeFile</tt> function. | <tt>Filename</tt> is a string containing the name of the file. <tt>FileNo</tt> is the handle created by the <tt>FreeFile</tt> function. | ||
− | Once the file is opened, the <tt>Print</tt> instruction can | + | Once the file is opened, the <tt>Print</tt> instruction can create the file contents, line by line: |
− | + | <syntaxhighlight lang="oobas"> | |
+ | Print #FileNo, "This is a test line." | ||
+ | </syntaxhighlight> | ||
<tt>FileNo</tt> also stands for the file handle here. The second parameter specifies the text that is to be saved as a line of the text file. | <tt>FileNo</tt> also stands for the file handle here. The second parameter specifies the text that is to be saved as a line of the text file. | ||
Line 180: | Line 236: | ||
Once the writing process has been completed, the file must be closed using a <tt>Close</tt> call: | Once the writing process has been completed, the file must be closed using a <tt>Close</tt> call: | ||
− | + | <syntaxhighlight lang="oobas"> | |
+ | Close #FileNo | ||
+ | </syntaxhighlight> | ||
Again here, the file handle should be specified. | Again here, the file handle should be specified. | ||
− | The following example shows how a text file is opened, | + | The following example shows how a text file is opened, written, and closed: |
+ | |||
+ | <syntaxhighlight lang="oobas"> | ||
+ | Dim FileNo As Integer | ||
+ | Dim CurrentLine As String | ||
+ | Dim Filename As String | ||
+ | |||
+ | Filename = "c:\data.txt" ' Define file name | ||
+ | FileNo = FreeFile ' Establish free file handle | ||
− | + | Open Filename For Output As #FileNo ' Open file (writing mode) | |
− | + | Print #FileNo, "This is a line of text" ' Save line | |
− | + | Print #FileNo, "This is another line of text" ' Save line | |
− | + | Close #FileNo ' Close file | |
− | + | </syntaxhighlight> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=== Reading Text Files === | === Reading Text Files === | ||
Line 204: | Line 264: | ||
Finally, when calling up a text file, the <tt>eof</tt> instruction is used to check whether the end of the file has been reached: | Finally, when calling up a text file, the <tt>eof</tt> instruction is used to check whether the end of the file has been reached: | ||
− | + | <syntaxhighlight lang="oobas"> | |
+ | eof(FileNo) | ||
+ | </syntaxhighlight> | ||
The following example shows how a text file can be read: | The following example shows how a text file can be read: | ||
− | + | <syntaxhighlight lang="oobas"> | |
− | + | Dim FileNo As Integer | |
− | + | Dim CurrentLine As String | |
− | + | Dim File As String | |
− | + | Dim Msg as String | |
− | + | ||
− | + | ' Define filename | |
− | + | Filename = "c:\data.txt" | |
− | + | ||
− | + | ' Establish free file handle | |
− | + | FileNo = Freefile | |
− | + | ||
− | + | ' Open file (reading mode) | |
− | + | Open Filename For Input As FileNo | |
− | + | ||
− | + | ' Check whether file end has been reached | |
− | + | Do While not eof(FileNo) | |
− | + | ' Read line | |
− | + | Line Input #FileNo, CurrentLine | |
− | + | If CurrentLine <>"" then | |
− | + | Msg = Msg & CurrentLine & Chr(13) | |
− | + | end if | |
− | + | Loop | |
+ | |||
+ | ' Close file | ||
− | + | Close #FileNo | |
− | + | Msgbox Msg | |
− | + | </syntaxhighlight> | |
− | |||
The individual lines are retrieved in a <tt>Do While</tt> loop, saved in the <tt>Msg</tt> variable, and displayed at the end in a message box. | The individual lines are retrieved in a <tt>Do While</tt> loop, saved in the <tt>Msg</tt> variable, and displayed at the end in a message box. | ||
− | + | ||
+ | {{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Files and Directories (Runtime Library)}} | ||
{{PDL1}} | {{PDL1}} |
Latest revision as of 12:13, 30 January 2021
Working with files is one of the basic tasks of an application. The Apache OpenOffice API provides you with a whole range of objects with which you can create, open and modify Office documents. These are presented in detail in the Introduction to the Apache OpenOffice API. Regardless of this, in some instances you will have to directly access the file system, search through directories or edit text files. The runtime library from Apache OpenOffice Basic provides several fundamental functions for these tasks.
Administering Files
Compatibility Mode
The CompatibilityMode statement and function provide greater compatibility with VBA, by changing the operation of certain functions. The effect on any particular function is described with that function, below.
As a statement, CompatibilityMode( value ) takes a Boolean value to set or clear the mode. As a function, CompatibilityMode() returns the Boolean value of the mode.
CompatibilityMode( True ) 'set mode
CompatibilityMode( False) 'clear mode
Dim bMode as Boolean
bMode = CompatibilityMode()
Searching Through Directories
The Dir function in Apache OpenOffice Basic is responsible for searching through directories for files and sub-directories. When first requested, a string containing the path of the directories to be searched must be assigned to Dir as its first parameter. The second parameter of Dir specifies whether files or directories are to be searched for. Apache OpenOffice Basic returns the name of the first directory entry found. To retrieve the next entry, the Dir function should be requested without parameters. If the Dir function finds no more entries, it returns an empty string.
The following example shows how the Dir function can be used to request all files located in one directory. The procedure saves the individual file names in the AllFiles variable and then displays this in a message box.
Sub ShowFiles
Dim NextFile As String
Dim AllFiles As String
AllFiles = ""
NextFile = Dir("C:\", 0)
While NextFile <> ""
AllFiles = AllFiles & Chr(13) & NextFile
NextFile = Dir
Wend
MsgBox AllFiles
End Sub
The 0 (zero) used as the second parameter in the Dir function ensures that Dir only returns the names of files; directories are ignored. The following parameters can be specified here:
- 0 : returns normal files
- 16 : sub-directories
The following example is virtually the same as the preceding example, but the Dir function transfers the value 16 as a parameter, which returns the sub-directories of a folder rather than the file names.
Sub ShowDirs
Dim NextDir As String
Dim AllDirs As String
AllDirs = ""
NextDir = Dir("C:\", 16)
While NextDir <> ""
AllDirs = AllDirs & Chr(13) & NextDir
NextDir = Dir
Wend
MsgBox AllDirs
End Sub
Creating and Deleting Directories
Apache OpenOffice Basic provides the MkDir function for creating directories.
MkDir ("C:\SubDir1")
This function creates directories and sub-directories. All directories needed within a hierarchy are also created, if required. For example, if only the C:\SubDir1 directory exists, then a call
MkDir ("C:\SubDir1\SubDir2\SubDir3\")
creates both the C:\SubDir1\SubDir2 directory and the C:\SubDir1\SubDir2\SubDir3 directory.
The RmDir function deletes directories.
RmDir ("C:\SubDir1\SubDir2\SubDir3\")
If the directory contains sub-directories or files, these are also deleted. You should therefore be careful when using RmDir.
VBA : In VBA, the MkDir and RmDir functions only relate to the current directory. In Apache OpenOffice Basic on the other hand, MkDir and RmDir can be used to create or delete levels of directories. |
Copying, Renaming, Deleting and Checking the Existence of Files
The following call creates a copy of the Source file under the name of Destination:
FileCopy(Source, Destination)
With the help of the following function you can rename the OldName file with NewName. The As keyword syntax, and the fact that a comma is not used, goes back to the roots of the Basic language.
Name OldName As NewName
The following call deletes the Filename file. If you want to delete directory (including its files) use the RmDir function.
Kill(Filename)
The FileExists function can be used to check whether a file exists:
If FileExists(Filename) Then
MsgBox "file exists."
End If
Reading and Changing File Properties
When working with files, it is sometimes important to be able to establish the file properties, the time the file was last changed and the length of the file.
The following call returns some properties about a file.
Dim Attr As Integer
Attr = GetAttr(Filename)
The return value is provided as a bit mask in which the following values are possible:
- 1 : read-only file
- 16 : name of a directory
The following example determines the bit mask of the test.txt file and checks whether this is read-only whether it is a directory. If neither of these apply, FileDescription is assigned the "normal" string.
Dim FileMask As Integer
Dim FileDescription As String
FileMask = GetAttr("test.txt")
If (FileMask AND 1) > 0 Then
FileDescription = FileDescription & " read-only "
End IF
If (FileMask AND 16) > 0 Then
FileDescription = FileDescription & " directory "
End IF
If FileDescription = "" Then
FileDescription = " normal "
End IF
MsgBox FileDescription
The SetAttr function permits the properties of a file to be changed. The following call can therefore be used to provide a file with read-only status:
SetAttr("test.txt", 1)
An existing read-only status can be deleted with the following call:
SetAttr("test.txt", 0)
The date and time of the last amendment to a file are provided by the FileDateTime function. The date is formatted here in accordance with the country-specific settings used on the system.
FileDateTime("test.txt") ' Provides date and time of the last file amendment.
The FileLen function determines the length of a file in bytes (as long integer value).
FileLen("test.txt") ' Provides the length of the file in bytes
Writing and Reading Text Files
Apache OpenOffice Basic provides a whole range of methods for reading and writing files. The following explanations relate to working with text files (not text documents).
Writing Text Files
Before a text file is accessed, it must first be opened. To do this, a free file handle is needed, which clearly identifies the file for subsequent file access.
The FreeFile function is used to create a free file handle:
FileNo = FreeFile
FileNo is an integer variable that receives the file handle. The handle is then used as a parameter for the Open instruction, which opens the file.
To open a file so that it can be written as a text file, the Open call is:
Open Filename For Output As #FileNo
Filename is a string containing the name of the file. FileNo is the handle created by the FreeFile function.
Once the file is opened, the Print instruction can create the file contents, line by line:
Print #FileNo, "This is a test line."
FileNo also stands for the file handle here. The second parameter specifies the text that is to be saved as a line of the text file.
Once the writing process has been completed, the file must be closed using a Close call:
Close #FileNo
Again here, the file handle should be specified.
The following example shows how a text file is opened, written, and closed:
Dim FileNo As Integer
Dim CurrentLine As String
Dim Filename As String
Filename = "c:\data.txt" ' Define file name
FileNo = FreeFile ' Establish free file handle
Open Filename For Output As #FileNo ' Open file (writing mode)
Print #FileNo, "This is a line of text" ' Save line
Print #FileNo, "This is another line of text" ' Save line
Close #FileNo ' Close file
Reading Text Files
Text files are read in the same way that they are written. The Open instruction used to open the file contains the For Input expression in place of the For Output expression and, rather than the Print command for writing data, the Line Input instruction should be used to read the data.
Finally, when calling up a text file, the eof instruction is used to check whether the end of the file has been reached:
eof(FileNo)
The following example shows how a text file can be read:
Dim FileNo As Integer
Dim CurrentLine As String
Dim File As String
Dim Msg as String
' Define filename
Filename = "c:\data.txt"
' Establish free file handle
FileNo = Freefile
' Open file (reading mode)
Open Filename For Input As FileNo
' Check whether file end has been reached
Do While not eof(FileNo)
' Read line
Line Input #FileNo, CurrentLine
If CurrentLine <>"" then
Msg = Msg & CurrentLine & Chr(13)
end if
Loop
' Close file
Close #FileNo
Msgbox Msg
The individual lines are retrieved in a Do While loop, saved in the Msg variable, and displayed at the end in a message box.
Content on this page is licensed under the Public Documentation License (PDL). |