Solved

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

Posted on 2004-04-24
18
1,547 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Basic Excel Formatting error 4 108
Reading the Contents of a Directory In Access VBA 5 71
Access query that references subform 5 47
Help me. 3 57
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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

831 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