Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to remove only the LAST carriage return and line feed from a text file 1

Status
Not open for further replies.

Zogfew

MIS
Feb 6, 2006
6
US
I would like some help with vb script. I know next to nothing about vb scripting so bare with me. I wanted write a dos batch file to do this but quickly realized that this is too complicated for a batch file. After searching the internet I found a vbscript that removes the CR\LF but the only one I found that works, removes all of them. I only want to remove the last one.
This is code I found:

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\VDW\VDW_Output\DDOD\TodayDDOD.csv", ForReading)

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

Set objFile = objFSO.OpenTextFile("D:\VDW\VDW_Output\DDOD\TodayDDOD.csv", ForWriting)
objFile.WriteLine strNewText
objFile.Close

Thanks for the help
 
I believe this will work, not tested though:
Code:
strNewText = Replace(strText, vbCrLf, "", InStrRev(strText, vbCrLf))

Notes: vbCrLf is a vbscript constant for a chr(10) (carriage return) and a chr(13) line feed

InStrRev will return the position of the search string starting from the end of the main string

The 4th parameter of the Replace function is the start position where the replacing will start.
 

Geates:
I mean if you sat down and typed up a letter to someone, and you hit the enter key just after the very last word you typed. (if that makes any sense)

Guitarzan:
Itried that code and it removed all of the file contents except for the CR\LF (perhaps I did something wrong?

 
Sorry for the error earlier (that's why I included the links to the definitions of the functions! ;-))

I think this will do the job
Code:
Option Explicit
Const ForReading = 1
Const ForWriting = 2

Dim objFSO, objFile, strText, iPos
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\data\vbs\tt1\repl\input.txt", ForReading)

strText = objFile.ReadAll
objFile.Close

iPos = InStrRev(strText, vbCrLf)
If iPos > 0 Then
   strText = Left(strText, iPos - 1) & Replace(Mid(strText, iPos), vbCrLf, "")
   Set objFile = objFSO.OpenTextFile("c:\data\vbs\tt1\repl\input.txt", ForWriting)
   objFile.Write strText
   objFile.Close
End If
 
Geates: No, ignore what I said earlier about the Replace function... I was working too quickly and was wrong about what that parameter does. [blush]
 
Why not simply this ?
Code:
If Right(strText, 2) = vbCrLf Then
  strNewText = Left(strText, Len(strText) - 2)
Else
  strNewText = strText
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
strNewText = trim(strText) ??

Never knock on Death's door: ring the bell and run away! Death really hates that!
 
Another way (even simpler) way is to replace this:
objFile.WriteLine strNewText
with thos:
objFile.Write strText

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I read the post as remove the last occurrence of "x", from a string, wherever it may occur. Removing a trailing CRLF is a much simpler effort.
 
Thanks for the suggestions. I have tried them and have not had luck so far. Perhaps it is me, i'm not sure. Here is a sample of the last three lines of text from the file:

1,112,1,0,15,290,15,16.10,12070,"CK",3,1487,1,194,108.41,0,0,0.00,0,0,5," ",2,0,0.00,0,0,0,0,1,0,0,84.30,0,4,0,0,11513,-3.47,0,0
1,112,1,0,15,292,15,200.00,12915,"WD",4,0,4,15,-287.85,0,0,0.00,0,4,5," ",2,0,0.00,0,0,0,0,1,0,0,-287.85,0,2,0,0,11513,-502.85,0,0
1,112,1,0,15,295,15,15.00,12915,"SC",4,0,4,15,-287.85,0,0,0.00,0,4,5," ",2,0,0.00,0,0,0,0,1,0,0,-287.85,0,2,0,0,11513,-502.85,0,0

See the goofy arrow at the end, it represents the carriage return. When I used Guitarzans code it left the last CR. I tried PHV's
If Right(strText, 2) = vbCrLf Then
strNewText = Left(strText, Len(strText) - 2)
Else
strNewText = strText
End If
with no luck
As far as PHV's post of:
Another way (even simpler) way is to replace this:
objFile.WriteLine strNewText
with thos:
objFile.Write strText

I am unsure which code i should try this in, my original posted code, PHV's, or Guitarzans
Thanks
 
Like this:
Code:
Const ForReading = 1 
Const ForWriting = 2 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("D:\VDW\VDW_Output\DDOD\TodayDDOD.csv", ForReading) 
strText = objFile.ReadAll 
objFile.Close 
Set objFile = objFSO.OpenTextFile("D:\VDW\VDW_Output\DDOD\TodayDDOD.csv", ForWriting) 
objFile.Write strText 
objFile.Close

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV:
I used the code you posted last and it worked for me.
Thank you for all the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top