|
|
 |
| Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET). |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning VB 6 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

November 16th, 2006, 03:46 PM
|
|
Authorized User
|
|
Join Date: Jul 2006
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Reading multiple txt from a specified dir
Hi there guys, This is my problem i have a bunch of .txt in a specified dir, and i want to read everyone of them(not at the same time) to verify they have the proper layout. I want to implement a loop to read them, but i don't know how to.
1.-How can i know the number of files let's say .txt in a specified dir.
2.-How can i get the filename in order to open it and start working with its contents.
Thanks a lot in advance !
|

November 16th, 2006, 04:30 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Alameda, ca, USA.
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
code not tested:
dim filename as string
filename = Dir(myFolder * "\*.txt") ''' find the first file
do
if len(filename) = 0 then exit do
''' do something with filename
filename= Dir '' find the next file
loop
To read the file, you can use the standard Freefile, Open, Line Input, or the FileSystemObject
Full documentation here:
http://msdn.microsoft.com/library/de...Processing.asp
|

November 20th, 2006, 05:37 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 1 Time in 1 Post
|
|
Hate to be a nit-picker, but ending the loop is good . . .
Code:
Dim filename As String
' Get the first file name
filename = Dir(myFolder & "\*.txt")
' If there was no such file, filename will be "", and you'll never enter the loop.
Do Until filename = ""
' Oops. Didn’t see the fol. line. Apologies.
' But decided to post anyway because I like the Do Until format so well.
' Makes more terse code IMHO, and puts the behavior of the loop in the
' same statement with the inception of the loop, for readability/ease of
' maintenance.
' if len(filename) = 0 then exit do
''' do something with filename
filename = Dir ' Dir, with no arguments, returns the next file that fits the
Loop ' arguments of the initial Dir() statement.
So, without all the comments:
Code:
Dim filename As String
filename = Dir(myFolder & "\*.txt")
Do Until filename = ""
''' do something with filename[/green]
filename = Dir
Loop
|

November 20th, 2006, 06:54 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Alameda, ca, USA.
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
reason why I did not use "do until" is that... well in VB I use only two loops: the for..next (with a counter) and the do...loop (when I do not know the number of loops)
I agree that "do until" it is a better choice in that case, but for me VB has too many loops... :)
I only use the do...loop because it is the more flexible. "do until" is ok 'until' (pun intended) the logic of the program stays the same. if the loop must be exited before the 'until', an instruction like "if...then exit do" is needed.
With "do...loop" I can change the loop logic by adding/removing/moving the "exit do" condition as I wish
well, I was not that clear, did I...
anyway, this is just syntax preference
PS: I prefer 'if len(s) = 0' than 'if s = ""' because it is an integer comparison instead of a string comparison (not big deal anyway)
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |