Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   File I/O and Registry (http://www.xtremevbtalk.com/file-i-o-and-registry/)
-   -   simple find and replace in text file (http://www.xtremevbtalk.com/file-i-o-and-registry/328661-simple-replace-text-file.html)

starmanMike 06-21-2017 10:19 AM

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.

Cerian Knight 06-21-2017 12:42 PM

Show us the code you have tried.

Daigon Ali 06-22-2017 04:29 AM

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


starmanMike 06-24-2017 06:36 AM

many thanks all. I shall give them a try and see!

starmanMike 06-24-2017 11:23 AM

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.

starmanMike 07-20-2017 05:34 AM

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.

Daigon Ali 07-24-2017 04:06 AM

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

gibra 09-14-2017 07:09 AM

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


All times are GMT -6. The time now is 07:24 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.