Solved

Hopefully Easy VB6 read & write data to a file

Posted on 2004-08-09
5
7,019 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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 86

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

740 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