Solved

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

Posted on 2004-04-24
18
1,545 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MsgBox 2 37
to transfer string from C lanaguage to VBA 4 44
Adding to a VBA? 6 48
Spell Check in VB6 13 43
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now