Getting file name in a loop
Getting file name in a loop
Getting file name in a loop
Getting file name in a loop
Getting file name in a loop
Getting file name in a loop Getting file name in a loop Getting file name in a loop Getting file name in a loop Getting file name in a loop Getting file name in a loop Getting file name in a loop Getting file name in a loop
Getting file name in a loop Getting file name in a loop
Getting file name in a loop
Go Back  Xtreme Visual Basic Talk > > > Getting file name in a loop


Reply
 
Thread Tools Display Modes
  #1  
Old 03-18-2012, 03:56 PM
supercrewed supercrewed is offline
Centurion
 
Join Date: Feb 2004
Posts: 152
Default Getting file name in a loop


I just started working with vb 2010 express, and i can't seem to get this to work, i've been searching for a solution but to no avail!

As you can see below, what i'm doing is looping through a directory, and opening, only the files that start with the number "6", but ican't figure out, how to read the file names. This code runs perfectly in VB6, and i can get it to run invb 2008, from which i've been trying to convert this, Thanks in advance...


Do While Microsoft.VisualBasic.Strings.Left(filename, 1) = "6"
FileOpen(1, My.Application.Info.DirectoryPath & "\Bin\" & filename, OpenMode.Input)
'********************************************************************* ****
If CDbl(Left(filename, 1)) = 6 And Left(filename, 5) <> Left(FileName_Last, 5) Then
TOOLS()
R_Buff = Replace(InputString(1, LOF(1)), "M03", RGH_TOOL_ID & " M06" & vbCrLf & "M03")
Else
R_Buff = Replace(InputString(1, LOF(1)), "G28 G91 Z0.00 M21" & vbCrLf & "M01" & vbCrLf, "")
End If
ROUGH_CYCLE = ROUGH_CYCLE & TrimAll(R_Buff) & vbCrLf
FileClose(1)
FileName_Last = filename
filename = Dir()

Loop
FileClose(1)
End Sub
Reply With Quote
  #2  
Old 03-18-2012, 05:24 PM
PlausiblyDamp's Avatar
PlausiblyDampGetting file name in a loop PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

If you are going to make the move to .Net then I would strongly suggest you make the move to using the .Net syntax and methods; although most of the VB6 mechanisms will work (albeit via a compatibility layer for some) it can often lead to convoluted syntax and hard to track down bugs.

The main issue I can see however is that the loop is checking if a filename begins with a number 6 but if Dir ever returns a filename that doesn't begin with a 6 then it will exit the loop automatically.

A more .Net version might be something like
Code:
     Dim filenames() As String
        filenames = System.IO.Directory.GetFiles("6*.*")

        For Each file As String In filenames
            FileOpen(1, My.Application.Info.DirectoryPath & "\Bin\" & filename(), OpenMode.Input)
            '********************************************************************* ****
            If CDbl(Left(filename, 1)) = 6 And Left(filename, 5) <> Left(FileName_Last, 5) Then
                TOOLS()
                R_Buff = Replace(InputString(1, LOF(1)), "M03", RGH_TOOL_ID & " M06" & vbCrLf & "M03")
            Else
                R_Buff = Replace(InputString(1, LOF(1)), "G28 G91 Z0.00 M21" & vbCrLf & "M01" & vbCrLf, "")
            End If
            ROUGH_CYCLE = ROUGH_CYCLE & TrimAll(R_Buff) & vbCrLf
            FileClose(1)
            FileName_Last = filename()
            FileClose(1)
        Next
although this is still a bit too VB6 heavy for my liking the various calls to Left, Replace and TrimAll could be replaced with the .Substring, .Replace and .Trim methods on a string variable.
The File handling could be done via a StreamReader and calls to it's .ReadLine method rather than using the legacy VB6 file open / inputstring methods as well.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Getting file name in a loop
Getting file name in a loop
Getting file name in a loop Getting file name in a loop
Getting file name in a loop
Getting file name in a loop
Getting file name in a loop Getting file name in a loop Getting file name in a loop Getting file name in a loop Getting file name in a loop Getting file name in a loop Getting file name in a loop
Getting file name in a loop
Getting file name in a loop
 
Getting file name in a loop
Getting file name in a loop
 
-->