simple find and replace in text file
simple find and replace in text file
simple find and replace in text file
simple find and replace in text file
simple find and replace in text file
simple find and replace in text file simple find and replace in text file simple find and replace in text file simple find and replace in text file simple find and replace in text file simple find and replace in text file simple find and replace in text file simple find and replace in text file
simple find and replace in text file simple find and replace in text file
simple find and replace in text file
Go Back  Xtreme Visual Basic Talk > > > simple find and replace in text file


Reply
 
Thread Tools Display Modes
  #1  
Old 06-21-2017, 09:19 AM
starmanMike starmanMike is offline
Junior Contributor
 
Join Date: Oct 2005
Location: near Norwich, UK
Posts: 286
Default simple find and replace in text file


Hi, This sounds so simple, but I just can't get it to work!
All I need to do is go through a .txt file which is delimited by a pair of # symbols and substitute each pair with just a single # (because I then want to convert that text file to a database using schema.ini, but I think that only wants a single delimiter symbol) - so just replace every occurrence of ## with # but otherwise leave the file unchanged.
Reply With Quote
  #2  
Old 06-21-2017, 11:42 AM
Cerian Knight's Avatar
Cerian Knightsimple find and replace in text file Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,191
Default

Show us the code you have tried.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #3  
Old 06-22-2017, 03:29 AM
Daigon Ali Daigon Ali is offline
Regular
 
Join Date: Aug 2014
Location: London
Posts: 53
Default

Hi,

You could try this. Needs a reference to Microsoft Scripting Runtime.

Code:
Private Sub Command1_Click()
Dim fso As Scripting.FileSystemObject
Dim ts As TextStream
Dim sPath As String
Dim sTxt As String

sPath = <your text file path>
'open text file and read text to variable
Set fso = New Scripting.FileSystemObject
Set ts = fso.OpenTextFile(sPath, ForReading)
sTxt = ts.ReadAll
ts.Close
'replace ## with #
Do While InStr(sTxt, "##") > 0
    sTxt = Replace(sTxt, "##", "#")
Loop
'write text back to text file
Set ts = fso.OpenTextFile(sPath, ForWriting)
ts.Write sTxt
ts.Close
Set fso = Nothing
Set ts = Nothing
End Sub
Or if you don't want to use the file system object

Code:
Private Sub Command1_Click()
Dim sPath As String
Dim ffile As Integer
Dim sTxt As String

sPath = <your text file path>
'open text file and read text to variable
ffile = FreeFile
Open sPath For Input As #ffile
sTxt = input(LOF(ffile), ffile)
Close #ffile
'replace ## with #
Do While InStr(sTxt, "##") > 0
    sTxt = Replace(sTxt, "##", "#")
Loop
'write text back to text file
Open sPath For Output As #ffile
Print #ffile, sTxt
Close #ffile
End Sub

Last edited by Daigon Ali; 06-22-2017 at 05:36 AM.
Reply With Quote
  #4  
Old 06-24-2017, 05:36 AM
starmanMike starmanMike is offline
Junior Contributor
 
Join Date: Oct 2005
Location: near Norwich, UK
Posts: 286
Default

many thanks all. I shall give them a try and see!
Reply With Quote
  #5  
Old 06-24-2017, 10:23 AM
starmanMike starmanMike is offline
Junior Contributor
 
Join Date: Oct 2005
Location: near Norwich, UK
Posts: 286
Default

Okay - gave it a try and surprise surprise! It worked!! I used the non-FSO version but I'm sure the other version worked too.
Thanks ever so much, both of you guys.
Reply With Quote
  #6  
Old 07-20-2017, 04:34 AM
starmanMike starmanMike is offline
Junior Contributor
 
Join Date: Oct 2005
Location: near Norwich, UK
Posts: 286
Default using split on txt file

Okay, just an addition.
I realised that I didn't need all this faffing about with schema.ini files and the rest - I could just use the split() function, which is what I did, and it works (sort of)...
Where the 'sort of' comes in is that, even though running through debug.print to check the output, the first line of the .txt file is included (which I don't want, it's the column headers) and I get an error saying 'subscript out of range'. The subscript isn't the array members of 'split' is it? since these go from 0 to 15 - and I only want some of them.
Here's the relevant coding:
Code:
...
Dim longstring As String
Dim bits() As String

Open afile For Input As #1
Do While Not EOF(1)
    Line Input #1, longstring
    bits = Split(longstring, "##")
        Debug.Print bits(1), bits(3), bits(5), bits(14)
        
        Loop
        Close #1
End Sub
And here's a section from the debug.print:
Code:
StarName      	JD            		Mag           	ChartID 
 TZ CYG        	2457029.2     	11.0          		1995 
 S CYG         	2457029.2     	13.0          		4600QA 
 SV CEP        	2457029.2271             10.1       		2007 
 BO CEP        	2457029.2278             11.7          		7219ARX 
 SS CYG        	2457029.2306             12.1          		5167HDQ 
 CH CYG        	2457029.2333              7.2           		1995 
 YZ CEP        	2457029.2354             11.0          		1991 
 V1514 CYG     	2457029.241   	12.5          		4600QA 
(I assume the left-offset in the 'mag' & 'chartID' columns is just a result of there not being 4 d.p. in the 'JD' column).
The whole text file is echoed, so no problem there. (it's about 25 lines long and is just a test file. In reality the file could contain thousands of lines) but 2 questions:

1) Why am I getting a 'subscript out of range' error?
2) How do I prevent the unwanted top line being read? Could I maybe run a 'preamble' routine which deletes this line first?
Loads of thanks for any help.
Reply With Quote
  #7  
Old 07-24-2017, 03:06 AM
Daigon Ali Daigon Ali is offline
Regular
 
Join Date: Aug 2014
Location: London
Posts: 53
Default

Hi,
Perhaps the subscript error is because the array resulting from the split doesn't have the number of elements you later refer to?

As for skipping the first line you could just create a temporary variable e.g. tmp and insert this line after opening the file and before you begin your loop.
Code:
If Not EOF(1) Then Line Input #1, tmp
Reply With Quote
  #8  
Old 09-14-2017, 06:09 AM
gibra's Avatar
gibra gibra is offline
Junior Contributor
 
Join Date: Apr 2008
Location: Italy
Posts: 386
Default

We need the file, to says what is the problem.
Can you upload it?
Reply With Quote
Reply

Tags
file, pair, replace, simple, single, text


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
simple find and replace in text file
simple find and replace in text file
simple find and replace in text file simple find and replace in text file
simple find and replace in text file
simple find and replace in text file
simple find and replace in text file simple find and replace in text file simple find and replace in text file simple find and replace in text file simple find and replace in text file simple find and replace in text file simple find and replace in text file
simple find and replace in text file
simple find and replace in text file
 
simple find and replace in text file
simple find and replace in text file
 
-->