Solved

Hopefully Easy VB6 read & write data to a file

Posted on 2004-08-09
5
7,013 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

895 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

13 Experts available now in Live!

Get 1:1 Help Now