Consulting

Results 1 to 8 of 8

Thread: Solved: How to delete all " characters of a Txt file?

  1. #1
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location

    Solved: How to delete all " characters of a Txt file?

    Hi everyone,

    I want to learn that how to delete all " characters of a txt file via VBA code?

    (I want to delete all Chr(34) from the txt file in located "C:\")

    The file contains as below:
    "<ACB>","<DEF>","<TARIH>","<ZAMAN>","<EDT>","<CTX>","<LKD>","<CVS>"
    "ANKARA","60","20000714","123000","0.032","0.029","0.030","24827"


    I wish do like this:
    <ACB>,<DEF>,<TARIH>,<ZAMAN>,<EDT>,<CTX>,<LKD>,<CVS>
    ANKARA,60,20000714,123000,0.032,0.029,0.030,24827


    Thanks in advance.
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    [VBA]
    Sub DelQuotes()
    Dim fs, a, strLine As String
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.OpenTextFile("C:\AAA\Quotes.txt", 1, 0)
    strLine = a.readall
    strLine = Application.WorksheetFunction.Substitute(strLine, Chr(34), "")
    a.Close
    Set a = fs.OpenTextFile("C:\AAA\Quotes.txt", 2)
    a.writeline strLine
    a.Close
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Hi mdmackillop,

    It's ok. Perfect!!!

    Thank you very much
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Doh! Beat me to it, Malcolm.

    I'm posting what I have anyway, although it outputs it to a new file:

    [vba]Sub TextStreamTest()
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    Dim fso As Object
    Dim fsoFileToWrite As Object, textStreamToWrite As Object
    Dim fsoFileToRead As Object, textStreamToRead As Object
    Dim strText As String

    'Link to the file to read and open text stream
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fsoFileToRead = fso.getfile("C:\Documents and Settings\kenp\Desktop\TextTest.txt")
    Set textStreamToRead = fsoFileToRead.OpenAsTextStream(ForReading, -2)

    'Creaete new file to write to
    fso.createtextfile ("C:\Documents and Settings\kenp\Desktop\TextTestOutput.txt")
    Set fsoFileToWrite = fso.getfile("C:\Documents and Settings\kenp\Desktop\TextTestOutput.txt")
    Set textStreamToWrite = fsoFileToWrite.OpenAsTextStream(ForAppending, -2)

    'Loop through the file, replacing "" with blanks
    Do Until textStreamToRead.atendofstream
    strText = textStreamToRead.ReadLine
    strText = Application.WorksheetFunction.Substitute(strText, Chr(34), "") & vbNewLine
    textStreamToWrite.Write strText
    Loop

    'Close text stream and release objects
    textStreamToRead.Close
    textStreamToWrite.Close
    Set textStreamToRead = Nothing
    Set textStreamToWrite = Nothing
    Set fsoFileToRead = Nothing
    Set fsoFileToWrite = Nothing
    Set fso = Nothing
    End Sub[/vba]
    Malcolm, yours is much shorter though.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Ken,
    I didn't realise that the original text would be overwritten until testing. I thought it would append. Right result, so I kept it!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Hi Ken,
    As you said, Malcolm's code is shorter.
    But thank you too.
    I saved yours too.
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  7. #7
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by mdmackillop
    Hi Ken,
    I didn't realise that the original text would be overwritten until testing. I thought it would append. Right result, so I kept it!
    Sounds reasonable to me! It's a pity that things don't work out like that more often, isn't it? LOL!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Always something new to learn, however one discovers it.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •