Solved

Hopefully Easy VB6 read & write data to a file

Posted on 2004-08-09
5
7,012 Views
Last Modified: 2008-02-01
Good Day All,

I can't believe this is giving me problems.  Every day, a file is deposited on our web server.
Unfortunately, the file that is deposited is exported from an application before it is dumped on
our web server.  As a result, some nasty characters work it's way into the file.

My dilemma, I'm writing a small VB app that takes this file reads it one character at a time,
filters the character (example:  if asc(character) = 9 then disregard ' No tabs wanted...) to
see if it's between A-Z, and if so, write the character out to a new file.  Fortunately for me,
the file will never be over 2K.

But I can't seem to find code out there that does what I want.

PSUEDOCODE----------------------------
open input file
open output file
while not eof(input file)
    read a character from the input file
    if the character <> ASC(9) then              ' do not want TABS in output file
        write character to the output file
    end if
wend
close input file
close output file

PSUEDOCODE----------------------------

Any help will be appreciated.  If needed, I'll add points if the solution warrants it.

TIA,

coson
0
Comment
Question by:coson
5 Comments
 
LVL 1

Expert Comment

by:Shatai
ID: 11759620
It differs slightly depending on whether you're using VB6 or VB.NET, but that's just syntax.

For your pseudocode example, here's the .NET version.

        Dim strInputFileName As String = "C:\input.txt"
        Dim strOutputFileName As String = "C:\output.txt"
        Dim oneChar As String

        FileOpen(1, strInputFileName, OpenMode.Binary, OpenAccess.Read)
        FileOpen(2, strOutputFileName, OpenMode.Output)
        While Not EOF(1)
            oneChar = InputString(1, 1)
            If Asc(oneChar) <> 9 Then
                Print(2, oneChar)
            End If
        End While
        FileClose(1)
        FileClose(2)

And here's the VB6 version (I don't have a copy of VB6 on this computer, so I can't test this version.)

        Dim strInputFileName As String
        Dim strOutputFileName As String
        Dim oneLine As String
        Dim newLine As String

        strInputFileName = "C:\input.txt"
        strOutputFileName = "C:\output.txt"

        Open strInputFileName For Input As #1
        Open strOutputFileName For Output As #2
        Do While Not EOF(1)
            newLine = ""
            Line Input #1, oneLine
            for i = 1 to len(oneLine)
                If Asc(Mid(oneLine, i, 1)) <> 9 Then
                    newLine = newLine & Mid(oneLine, i, 1)
                End If
            next i
            Print #2, newLine
        Loop
        Close #1
        Close #2

Hope that helps.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 100 total points
ID: 11759632
Try this out:

Private Sub Command1_Click()
    filterOutTabs "c:\test.txt", "c:\test2.txt"
End Sub

Private Sub filterOutTabs(ByVal fileName As String, ByVal outputFileName As String)
    Dim entireFile As String
   
    If Dir(fileName) <> "" Then
        Open fileName For Binary As #1
        entireFile = Space(LOF(1))
        Get #1, , entireFile
        Close #1
               
        If Dir(outputFileName) <> "" Then
            Kill outputFileName
        End If
       
        entireFile = Replace(entireFile, vbTab, "")
        Open outputFileName For Binary As #1
        Put #1, , entireFile
        Close #1
    Else
        MsgBox fileName, vbInformation, "File not found"
    End If
End Sub

Regards,

Idle_Mind
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11761486
'This code will clean all character below ascii 32 and above ascii 122

Private Sub Command1_Click()
Dim myLine As String, posChar As Integer, newLine As String
Open "sourceFilePath&name" For Input As #1
Open "targetfilePath&name" For Output As #2
    Do Until EOF(1)
        Line Input #1, myLine
            For posChar = 0 To 31
                newLine = Replace(myLine, Chr(posChar), "")
            Next posChar
            For posChar = 123 To 255
                newLine = Replace(newLine, Chr(posChar), "")
            Next posChar
            Print #2, newLine
    Loop
Close #2
Close #1
End Sub

S
0
 

Author Comment

by:coson
ID: 11764809
Idle_Mind,

While all the solutions worked, I'm going to give you the points.  But I'm intrigued by your use of:

entireFile = Space(LOF(1))

Why the use of the Space function?

coson
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11765461
When you open a file in Binary mode and use a statement like

    Get #1, , entireFile

the number of bytes read is determined by how big entireFile is.  By making it as large as the file with

    entireFile = Space(LOF(1))

we read the whole contents of the file into entireFile.

The Space() function creates a string of x spaces so

    Dim s As String
    s = Space(5)

would result in a string of five spaces like "     ".

Regards,

Idle_Mind
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now