---
title: "VarFile (_VarFile class)"
canonical: "https://docs.cuesystem.com/space/CVCHELP/512983362/VarFile%20(_VarFile%20class)"
format: markdown
---
> Macro (toc)

---

# Close 

This function closes the file for that instance _VarFile. 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 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 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. 

# ReadValues 

This function reads one or more values from the current reading position. A current pointer position is shifted by the number of loaded values. The function only works with data types <u>Boolean</u>, <u>Byte</u>, <u>Long</u>, <u>Double</u>, <u>Time</u>, <u>Char</u>, <u>Text</u>, and <u>ByteString</u>. When the end of the file is reached 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>, <u>[116](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#116)</u>, <u>[117](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#117)</u>. 

#### Syntax 

```visualbasic
Function ReadValues(ByRef ParamArray) As Boolean
```

#### Parameters 

ParamArray 

<u>[ParamArray](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/509149361)</u> It is the array of values. 

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

# WriteValues 

This function writes the value of one or more variables at end of the file. The function only works with data types <u>Boolean</u>, <u>Byte</u>, <u>Long</u>, <u>Double</u>, <u>Time</u>, <u>Char</u>, <u>Text</u>, and <u>ByteString</u>. 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>[113](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#113)</u>, <u>[114](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#114)</u>, <u>[115](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#115)</u>. 

#### Syntax 

```visualbasic
Function WriteValues(ParamArray) As Boolean
```

#### Parameters 

ParamArray 

<u>[ParamArray](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/509149361)</u> It is the array of values. 

#### 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 various data types 

```visualbasic
Var CfgVarFile As _VarFile 
Var BooleanData As Boolean := True 
Var ByteData As Byte := 123 
Var LongData As Long := 1234567890 
Var DoubleData As Double := 3.1415926535 
Var TimeData As Time := :11:22:33.44 
Var CharData As Char := 'A' 
Var TextData As Text[100] := "Hello, how are you?" 
Var ByteStringData As ByteString[100] := &S"First line\0ASecond line" 

If CfgVarFile.Open("/filestorage/cfg.bin") Then 
  CfgVarFile.EraseContent() 
  CfgVarFile.WriteValues( 
    BooleanData, 
    ByteData, 
    LongData, 
    DoubleData, 
    TimeData, 
    CharData, 
    TextData, 
    ByteStringData) 
  CfgVarFile.Close() 
End If 

If CfgVarFile.Open("/filestorage/cfg.bin") Then 
  CfgVarFile.ReadValues( 
    BooleanData, 
    ByteData, 
    LongData, 
    DoubleData, 
    TimeData, 
    CharData, 
    TextData, 
    ByteStringData) 
  CfgVarFile.Close() 
End If
```

#### Reading/writing structure 

```visualbasic
Structure STRUCT_ROOM 
  Var Name As Text[50] 
  Var Length As Long 
  Var Width As Long 
  Var Height As Long 
End Structure 

Process Autoexec() 
  Var CfgVarFile As _VarFile 
  Var Room As STRUCT_ROOM 

  Room.Name := "Bedroom" 
  Room.Length := 5 
  Room.Width := 2 
  Room.Height := 3 

  If CfgVarFile.Open("/filestorage/bedroom") Then 
    CfgVarFile.EraseContent() 
    CfgVarFile.WriteValues(Room.Name, Room.Length, Room.Width, Room.Height) 
    CfgVarFile.Close() 
  End If 

  If CfgVarFile.Open("/filestorage/bedroom") Then 
    CfgVarFile.ReadValues(Room.Name, Room.Length, Room.Width, Room.Height) 
    CfgVarFile.Close() 
  End If
End Process
```

#### Reading/writing array 

```visualbasic
Const ITEMS_COUNT As Long := 100 

Process Autoexec() 
  Var CfgVarFile As _VarFile 
  Var i As Long 
  Var a[ITEMS_COUNT] As Long 

  If CfgVarFile.Open("/filestorage/temperatures") Then 
    CfgVarFile.EraseContent() 
    For i := 1 To ITEMS_COUNT 
      CfgVarFile.WriteValues(a[i]) 
    End For 
    CfgVarFile.Close() 
  End If 

  If CfgVarFile.Open("/filestorage/temperatures") Then 
    For i := 1 To ITEMS_COUNT 
      CfgVarFile.ReadValues(a[i]) 
    End For 
    CfgVarFile.Close() 
  End If 
End Process
```

#### Reading/writing array of structures 

```visualbasic
Structure STRUCT_ROOM 
  Var Name As Text[50] 
  Var Length As Long 
  Var Width As Long 
  Var Height As Long 
End Structure 

Const ITEMS_COUNT As Long := 100 

Process Autoexec() 
  Var CfgVarFile As _VarFile 
  Var i As Long 
  Var Rooms[ITEMS_COUNT] As STRUCT_ROOM 

  If CfgVarFile.Open("/filestorage/allmyrooms") Then 
    CfgVarFile.EraseContent() 
    For i := 1 To ITEMS_COUNT 
      CfgVarFile.WriteValues( 
      Rooms[i].Name, 
      Rooms[i].Length, 
      Rooms[i].Width, 
      Rooms[i].Height) 
    End For 
    CfgVarFile.Close() 
  End If 

  If CfgVarFile.Open("/filestorage/allmyrooms") Then 
    For i := 1 To ITEMS_COUNT 
      CfgVarFile.ReadValues( 
        Rooms[i].Name, 
        Rooms[i].Length, 
        Rooms[i].Width, 
        Rooms[i].Height) 
    End For 
    CfgVarFile.Close() 
  End If 
End Process
```

#### Copy 

```visualbasic
Var CfgVarFile As _VarFile 

If CfgVarFile.Open("/filestorage/cfg.bin") Then 
  CfgVarFile.Copy("/filestorage/cfg2.bin") 
  CfgVarFile.Close() 
End If
```

#### GetPathName 

```visualbasic
Var CfgVarFile As _VarFile 
Var FilePath As Text[100] 

If CfgVarFile.Open("/filestorage/cfg.bin") Then 
  CfgVarFile.GetPathName(FilePath) 
  CfgVarFile.Close() 
End If
```