p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 16th, 2006, 03:46 PM
Authorized User
Points: 79, Level: 1
Points: 79, Level: 1 Points: 79, Level: 1 Points: 79, Level: 1
Activity: 2%
Activity: 2% Activity: 2% Activity: 2%
 
Join Date: Jul 2006
Location: , , .
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 !


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old November 16th, 2006, 04:30 PM
Friend of Wrox
Points: 1,254, Level: 14
Points: 1,254, Level: 14 Points: 1,254, Level: 14 Points: 1,254, Level: 14
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Alameda, ca, USA.
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old November 20th, 2006, 05:37 PM
Friend of Wrox
Points: 5,269, Level: 30
Points: 5,269, Level: 30 Points: 5,269, Level: 30 Points: 5,269, Level: 30
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 1 Time in 1 Post
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old November 20th, 2006, 06:54 PM
Friend of Wrox
Points: 1,254, Level: 14
Points: 1,254, Level: 14 Points: 1,254, Level: 14 Points: 1,254, Level: 14
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Alameda, ca, USA.
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading txt file umeshtheone SQL Server 2000 9 May 15th, 2007 01: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 09:24 AM
multiple reading from db dannyphw Classic ASP Databases 2 October 21st, 2004 12:07 PM



All times are GMT -4. The time now is 07:22 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc