Wrox Programmer Forums
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 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 April 1st, 2005, 08:43 AM
Registered User
Join Date: Apr 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default statement replace

i have a little problem,

i have a txt file and in that file i want replace spaces into zeros...

but can i say where to begin with replacing and where to stop?

this is my code

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim FileName, Find, ReplaceWith, FileContents, dFileContents
        Find = " "
        ReplaceWith = "000"
        FileName = "\\srvbo\temp\KSTAYHOSP.TXT"

        'Read source text file
        FileContents = GetFile(FileName)

        'replace all string In the source file
        dFileContents = Replace(FileContents, Find, ReplaceWith, 1, -1, 1)

        'Compare source And result
        If dFileContents <> FileContents Then
            'write result If different
            WriteFile(FileName, dFileContents)

            MsgBox("Replace done.")
            If Len(ReplaceWith) <> Len(Find) Then 'Can we count n of replacements?
                MsgBox((Len(dFileContents) - Len(FileContents)) / (Len(ReplaceWith) - Len(Find)) & " replacements.")
            End If
            MsgBox("Searched string Not In the source file")
        End If
    End Sub

    'Read text file
    Function GetFile(ByVal FileName)
        If FileName <> "" Then
            Dim FS, FileStream
            FS = CreateObject("Scripting.FileSystemObject")
            On Error Resume Next
            FileStream = FS.OpenTextFile(FileName)
            GetFile = FileStream.ReadAll
        End If
    End Function

    'Write string As a text file.
    Function WriteFile(ByVal FileName, ByVal Contents)
        Dim OutStream, FS

        On Error Resume Next
        FS = CreateObject("Scripting.FileSystemObject")
        OutStream = FS.OpenTextFile(FileName, 2, True)
    End Function

Old April 1st, 2005, 12:05 PM
Friend of Wrox
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts


Please don't post the same question in to fora. Post it in one, (as you have in http://p2p.wrox.com/topic.asp?TOPIC_ID=28688 ), then reference it in other fora. That way, all info pertaining to it will be in one place.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace In c# jayanth_nadig SQL Language 2 July 3rd, 2006 03:25 AM
replace "." with "/" thelos Excel VBA 1 September 14th, 2005 10:47 AM
Replace crmpicco VB How-To 15 May 20th, 2005 01:34 PM
replace statement nebby VB.NET 2002/2003 Basics 1 April 1st, 2005 11:58 AM
replace() Missy_K Javascript 1 January 16th, 2004 11:17 AM

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