Solved

Read Binary input, find and replace string-a constant problem

Posted on 2004-04-24
18
1,548 Views
Last Modified: 2012-06-27
Hi guys, I've already posted a similar question and it was similar but now im stuck again and i just dont understand.

I'm reading an input file, FEENG.LOC, it can be downloaded from my shared folder

http://home.ripway.com/2004-4/104687/file/index.html

I want to search for a particular word, and replace it with another. This is not a regular ascii text file.

its starts off with a lot of jiberish and then contains words.

my code is

Private Sub Command5_Click()
     Dim find As String
     Dim replaced As String
     Dim final As String
     Dim BFreeFile As Integer
     Dim ending As String

location = App.Path & "\FEENG.LOC"
BFreeFile = FreeFile
Open location For Binary Access Read As #BFreeFile
entirefile = InputB(LOF(BFreeFile), 1)
Close #BFreeFile

find = " Y a n k e e s"
replaced = "F u z k e e s"
final = Replace(entirefile, find, replaced, 1, -1, vbBinaryCompare)
ending = StrConv(final, vbUnicode)
'''''''''''''''''''''''''

location = App.Path & "\zyyy.txt"
BFreeFile = FreeFile
If Dir(location) <> "" Then Kill (App.Path & "\zyyy.txt") 'to ensure that its written
Open location For Binary Access Write As #BFreeFile
Put #BFreeFile, , ending
Close #BFreeFile

End Sub

Any help would be greatly appreciated. Thanks.
0
Comment
Question by:fuzzyfluid
18 Comments
 
LVL 14

Expert Comment

by:ajexpert
ID: 10906541
Hi,
  can you please check the url.  It says file not found
0
 
LVL 14

Expert Comment

by:Farzad Akbarnejad
ID: 10906543
I can't download FEENG.LOC file from your website.

-FA
0
 
LVL 7

Accepted Solution

by:
OHDev2004 earned 400 total points
ID: 10906551
hey man i've already answered it ,, here it is :)

Private Sub Form_Load()
'its now written in the shape of a function so you can call
'it and specify different parameters if you like
'the parameters are :
'TxtSource --> the source file to read from
'TxtDestination --> the Destination file to write to
'TxtTxtFind--> the text to be searched for
'TxtReplace--> the text to be replaced
Call MyFunction(App.Path & "\Zoobhrt.txt", App.Path & "\zyyy.txt", "N e w   Y o r k   Y a n k e e s", "N e w   Y o r k   T H I S W O R K S")

End Sub


Private Function MyFunction(TxtSource As String, TxtDestination As String, TxtTxtFind As String, TxtReplace As String)
 Dim TmpBuffer As String 'the buffer which will hold the input/output
 Dim BFreeFile As Integer 'this will hold the free file port

BFreeFile = FreeFile
Open TxtSource For Binary Access Read As #BFreeFile
TmpBuffer = InputB(2, BFreeFile) 'To Ignore the first character "?"
TmpBuffer = InputB(LOF(BFreeFile), BFreeFile) 'put all the contents to the TmpBuffer
Close #BFreeFile

TmpBuffer = Replace(TmpBuffer, TxtFind, TxtReplace, 1, -1, vbBinaryCompare) ' do the search/replace trick

BFreeFile = FreeFile
If Dir(TxtDestination) <> "" Then Kill (TxtDestination) 'to ensure that its written
Open TxtDestination For Binary Access Write As #BFreeFile
Put #BFreeFile, , TmpBuffer 'Output the file
Close #BFreeFile

End Function
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 7

Expert Comment

by:OHDev2004
ID: 10906561
check the URL
hey fuzzyfluid what's the problem with this one this time :)
what's the error ,, did you try the above code ?
0
 

Author Comment

by:fuzzyfluid
ID: 10906566
if the link doesn't work, i'll post it up on the permanent server tomorrow. Thanks for all the quick responses. I was hoping i could get this done tonight but i guess it'll wait till tomorrow. OHDEV, i'll try your code in the morning. its 4 AM in NYC. Thanks.
0
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10906567
lol :)
0
 

Author Comment

by:fuzzyfluid
ID: 10908063
ok, heres the input file, FEENG.LOC

it's compressed so that bandwith doesnt get close to maxing out. The input file is original 400k.

www.kawaii-girls-want-to-s3x0r.us/revolutionary/FEENG.rar


any help would be appreciated.
0
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10909156
what's the problem this time,, :)

why does the file include weird characters at the beginning ???
did you output that file from vb or what ??
do  you want to do the same as the other one ?? or what  .....
OHDev
0
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10909254
it seams that you've put that jiberish at the beginning right ?
ok this time you try to find " Y a n k e e s" and replace it with "F u z k e e s"
then save it ,, am i right ?
if so the above function should work ,, i'll test it and let you know
0
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10909536
this really makes me crazy ,,
the jiberish at the beginning of the file makes it impossible to search and replace,,
donnow why ,, and when i try to truncate the jiberish; all the spaces in the words are removed ,, do you need that jiberish  at the beginning of the file ,, did you put it on purpose ? ,,,
OHDev
0
 

Author Comment

by:fuzzyfluid
ID: 10909979
no thats a totally different file. It's as is. The jibberish needs to be there. I tried out ur function on this file and it doesnt work. It removes the jibberish and puts ? marks.

I appreciate your hardwork and time into this. if you figure it out let me know.

1)the jibberish at the beginning is needed for the file to be interpreted properly.
2)i dont know what the jibberish is for, but if it is altered in any shape or form, it will not be read properly by the software.
0
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10910008
what software do you mean ?
i managed to output the file in the right order ,, but still cant search and replace
0
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10910015
you're welcome man :P
u can talk to me on msn msngr if you like ,,

0
 

Author Comment

by:fuzzyfluid
ID: 10910026
i figured it out.lol. since it's binary and the file isn't ascii. Those aren't spaces in between the letters.

Don't search "Y A N K E E S". Instead find

"Y" & vbnullchar & "A" & vbnullchar & "N" & vbnullchar & "K" & vbnullchar & "E" & vbnullchar & "E" & vbnullchar & "S"

and replace with
"F" & vbnullchar & "u" & vbnullchar & "z" & vbnullchar & "K" & vbnullchar & "E" & vbnullchar & "E" & vbnullchar & "S"


i'll award your the points because of the time you spent and the need function you posted that is helping me out. Thanks again ohdev.
0
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10910030
you know you really are very smart ,, really i mean it
its really a smart one !!
thanks but you know youre the one who must be awarded with those points ,, lol
0
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10910041
i will provide you with the latest functionally function now :) lol
if you like
0
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10910058
here is the final code man ,, hope it fullfills ur needs :)
 
=============================================

Private Sub Form_Load()
'its now written in the shape of a function so you can call
'it and specify different parameters if you like
'the parameters are :
'TxtSource --> the source file to read from
'TxtDestination --> the Destination file to write to
'TxtTxtFind--> the text to be searched for
'TxtReplace--> the text to be replaced
'Call MyFunction(App.Path & "\Zoobhrt.txt", App.Path & "\zyyy.txt", "N e w   Y o r k   Y a n k e e s", "N e w   Y o r k   T H I S W O R K S")
'MsgBox FileLen(App.Path & "\11.txt")
Call MyFunction(App.Path & "\FEENG.LOC", App.Path & "\FEENG2.txt", "Yankees", "Fuzkees")

End Sub


Private Function MyFunction(TxtSource As String, TxtDestination As String, TxtFind As String, TxtReplace As String)
 Dim TmpBuffer As String 'the buffer which will hold the input/output
 Dim BFreeFile As Integer 'this will hold the free file port

TxtFind = DecodeText2Bin(TxtFind)
TxtReplace = DecodeText2Bin(TxtReplace)

BFreeFile = FreeFile
Open TxtSource For Binary Access Read As #BFreeFile
'TmpBuffer = Input(1, BFreeFile) 'To Ignore the first character "?"
TmpBuffer = Input(LOF(BFreeFile), BFreeFile)  'put all the contents to the TmpBuffer
Close #BFreeFile


TmpBuffer = Replace(TmpBuffer, TxtFind, TxtReplace, 1, -1, vbBinaryCompare) ' do the search/replace trick
BFreeFile = FreeFile
If Dir(TxtDestination) <> "" Then Kill (TxtDestination) 'to ensure that its written
Open TxtDestination For Binary Access Write As #BFreeFile
Put #BFreeFile, , TmpBuffer 'Output the file
Close #BFreeFile
Unload Me
End Function



Private Function DecodeText2Bin(DTxt As String) As String
Dim x As Integer
Dim Tmp As String
For x = 1 To Len(DTxt) - 1
Tmp = Tmp & Mid(DTxt, x, 1) & vbNullChar
Next x
DecodeText2Bin = Tmp
End Function

0
 

Author Comment

by:fuzzyfluid
ID: 10910139
Thanks a billion Ohdev. And it's been a pleasure.

fuzZ
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

829 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