Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 17th, 2007, 01:14 PM
Registered User
 
Join Date: Aug 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default ReplaceChar Script

Hello!

I am using this function:

Public Sub ReplaceChar2(ByVal File As String, CharToReplace As String, ReplaceWith As String)

    'If Len(CharToReplace) <> 1 Or Len(ReplaceWith) <> 1 Then Err.Raise -1, , "Invalid argument"
    Dim n As Integer, b As Byte, l As Long, i As Long
    Dim repl As Byte, replwith As Byte
    repl = AscW(CharToReplace)
    replwith = AscW(ReplaceWith)
    n = FreeFile
   ' fTextWidth (File)
    l = FileLen(File)

    Open File For Binary As n

     For i = 1 To l

        Get n, , b

        If b = repl Then Put n, i, replwith 'write the replacement character

        Next

    Close n


End Sub


to replace all instances of the character ^ with *. However, i need to modify this script so that it will replace all instances of ~ with ~ + vbcrlf.

Can anyone help me with this? the problem is that this script is only working for single character replacement. I need to replace a single character with two characters.

Any help is appreciated!!! Thanks!

 
Old August 17th, 2007, 01:24 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there.. this function receive strings, so what happend is you pass the 2 chars you need to the function??
put will not save the 2 char???
can you just get the len of the string and build an array of chars to save?? (just an easy version of this)...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old August 17th, 2007, 04:50 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

If I understand right, you are running into difficulty because the original replaces a location the same size as what was read (1 character), but what you need to do puts more characters than what was read.

I suggest you open 2 files, one to read from, and one to hold the result. As you read the input file, either put what was read into the output file, or put your replacement.

You do have a problem with scaleability. Perhaps you should pass a 2 dimension array to the function, with dimension 0 being the searched–for char, and dimension 1 being the replacement. For each character read from the file, test it against all the dimension 0's. If a match is found, output dimension 1 (which if it were "" would effectively delete the character found). If no matches are found, send the read char to the output.

When you are done, delete the input file and rename the output file as what the input file had been (File$).

With this process, if you need to do a third type of replacement later (or only need to do 1, instead of 2), you don't need to re-write your function; all you would need to do is add (or remove) an entry to the array.

Seems this would be easier with the Scripting Runtime library’s FileSystemObject...

Does that work for you?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Script Help :) zere0 VBScript 6 December 7th, 2004 02:23 PM
looking for script 7474information Classic ASP Basics 4 October 22nd, 2004 02:48 AM
Call and run CGI script from a PHP script ... how? dbruins BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 June 10th, 2003 03:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.