Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Remove Carriage Return/Line Feed from a Text File

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,2077
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Remove Carriage Return/Line Feed from a Text File
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Remove Carriage Return/Line Feed from a Text File - 2/16/2005 4:44:49 AM   
  nashvillemike

 

Posts: 41
Score: 0
Joined: 1/26/2005
From:
Status: offline
Is there a way to remove all carriage returns (and/or line feeds) from a text file using a vbscript?

For example, my file now reads:
test
test1
test3
test99

I need it to read like this:
testtest1test3test99

I've tried using this code which will replace for me, but I can't get it to replace/remove the returns:

Const ForReading = 1
Const ForWriting = 2

strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close

At a command line, I can type the following and test gets replaced with test1:
cscript replace.vbs "C:\Text.txt" "test " "test1 "

Any ideas?
 
 
Post #: 1
 
 Re: Remove Carriage Return/Line Feed from a Text File - 2/16/2005 5:46:13 AM   
  CaffeineAddiction

 

Posts: 144
Score: 0
Joined: 2/9/2005
From:
Status: offline
In stead of sending a character as a argument ... try using a number as an argument ...


syntax for replace (had to look it up) is:
Replace(expression, find, replacewith[, start[, count[, compare]]])

Your current find replace line:
strNewText = Replace(strText, strOldText, strNewText)

What you might want to think about changing it to:
strNewText = Replace(strText, chr(013), "") ' chr(013) = carriage return
strNewText = Replace(strText, chr(010), "") ' chr(010) = NL ... or New Line
strNewText = Replace(strText, chr(N), "") ' chr(010) = NL ... or New Line ' where n is = to one of the Dec char values here (http://www.lookuptables.com/)



...

(in reply to nashvillemike)
 
 
Post #: 2
 
 Re: Remove Carriage Return/Line Feed from a Text File - 2/16/2005 6:09:49 AM   
  nashvillemike

 

Posts: 41
Score: 0
Joined: 1/26/2005
From:
Status: offline
Good thinking!! Here's my finished code that doesn't ask for input. It just does the job! Thanks for the help!! :)


Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\test.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, chr(013) & chr(010), "") ' chr(010) = line feed chr(013) = carriage return

Set objFile = objFSO.OpenTextFile("c:\test.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close

(in reply to nashvillemike)
 
 
Post #: 3
 
 Re: Remove Carriage Return/Line Feed from a Text File - 2/16/2005 8:50:34 AM   
  bauhau

 

Posts: 8
Score: 0
Joined: 2/15/2005
From: New Zealand
Status: offline
hi NashvilleMike,

i was going to give my 2cents worth but then saw u already had a good answer.
i would add (1cent), go with 2 seperate lines to replace first Chr(10) linefeeds, then Chr(13) carriage returns, just to cover those situations where you don't have bothe next to each other -i'm certain i've seen some text files that only come with linefeeds but no carriage return which your replace code as it stands would miss..

cheers
james

(in reply to nashvillemike)
 
 
Post #: 4
 
 Re: Remove Carriage Return/Line Feed from a Text File - 2/16/2005 9:42:42 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
I would just do replace(replace(text,chr(10),""),chr(13),"") :D You could also use ReadLine which I believe it doesn't read CR/LF. Of course, it requires more work as to concatenate the line read, but it's just another way of doing it.

(in reply to nashvillemike)
 
 
Post #: 5
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Remove Carriage Return/Line Feed from a Text File Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts