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?
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)
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
ASKER
I get an error on
Input #1, buffer2
Err msg, Input past end of file
Input #1, buffer2
Err msg, Input past end of file
ASKER
Can someone help me please?
Use my first example, forget second one
ASKER
i just try it , nothing seems to be happened. The .txt file still contains the following :
Y!÷äÝs P!÷äÕY 6!÷äÎi ,!÷ä¸ !÷ä²s !÷ä©y !÷ä¦$
Y!÷äÝs P!÷äÕY 6!÷äÎi ,!÷ä¸ !÷ä²s !÷ä©y !÷ä¦$
Add this function to your code and call it like this:
RemoveChars("C:\Yourfile.t
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
Before trying don't forget to have backup of your file, use it at your own risk
ASKER
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 ?
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 ?
ASKER
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
ASKER
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
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
ASKER
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.
Replace(Buf, chr(100), "")
If ascii code of that character is 100.
ASKER
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
ASKER
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
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
ASKER
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.
You file is SQLLite file, try reading it with it's driver.
ASKER
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
ASKER
this code doesnt remove all the characters.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Like this:
Open in new window