---
title: "TextFile (_TextFile class)"
canonical: "https://docs.cuesystem.com/space/CVCHELP/512983245/TextFile%20(_TextFile%20class)"
format: markdown
---
> Macro (toc)

---

TextFile file content must be in **UTF16-LE** encoding with BOM.   

# Close

This function closes the file for that instance _TextFile. Over a closed file cannot perform any functions except Open. See <u>examples</u>. 

#### Syntax 

```visualbasic
Function Close()
```

# Copy

This function copies the current contents of an opened file to the destination file. If the destination file does not exist, it is automatically created. If it exists and the parameter eraseexisting is false, then the copy will fail. See <u>examples</u>. 

The function <u>[GetLastError](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#GetLastError)</u> returns the error number that occurred while executing the command. The errors that can happen: <u>[78](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#78)</u>, <u>[103](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#103)</u>, <u>[104](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#104)</u>, <u>[105](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#105)</u>, <u>[106](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#106)</u>, <u>[107](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#107)</u>, <u>[108](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#108)</u>, <u>[109](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#109)</u>, <u>[110](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#110)</u>. 

#### Syntax 

```visualbasic
Function Copy( 
  destinationfilepathname As Text, 
  Optional eraseexisting As Boolean := True) 
As Boolean
```

#### Parameters 

destinationfilepathname 

<u>[Text](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Text)</u> It is the pathname of the destination file. See <u>[rules of names of files and folders](/wiki/spaces/CVCHELP/pages/512983041/Files+folders#NamesOfFilesAndFolders)</u>. 

eraseexisting 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> Optional, defaults to True. If it is True, the existing file is erased. 

#### Return value 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is True on success or False if an error occurs. The function GetLastError returns the error number. 

# EraseContent

This function deleted file contents. See <u>examples</u>. 

The function <u>[GetLastError](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#GetLastError)</u> returns the error number that occurred while executing the command. The errors that can happen: <u>[78](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#78)</u>, <u>[102](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#102)</u>, <u>[103](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#103)</u>. 

#### Syntax 

```visualbasic
Function EraseContent() As Boolean
```

#### Return value 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is True on success or False if an error occurs. The function GetLastError returns the error number. 

# GetPathName

This function returns the path to an opened file in its entirety, including the directory name. See <u>examples</u>. 

The function <u>[GetLastError](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#GetLastError)</u> returns the error number that occurred while executing the command. The errors that can happen: <u>[78](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#78)</u>, <u>[80](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#80)</u>, <u>[103](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#103)</u>. 

#### Syntax 

```visualbasic
Function GetPathName(ByRef filepath As Text) As Boolean
```

#### Parameters 

filepath 

<u>[Text](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Text)</u> It is the pathname of the destination file. 

#### Return value 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is True on success or False if an error occurs. The function GetLastError returns the error number. 

# Open

This function opens a file in read and write mode and sets the position to start. The file is always opened exclusively. If the file does not exist, it is automatically created. If there is no directory structure, it is automatically created. See <u>examples</u>. The file must be in UTF16-LE encoding with BOM otherwise the function returns False and the error is set to value 111. 

The function <u>[GetLastError](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#GetLastError)</u> returns the error number that occurred while executing the command. The errors that can happen: <u>[77](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#77)</u>, <u>[79](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#79)</u>, <u>[103](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#103)</u>, <u>[108](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#108)</u>, <u>[109](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#109)</u>, <u>[110](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#110)</u>, <u>[111](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#111)</u>, <u>[119](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#119)</u>. 

#### Syntax 

```visualbasic
Function Open(filepathname As Text) As Boolean
```

#### Parameters* *

filepathname 

<u>[Text](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Text)</u> It is the pathname of the file. See <u>[rules of names of files and folders](/wiki/spaces/CVCHELP/pages/512983041/Files+folders#NamesOfFilesAndFolders)</u>. 

#### Return value 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is True on success or False if an error occurs. The function GetLastError returns the error number. 

# OpenReadOnly

This function opens a file in read-only mode and sets the position to start. If the file does not exist, one is not created and the function returns False. See <u>examples</u>. The file must be in UTF16-LE encoding with BOM otherwise the function returns False and the error is set to value 111. 

The function <u>[GetLastError](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#GetLastError)</u> returns the error number that occurred while executing the command. The errors that can happen: <u>[77](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#77)</u>, <u>[79](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#79)</u>, <u>[103](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#103)</u>, <u>[108](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#108)</u>, <u>[109](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#109)</u>, <u>[110](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#110)</u>, <u>[111](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#111)</u>, <u>[119](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#119)</u>. 

#### Syntax 

```visualbasic
Function OpenReadOnly(filepathname As Text) As Boolean
```

#### Parameters 

filepathname 

<u>[Text](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Text)</u> It is the pathname of the file. See <u>[rules of names of files and folders](/wiki/spaces/CVCHELP/pages/512983041/Files+folders#NamesOfFilesAndFolders)</u>. 

#### Return value 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is True on success or False if an error occurs. The function GetLastError returns the error number. 

# ReadText

This function reads the optional number of characters from a file. A file position is shifted by the number of characters read. If the specified number of characters numofchars = 0, then reads as many characters as there is capacity destinationvalue. When the end of the file is reached False is returned. If reading fails for other reasons, False is returned.  See <u>examples</u>.  

The function <u>[GetLastError](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#GetLastError)</u> returns the error number that occurred while executing the command. The errors that can happen: <u>[78](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#78)</u>, <u>[103](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#103)</u>, <u>[112](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#112)</u>. 

#### Syntax 

```visualbasic
Function ReadText( 
  ByRef destinationvalue As Text, 
  Optional numofchars As Long := 0) 
As Boolean
```

#### Parameters 

destinationvalue 

<u>[Text](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Text)</u> It is the destination text. 

numofchars 

<u>Long</u> Optional, defaults to 0. It is the number of characters to read. If it is 0, then reads as many characters as there is capacity destinationvalue. 

#### Return value 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is True on success or False if an error occurs. The function GetLastError returns the error number. 

# WriteText

This function writes the value of the source text at end of the file. See <u>examples</u>. 

The function <u>[GetLastError](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#GetLastError)</u> returns the error number that occurred while executing the command. The errors that can happen: <u>[78](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#78)</u>, <u>[103](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#103)</u>, <u>[115](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#115)</u>. 

#### Syntax 

```visualbasic
Function WriteText(texttowrite As Text) As Boolean
```

#### Parameters 

texttowrite 

<u>[Text](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Text)</u> It is the text to write. 

#### Return value 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is True on success or False if an error occurs. The function GetLastError returns the error number. 

# Examples

#### Reading/writing text data 

```visualbasic
Var CfgTextFile As _TextFile 
Var CfgText As Text[100] 

If CfgTextFile.Open("/filestorage/cfg.txt") Then 
  CfgTextFile.EraseContent() 
  CfgTextFile.WriteText("Hello, how are you?") 
  CfgTextFile.Close() 
End If 

If CfgTextFile.Open("/filestorage/cfg.txt") Then 
  CfgTextFile.ReadText(CfgText) 
  CfgTextFile.Close() 
End If
```

#### Copy 

```visualbasic
Var CfgTextFile As _TextFile 

If CfgTextFile.Open("/filestorage/cfg.txt") Then 
  CfgTextFile.Copy("/filestorage/cfg2.txt") 
  CfgTextFile.Close() 
End If
```

#### GetPathName 

```visualbasic
Var CfgTextFile As _TextFile 
Var FilePath As Text[100] 

If CfgTextFile.Open("/filestorage/cfg.txt") Then 
  CfgTextFile.GetPathName(FilePath) 
  CfgTextFile.Close() 
End If
```