<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="VB6Parse Library Reference - seek - File">
<title>seek - File - VB6Parse Library Reference</title>
<link rel="stylesheet" href="../../../assets/css/style.css">
<link rel="stylesheet" href="../../../assets/css/docs-style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
<script src="../../../assets/js/theme-switcher.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body>
<header class="docs-header">
<div class="container">
<h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/file/index.html">File</a> / seek</h1>
<p class="tagline">VB6 Library Reference</p>
</div>
</header>
<nav class="docs-nav">
<div class="container">
<a href="../../../index.html">Home</a>
<a href="../../../library/index.html">Library Reference</a>
<a href="../../../documentation.html">Documentation</a>
<a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
<a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
<button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
<span class="theme-icon">🌙</span>
</button>
</div>
</nav>
<main class="container">
<article class="library-item">
<h1 id="seek-function">Seek Function</h1>
<p>Returns a Long specifying the current read/write position within a file opened using Open statement.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Seek(filenumber)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>filenumber</code> - Required. Any valid Integer file number.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a Long value indicating the current position in the file:
- For Random mode files: Returns the record number of the next record to be read or written (1-based)
- For Binary, Output, Append, and Input mode files: Returns the byte position where the next operation occurs (1-based)
- Position 1 is the beginning of the file</p>
<h2 id="remarks">Remarks</h2>
<p>The Seek function is used to determine the current position in a file opened with the Open statement.
It is particularly useful for:
- Determining current position before reading/writing
- Saving position to return to later
- Calculating file progress (current position vs file size using LOF)
- Implementing custom file navigation
- Verifying position after Seek statement
For Random access files, Seek returns the record number (1-based) of the next record to be read or written.
For all other access modes (Binary, Input, Output, Append), Seek returns the byte position (1-based).
The Seek function is different from the Seek statement:
- Seek function: Returns current position (read-only operation)
- Seek statement: Sets the position for next read/write (write operation)
The file must be open before calling Seek. Use <code>FreeFile</code> to get an available file number.
Always close files when done using Close statement.</p>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Current Position</strong>: Determine where you are in the file</li>
<li><strong>Save Position</strong>: Store current position to return to later</li>
<li><strong>Progress Tracking</strong>: Calculate percentage of file processed</li>
<li><strong>Navigation</strong>: Implement forward/backward navigation in files</li>
<li><strong>Record Counting</strong>: Count records processed in Random access</li>
<li><strong>Byte Counting</strong>: Track bytes read/written in Binary mode</li>
<li><strong>Position Verification</strong>: Verify Seek statement worked correctly</li>
<li><strong>File Parsing</strong>: Parse structured binary files with position tracking</li>
</ol>
<h2 id="basic-examples">Basic Examples</h2>
<pre><code class="language-vbnet">' Example 1: Get current position in binary file
Dim FileNum As Integer
Dim CurrentPos As Long
FileNum = FreeFile
Open "data.bin" For Binary As #FileNum
CurrentPos = Seek(FileNum) ' Returns 1 at start
Debug.Print "Position: " & CurrentPos
Close #FileNum</code></pre>
<pre><code class="language-vbnet">' Example 2: Get current record number in random access
Dim FileNum As Integer
Dim RecordNum As Long
FileNum = FreeFile
Open "records.dat" For Random As #FileNum Len = 100
RecordNum = Seek(FileNum) ' Returns next record number
Debug.Print "Next Record: " & RecordNum
Close #FileNum</code></pre>
<pre><code class="language-vbnet">' Example 3: Save and restore position
Dim FileNum As Integer
Dim SavedPos As Long
FileNum = FreeFile
Open "data.txt" For Input As #FileNum
' Read some data...
SavedPos = Seek(FileNum) ' Save position
' Read more data...
Seek #FileNum, SavedPos ' Restore position
Close #FileNum</code></pre>
<pre><code class="language-vbnet">' Example 4: Calculate progress percentage
Dim FileNum As Integer
Dim Progress As Double
FileNum = FreeFile
Open "large.dat" For Binary As #FileNum
Progress = (Seek(FileNum) / LOF(FileNum)) * 100
Debug.Print "Progress: " & Format(Progress, "0.0") & "%"
Close #FileNum</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="pattern-1-getcurrentposition">Pattern 1: <code>GetCurrentPosition</code></h3>
<p>Get current file position with error handling</p>
<pre><code class="language-vbnet">Function GetCurrentPosition(FileNum As Integer) As Long
On Error Resume Next
GetCurrentPosition = Seek(FileNum)
If Err.Number <> 0 Then
GetCurrentPosition = -1 ' Error indicator
End If
End Function</code></pre>
<h3 id="pattern-2-getprogresspercentage">Pattern 2: <code>GetProgressPercentage</code></h3>
<p>Calculate how much of file has been processed</p>
<pre><code class="language-vbnet">Function GetProgressPercentage(FileNum As Integer) As Double
Dim CurrentPos As Long
Dim FileSize As Long
CurrentPos = Seek(FileNum)
FileSize = LOF(FileNum)
If FileSize > 0 Then
GetProgressPercentage = (CurrentPos / FileSize) * 100
Else
GetProgressPercentage = 0
End If
End Function</code></pre>
<h3 id="pattern-3-isatendoffile">Pattern 3: <code>IsAtEndOfFile</code></h3>
<p>Check if at end of file using position</p>
<pre><code class="language-vbnet">Function IsAtEndOfFile(FileNum As Integer) As Boolean
IsAtEndOfFile = (Seek(FileNum) > LOF(FileNum))
End Function</code></pre>
<h3 id="pattern-4-getremainingbytes">Pattern 4: <code>GetRemainingBytes</code></h3>
<p>Calculate bytes remaining in file</p>
<pre><code class="language-vbnet">Function GetRemainingBytes(FileNum As Integer) As Long
GetRemainingBytes = LOF(FileNum) - Seek(FileNum) + 1
End Function</code></pre>
<h3 id="pattern-5-saveandrestoreposition">Pattern 5: <code>SaveAndRestorePosition</code></h3>
<p>Save position, perform operation, restore</p>
<pre><code class="language-vbnet">Sub SaveAndRestorePosition(FileNum As Integer)
Dim SavedPos As Long
SavedPos = Seek(FileNum)
' Perform operations that change position
Seek #FileNum, 1 ' Go to start
' Read header...
Seek #FileNum, SavedPos ' Restore position
End Sub</code></pre>
<h3 id="pattern-6-getcurrentrecordnumber">Pattern 6: <code>GetCurrentRecordNumber</code></h3>
<p>Get current record in Random access file</p>
<pre><code class="language-vbnet">Function GetCurrentRecordNumber(FileNum As Integer) As Long
' For Random access, Seek returns record number
GetCurrentRecordNumber = Seek(FileNum)
End Function</code></pre>
<h3 id="pattern-7-calculatebytesprocessed">Pattern 7: <code>CalculateBytesProcessed</code></h3>
<p>Track bytes processed since start</p>
<pre><code class="language-vbnet">Function CalculateBytesProcessed(FileNum As Integer, StartPos As Long) As Long
CalculateBytesProcessed = Seek(FileNum) - StartPos
End Function</code></pre>
<h3 id="pattern-8-validatefileposition">Pattern 8: <code>ValidateFilePosition</code></h3>
<p>Verify position is within file bounds</p>
<pre><code class="language-vbnet">Function ValidateFilePosition(FileNum As Integer) As Boolean
Dim CurrentPos As Long
Dim FileSize As Long
CurrentPos = Seek(FileNum)
FileSize = LOF(FileNum)
ValidateFilePosition = (CurrentPos >= 1 And CurrentPos <= FileSize + 1)
End Function</code></pre>
<h3 id="pattern-9-getpositioninfo">Pattern 9: <code>GetPositionInfo</code></h3>
<p>Get detailed position information</p>
<pre><code class="language-vbnet">Sub GetPositionInfo(FileNum As Integer, ByRef Position As Long, _
ByRef Size As Long, ByRef Remaining As Long)
Position = Seek(FileNum)
Size = LOF(FileNum)
Remaining = Size - Position + 1
End Sub</code></pre>
<h3 id="pattern-10-seektopercentage">Pattern 10: <code>SeekToPercentage</code></h3>
<p>Jump to percentage of file</p>
<pre><code class="language-vbnet">Sub SeekToPercentage(FileNum As Integer, Percentage As Double)
Dim TargetPos As Long
TargetPos = CLng((LOF(FileNum) * Percentage) / 100)
If TargetPos < 1 Then TargetPos = 1
Seek #FileNum, TargetPos
Debug.Print "Moved to position: " & Seek(FileNum)
End Sub</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="example-1-filepositiontracker-class">Example 1: <code>FilePositionTracker</code> Class</h3>
<p>Track and manage file positions with bookmarks</p>
<pre><code class="language-vbnet">' Class: FilePositionTracker
Private m_FileNum As Integer
Private m_Bookmarks As Collection
Private Sub Class_Initialize()
Set m_Bookmarks = New Collection
End Sub
Public Sub Initialize(FileNum As Integer)
m_FileNum = FileNum
End Sub
Public Function GetCurrentPosition() As Long
GetCurrentPosition = Seek(m_FileNum)
End Function
Public Sub AddBookmark(BookmarkName As String)
Dim CurrentPos As Long
CurrentPos = Seek(m_FileNum)
On Error Resume Next
m_Bookmarks.Remove BookmarkName
On Error GoTo 0
m_Bookmarks.Add CurrentPos, BookmarkName
End Sub
Public Sub GoToBookmark(BookmarkName As String)
Dim BookmarkPos As Long
On Error Resume Next
BookmarkPos = m_Bookmarks(BookmarkName)
If Err.Number = 0 Then
Seek #m_FileNum, BookmarkPos
Else
Err.Raise vbObjectError + 1001, "FilePositionTracker", _
"Bookmark not found: " & BookmarkName
End If
End Sub
Public Function GetProgress() As Double
Dim CurrentPos As Long
Dim FileSize As Long
CurrentPos = Seek(m_FileNum)
FileSize = LOF(m_FileNum)
If FileSize > 0 Then
GetProgress = (CDbl(CurrentPos) / CDbl(FileSize)) * 100
Else
GetProgress = 0
End If
End Function
Public Function GetRemainingBytes() As Long
GetRemainingBytes = LOF(m_FileNum) - Seek(m_FileNum) + 1
End Function
Public Sub ClearBookmarks()
Set m_Bookmarks = New Collection
End Sub</code></pre>
<h3 id="example-2-binaryfileparser-module">Example 2: <code>BinaryFileParser</code> Module</h3>
<p>Parse structured binary files with position tracking</p>
<pre><code class="language-vbnet">' Module: BinaryFileParser
Private Type FileHeader
Signature As String * 4
Version As Integer
RecordCount As Long
End Type
Public Function ParseFile(FileName As String) As Collection
Dim FileNum As Integer
Dim Header As FileHeader
Dim Records As New Collection
Dim i As Long
Dim StartPos As Long
FileNum = FreeFile
Open FileName For Binary As #FileNum
' Read header
StartPos = Seek(FileNum)
Debug.Print "Reading header at position: " & StartPos
Get #FileNum, , Header
Debug.Print "Header read, now at position: " & Seek(FileNum)
' Validate signature
If Header.Signature <> "DATA" Then
Close #FileNum
Err.Raise vbObjectError + 1001, "ParseFile", "Invalid file signature"
End If
' Read records
For i = 1 To Header.RecordCount
Dim RecordPos As Long
Dim RecordData As String
RecordPos = Seek(FileNum)
Debug.Print "Reading record " & i & " at position: " & RecordPos
' Read record (example: 100 byte records)
RecordData = Space$(100)
Get #FileNum, , RecordData
Records.Add RecordData
Next i
Debug.Print "Finished at position: " & Seek(FileNum)
Debug.Print "File size: " & LOF(FileNum)
Close #FileNum
Set ParseFile = Records
End Function
Public Function GetFileProgress(FileNum As Integer) As String
Dim CurrentPos As Long
Dim FileSize As Long
Dim Percentage As Double
CurrentPos = Seek(FileNum)
FileSize = LOF(FileNum)
If FileSize > 0 Then
Percentage = (CDbl(CurrentPos) / CDbl(FileSize)) * 100
Else
Percentage = 0
End If
GetFileProgress = "Position: " & CurrentPos & " of " & FileSize & _
" (" & Format(Percentage, "0.0") & "%)"
End Function</code></pre>
<h3 id="example-3-randomaccessnavigator-class">Example 3: <code>RandomAccessNavigator</code> Class</h3>
<p>Navigate through Random access file records</p>
<pre><code class="language-vbnet">' Class: RandomAccessNavigator
Private m_FileNum As Integer
Private m_RecordLength As Integer
Private m_TotalRecords As Long
Public Sub Initialize(FileName As String, RecordLength As Integer)
m_RecordLength = RecordLength
m_FileNum = FreeFile
Open FileName For Random As #m_FileNum Len = RecordLength
m_TotalRecords = LOF(m_FileNum) \ RecordLength
End Sub
Public Function GetCurrentRecord() As Long
' For Random access, Seek returns record number
GetCurrentRecord = Seek(m_FileNum)
End Function
Public Function GetTotalRecords() As Long
GetTotalRecords = m_TotalRecords
End Function
Public Function MoveNext() As Boolean
Dim CurrentRecord As Long
CurrentRecord = Seek(m_FileNum)
If CurrentRecord <= m_TotalRecords Then
' Seek statement will auto-advance after Get
MoveNext = True
Else
MoveNext = False
End If
End Function
Public Sub MovePrevious()
Dim CurrentRecord As Long
CurrentRecord = Seek(m_FileNum)
If CurrentRecord > 1 Then
Seek #m_FileNum, CurrentRecord - 1
End If
End Sub
Public Sub MoveFirst()
Seek #m_FileNum, 1
End Sub
Public Sub MoveLast()
Seek #m_FileNum, m_TotalRecords
End Sub
Public Function IsAtBeginning() As Boolean
IsAtBeginning = (Seek(m_FileNum) = 1)
End Function
Public Function IsAtEnd() As Boolean
IsAtEnd = (Seek(m_FileNum) > m_TotalRecords)
End Function
Public Function GetProgress() As Double
Dim CurrentRecord As Long
CurrentRecord = Seek(m_FileNum)
If m_TotalRecords > 0 Then
GetProgress = (CDbl(CurrentRecord - 1) / CDbl(m_TotalRecords)) * 100
Else
GetProgress = 0
End If
End Function
Public Sub Close()
Close #m_FileNum
End Sub</code></pre>
<h3 id="example-4-fileprogressmonitor-class">Example 4: <code>FileProgressMonitor</code> Class</h3>
<p>Monitor file processing progress with time estimates</p>
<pre><code class="language-vbnet">' Class: FileProgressMonitor
Private m_FileNum As Integer
Private m_StartPosition As Long
Private m_StartTime As Double
Private m_FileSize As Long
Public Sub StartMonitoring(FileNum As Integer)
m_FileNum = FileNum
m_StartPosition = Seek(FileNum)
m_StartTime = Timer
m_FileSize = LOF(FileNum)
End Sub
Public Function GetCurrentPosition() As Long
GetCurrentPosition = Seek(m_FileNum)
End Function
Public Function GetBytesProcessed() As Long
GetBytesProcessed = Seek(m_FileNum) - m_StartPosition
End Function
Public Function GetPercentComplete() As Double
Dim CurrentPos As Long
CurrentPos = Seek(m_FileNum)
If m_FileSize > 0 Then
GetPercentComplete = (CDbl(CurrentPos) / CDbl(m_FileSize)) * 100
Else
GetPercentComplete = 0
End If
End Function
Public Function GetElapsedSeconds() As Double
GetElapsedSeconds = Timer - m_StartTime
End Function
Public Function GetEstimatedTimeRemaining() As Double
Dim BytesProcessed As Long
Dim BytesRemaining As Long
Dim ElapsedTime As Double
Dim BytesPerSecond As Double
BytesProcessed = Seek(m_FileNum) - m_StartPosition
BytesRemaining = m_FileSize - Seek(m_FileNum) + 1
ElapsedTime = Timer - m_StartTime
If BytesProcessed > 0 And ElapsedTime > 0 Then
BytesPerSecond = BytesProcessed / ElapsedTime
GetEstimatedTimeRemaining = BytesRemaining / BytesPerSecond
Else
GetEstimatedTimeRemaining = 0
End If
End Function
Public Function GetProgressReport() As String
Dim Report As String
Dim PercentComplete As Double
Dim ElapsedTime As Double
Dim RemainingTime As Double
PercentComplete = GetPercentComplete()
ElapsedTime = GetElapsedSeconds()
RemainingTime = GetEstimatedTimeRemaining()
Report = "Progress: " & Format(PercentComplete, "0.0") & "%" & vbCrLf
Report = Report & "Position: " & Seek(m_FileNum) & " of " & m_FileSize & vbCrLf
Report = Report & "Elapsed: " & Format(ElapsedTime, "0.0") & "s" & vbCrLf
Report = Report & "Remaining: " & Format(RemainingTime, "0.0") & "s"
GetProgressReport = Report
End Function
Public Sub Reset()
m_StartPosition = Seek(m_FileNum)
m_StartTime = Timer
End Sub</code></pre>
<h2 id="error-handling">Error Handling</h2>
<p>The Seek function can generate the following errors:
- <strong>Error 52</strong> (Bad file name or number): File not open or invalid file number
- <strong>Error 5</strong> (Invalid procedure call): File number is invalid
Always use error handling when working with file I/O:</p>
<pre><code class="language-vbnet">On Error Resume Next
CurrentPos = Seek(FileNum)
If Err.Number <> 0 Then
MsgBox "Error getting position: " & Err.Description
End If</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li>Seek function is very fast (just reads internal file pointer)</li>
<li>No disk I/O involved (unlike reading/writing data)</li>
<li>Can be called frequently without performance impact</li>
<li>Useful for progress tracking in loops without overhead</li>
<li>Combine with LOF for efficient progress calculations</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Validate File Number</strong>: Ensure file is open before calling Seek</li>
<li><strong>Use with LOF</strong>: Combine with LOF function to calculate progress</li>
<li><strong>Error Handling</strong>: Always use error handling for file operations</li>
<li><strong>Close Files</strong>: Always close files when done to free resources</li>
<li><strong>Save Position</strong>: Store position before operations that change it</li>
<li><strong>1-Based Position</strong>: Remember positions start at 1, not 0</li>
<li><strong>Mode Awareness</strong>: Know whether function returns record# or byte position</li>
<li><strong><code>FreeFile</code> Usage</strong>: Use <code>FreeFile</code> to get available file numbers</li>
<li><strong>Progress Updates</strong>: Update UI periodically, not on every byte</li>
<li><strong>Position Validation</strong>: Verify position is within expected range</li>
</ol>
<h2 id="comparison-with-related-functions">Comparison with Related Functions</h2>
<table>
<thead>
<tr>
<th>Function/Statement</th>
<th>Purpose</th>
<th>Returns</th>
<th>Mode-Specific</th>
</tr>
</thead>
<tbody>
<tr>
<td>Seek (Function)</td>
<td>Get current position</td>
<td>Long (position)</td>
<td>Record# or byte position</td>
</tr>
<tr>
<td>Seek (Statement)</td>
<td>Set position</td>
<td>N/A (void)</td>
<td>Sets for next operation</td>
</tr>
<tr>
<td>LOF</td>
<td>Get file size</td>
<td>Long (bytes)</td>
<td>Total file size</td>
</tr>
<tr>
<td>EOF</td>
<td>Check end of file</td>
<td>Boolean</td>
<td>True if past end</td>
</tr>
<tr>
<td>Loc</td>
<td>Get current position</td>
<td>Long</td>
<td>Similar to Seek</td>
</tr>
<tr>
<td><code>FileLen</code></td>
<td>Get file length</td>
<td>Long (bytes)</td>
<td>File must be closed</td>
</tr>
</tbody>
</table>
<h2 id="platform-considerations">Platform Considerations</h2>
<ul>
<li>Available in VB6, VBA (all versions)</li>
<li>1-based positioning (unlike many other languages)</li>
<li>Maximum file size limited by Long data type (2GB)</li>
<li>For files > 2GB, position may overflow</li>
<li>Random access mode returns record numbers</li>
<li>All other modes return byte positions</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Cannot handle files larger than 2GB (Long overflow)</li>
<li>File must be open before calling Seek</li>
<li>Returns position for next operation (not last operation)</li>
<li>For Random access, assumes fixed-length records</li>
<li>No support for 64-bit file positions</li>
<li>Cannot determine position in closed files</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Seek</code> (Statement): Set the position for next read/write operation</li>
<li><code>LOF</code>: Get the length of an open file in bytes</li>
<li><code>EOF</code>: Determine if end of file has been reached</li>
<li><code>Loc</code>: Get the current read/write position (similar to Seek function)</li>
<li><code>Get</code>: Read data from file (advances position)</li>
<li><code>Put</code>: Write data to file (advances position)</li>
<li><code>Open</code>: Open file for I/O operations</li>
<li><code>Close</code>: Close an open file</li>
<li><code>FreeFile</code>: Get next available file number</li>
</ul>
</article>
<div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
<p>
<a href="index.html">← Back to File</a> |
<a href="../index.html">View all functions</a>
</p>
</div>
</main>
<footer>
<div class="container">
<p>© 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
</div>
</footer>
</body>
</html>