Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2006, 03:46 PM
Authorized User
 
Join Date: Jul 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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 !


 
Old November 16th, 2006, 04:30 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old November 20th, 2006, 05:37 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

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
 
Old November 20th, 2006, 06:54 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default


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)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading txt file umeshtheone SQL Server 2000 9 May 15th, 2007 12:43 AM
Reading a txt file AlphaX Beginning VB 6 2 February 20th, 2006 11:22 PM
Reading line by line from a .txt file x_ray VB.NET 2002/2003 Basics 5 February 10th, 2006 01:55 PM
multiple reading from db dannyphw ASP.NET 1.0 and 1.1 Basics 2 October 23rd, 2004 08:24 AM
multiple reading from db dannyphw Classic ASP Databases 2 October 21st, 2004 11:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.