Link to home
Start Free TrialLog in
Avatar of KingSencat
KingSencat

asked on

How do i Remove non-recognizable characters from a .txt file using Visual Basic 6.0 ?

I have a .txt file which contains some non-recognizable characters like ? Å etc.. how do i clean the .txt file from those characters and remain the normal one?
Avatar of CSecurity
CSecurity
Flag of Iran, Islamic Republic of image

You can say if it's ascii code was more than 127 or lower than 31 remove it.

Like this:


Dim buf As String
buf = Space(FileLen("yourfile"))
Open "yourfile" For Binary As #1
Get #1, , buf
Close #1
 
For i = 0 To 31
buf = Replace(buf, Chr(i), "")
buf = Replace(buf, Chr(i), "")
Next i
 
For i = 128 To 255
buf = Replace(buf, Chr(i), "")
buf = Replace(buf, Chr(i), "")
Next i

Open in new window

after closing the file, buffer contains all of your files text.
now you should use Replace function this way:
buffer will contain all digit characters and letters (A-Z and a-z)


Private Sub Form_Load()
Dim buffer As String, buffer2 As String
buffer = ""
Open "c:\test.txt" For Input As #1
Do While Not EOF(1)
Input #1, buffer2
buffer = buffer & buffer2
Loop
Close #1
'DO REPLACEMENTS HERE
for i = 1 to len(buffer)
if not((asc(mid(buffer,i,1)) > 47 and asc(mid(buffer,i,1)) < 58) or (asc(mid(buffer,i,1)) > 64 and asc(mid(buffer,i,1)) < 91) or (asc(mid(buffer,i,1)) > 96 and asc(mid(buffer,i,1)) < 123)) then
buffer = Replace(buffer, mid(buffer,i,1), "", 1, , vbTextCompare)
end if
next i
End Sub

Open in new window

Avatar of KingSencat
KingSencat

ASKER

I get an error on

Input #1, buffer2

Err msg, Input past end of file

Can someone help me please?
Use my first example, forget second one
i just try it , nothing seems to be happened. The .txt file still contains the following :

Y!÷äÝs   P!÷äÕY   6!÷äÎi   ,!÷ä¸   !÷ä²s   !÷ä©y   !÷ä¦$   

Add this function to your code and call it like this:
RemoveChars("C:\Yourfile.txt")
Private Sub RemoveChars(FileName as String)
Dim buf As String
buf = Space(FileLen(FileName))
Open FileName For Binary As #1
Get #1, , buf
Close #1
 
For i = 0 To 31
buf = Replace(buf, Chr(i), "")
buf = Replace(buf, Chr(i), "")
Next i
 
For i = 128 To 255
buf = Replace(buf, Chr(i), "")
buf = Replace(buf, Chr(i), "")
Next i
 
Kill FileName
Open FileName for binary as#1
Put#1, , buf
close#1
End Sub

Open in new window

Before trying don't forget to have backup of your file, use it at your own risk
Its work but its changes the arhitecture of my .txt, something that i dont want :(

Before:

   Y!÷äÝs   P!÷äÕY   6!÷äÎi   ,!÷ä¸   !÷ä²s   !÷ä©y   !÷ä¦$   t = " 0 " >
       < S e a t s > 
             < S e a t   n u m = " 1 "   a l i a s = " S a m p d o r i a 7 6 "   u n i c o d e a l i a s = " U w B h A G 0 A c A B k A G 8 A c g B p A G E A N w A 2 A A = = "   b a l a n c e = " 4 0 9 3 . 0 0 "   e n d b a l a n c e = " 4 7 9 8 . 0 0 " / > 
         
Now:

 Ys   PY   6i   ,   s   y   $   t = " 0 " >        < S e a t s >              < S e a t   n u m = " 1 "   a l i a s = " S a m p d o r i a 7 6 " 

Any idea how to fix it ?

And yes i always keep backups ;)
It's last possible thing... You need to find any char you don't want to removed... Customize it as you want... It removes all unneeded chars
Actually my problem is this:

I dont want the following symbol to appear on my .txt file because i cant import the .txt file it to my project, this is the main reason why i want to do that.

Look at this screenshot
http://www.picoodle.com/view.php?img=/3/8/12/f_heressssm_334c34c.png&srv=img29
Find ascii code of that charachter and remove it as I did with Replace function
What you mean?
My code removes a lot of chars, if you still see some chars, find their ascii code and add replace to them like

Replace(Buf, chr(100), "")

If ascii code of that character is 100.
but i told i can replace it succesfull the problem that all text of the .txt file its everything on 1 line, its changes the .txt architecture.
Send me your txt file
I attach it.

What i am trying to do is to import this .txt file to a listbox in my visual basic project, but i cant get it work to show line per line on the listbox.

 Dim Line As String

    Open ("c:\file.txt") For Input As #1
    Do While Not EOF(1)
        Line Input #1, Line
        List1.AddItem Line
        Loop
    Close #1
file.txt
note: the original file was .dat, but because i cant upload here .dat i rename to .txt
No idea. My code removes all non printable chars.

You file is SQLLite file, try reading it with it's driver.
Your code removes all non printable chars but he print everything in 1 line.
So use this:

Private Sub RemoveChars(FileName as String)
Dim buf As String
buf = Space(FileLen(FileName))
Open FileName For Binary As #1
Get #1, , buf
Close #1
 
For i = 0 To 31
if i <> 13 then
buf = Replace(buf, Chr(i), "")
buf = Replace(buf, Chr(i), "")
end if
Next i
 
For i = 128 To 255
buf = Replace(buf, Chr(i), "")
buf = Replace(buf, Chr(i), "")
Next i
 
Kill FileName
Open FileName for binary as#1
Put#1, , buf
close#1
End Sub

Open in new window

this code doesnt remove all the characters.
ASKER CERTIFIED SOLUTION
Avatar of CSecurity
CSecurity
Flag of Iran, Islamic Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial