Hi, all

Does somebody know how to open text file with half of its name using vb6. example i have 2 files in C:\,
they are :
1. Myfile10001.txt
2. Myfile20001.txt
I want to read file no.2 with 7 characters first (Myfile2xxxx.txt), and ignore next characters.

my script is :
Option Explicit
dim file1, file2, astrx
astrx = "*"
file1 ="c:\Myfile10001.txt"
file2 ="c:\Myfile2" & astrx & ".txt"

'this is run
Sub OpenTextFile1
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs1, f1
Set fs1 = CreateObject("Scripting.FileSystemObject")
Set f1 = fs1.OpenTextFile(file1, ForReading,TristateFalse)
f1.Close
End Sub

'this is error
Sub OpenTextFile2
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs2, f2
Set fs2 = CreateObject("Scripting.FileSystemObject")
Set f2 = fs2.OpenTextFile(file2, ForReading,TristateFalse)
f2.Close
End Sub

Dani AI

Generated

Short answer: use a pattern to find the matching filename, then open the file using its full path. 's Dir idea is fine for finding matches and 's Left-check can be used to verify a prefix, but two gotchas should be highlighted: Dir usually returns just the filename (not the folder), and FSO.OpenTextFile expects a full path (or the current working folder must match). That explains why the code in Post #6 appeared to “work” only when the current folder matched C:.

A robust, easy-to-read alternative is to use the FileSystemObject Files collection and the VB Like operator to find the first match, then open it via the file's Path property. The example below is VB6-style and keeps declarations explicit (Option Explicit):

Dim fs As Object
Dim f As Object
Set fs = CreateObject("Scripting.FileSystemObject")

For Each f In fs.GetFolder("C:\").Files
    If f.Name Like "Myfile2*.txt" Then
        Dim ts As Object
        Set ts = fs.OpenTextFile(f.Path, 1, False, 0)  '1 = ForReading, 0 = TristateFalse
        Debug.Print ts.ReadAll
        ts.Close
        Exit For   'remove this line to process all matches
    End If
Next

Notes and troubleshooting:

  • Declare variables with types when using Option Explicit (Dim fileName As String, etc.).
  • OpenTextFile parameters: (filename, iomode, create, format). Use False for create and Tristate constants (0) for format when needed.
  • For case-insensitive matching, compare LCase(f.Name) against a lowercased pattern or use Option Compare Text.
  • If multiple files match, either loop through all matches or use Exit For to take the first one.
  • If Dir must be used (as in ’s snippet), remember to prepend the folder path to Dir’s return before calling OpenTextFile (folder & "\" & DirResult).

Recommended Answers

All 7 Replies

If i understand correctly... This is loop with DIR function thru all files in folder names MyFile2*.txt (eg20001, 20002, 202 etc.)

MyFile = Dir("e:\MyFile2*.txt")
While MyFile <> ""
    Debug.Print MyFile 'Or do whatever you like with file... MyFile variable contain file full name
    MyFile = Dir() 'read new file if it exist... if not loop will end
Wend

You need to read into the text before the "*", using the left function with how many characters you need to read into.

Thanks for replay.
I mean like windows search file or directory function, where if i want to find file Myfile20001.txt just enter keyword Myfile2*.txt so 0001 char is ignore.

I do not understand, you need to search while hdd for file and than open it or just single folder? Solution up is for single (known) folder and for partial filename. You can use any name with wildchars like DIR("e:\MyFile2???.txt") or DIR(e:\MyFile2*.txt")

Thanks monarchmk,

My code is running now. I change my code to :

Option Explicit
dim file1, file2, astrx
astrx = "c:\Myfile2*.txt"
file1 ="c:\Myfile10001.txt"
file2 =Dir(astrx)

Sub OpenTextFile2
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs2, f2
Set fs2 = CreateObject("Scripting.FileSystemObject")
Set f2 = fs2.OpenTextFile(file2, ForReading,TristateFalse)
f2.Close
End Sub

glad to help :)

otomatis, if this thread already answered then please mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.