How do I remove hidden Line breaks in a text file with vbscript?

Posted on 2009-04-29
Last Modified: 2012-05-06
I am using a vbscript to read the event log and dump the information into a text file. I will be importing the text file at a later date into Excel or elsewhere.

the important bits of code is  below, however it doesnt appear to be working. The below gets rid of the majority of line breaks but still leaves some behind and it shows in Notepad as a little square (see attached image) but when looked at through notepad++ or excel it does as below and uses a new line
"The memory could not be "read"
Click on OK to terminate the program"

Is there any way to get rid of these line breaks, where have I gone wrong in my code?

Details = objevent.message

Details = Replace(Details,vbCrLf,"")

strFile.WriteLine Details

Open in new window

Question by:scarthy
    LVL 27

    Expert Comment

    Try replacing the ANSI character for carriage return, e.g.
    Details = Replace(Details,CHR(13),"")
    LVL 10

    Accepted Solution

    A vbCrLf is the equivilant of a chr(13)&chr(10) which is also the same as a carriage return and line feed.  When you view thes on a windows pc, unless you have both of those characters consecutively and in that order, notepad and other text editors will not show them as seperate lines and will show whichever character is there as the square that you see.  If you want to see what these characters really are, you can check by opening the text file in a hex editor and see what their value is.

    If you want to get rid of ALL of these characters.  Then along with what bluntTony said,

    Details = Replace(Replace(Details,CHR(13),"") ,CHR(10),"")

    Author Comment

    Thanks very much to you both for your replies.
    bluntTony I tried yours first, but unfortunately I got the same result and also got extra rows between each result (probably because of the missing chr(10) value. But thanks again for replying.

    TakedaT. Thats worked thank you very much.  How does yours work? does it replace  all carriage returns with line feeds and then replace all line feeds with a space?

    Thanks again both of you for your efforts.

    Author Closing Comment

    LVL 10

    Expert Comment

    No problem.

    If you break down the line from inside out.  It first replaces chr(13) with "", which is nothing so it deletes the character, then the next step outward does the same for any chr(10).  It gets rid of them one at a time.  Your original replace only looked for both of them as a whole (vbCrLf stands for just that, vb CarriageReturnLineFeed).  If yours didnt see a chr(13) and a chr(10) directly next to eachother, it left it alone, leaving some chr(10)s in there.

    Details = Replace(Replace(Details,CHR(13),"") ,CHR(10),"")

    Author Comment

    I understand. Thanks for that.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This script will sweep a range of IP addresses (class c only, and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
    Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now