Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Hopefully Easy VB6 read & write data to a file

Posted on 2004-08-09
5
Medium Priority
?
7,026 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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

604 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