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

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.