Re: End of Line
- From: Odd <audrey.nsh@xxxxxxxxx>
- Date: Tue, 8 Sep 2009 11:36:47 -0700 (PDT)
On Sep 8, 12:38 pm, "Richard Mueller [MVP]" <rlmueller-
nos...@xxxxxxxxxxxxxxxxxxxx> wrote:
"Odd" <audrey....@xxxxxxxxx> wrote in message
news:3ccd9e18-9354-4c57-8ec5-4298bc9be107@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Sep 8, 11:45 am, "Richard Mueller [MVP]" <rlmueller-
nos...@xxxxxxxxxxxxxxxxxxxx> wrote:
"Odd" <audrey....@xxxxxxxxx> wrote in message
news:8a2b8b75-480a-4315-88d4-849b5ceb3c96@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi everyone,
I have a .csv file where certain fields have carriage returns/new line
feeds. Because of such, I cannot properly import my file. Hence, I
wrote the following script and while it removes the carriage returns/
new line feeds through the file, it ALSO removes the END OF LINE
carriage return/line feed. So now, I have one file with one record.
Basically,I need to know how to remove the carriage returns/line feeds
in the file EXCEPT for the ones that is END OF LINE. Can somebody help
me please?
Dim strSearchString, objFSO, objFile
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("d:\Test\Project.csv", ForReading)
strSearchString = objFile.ReadAll
objFile.Close
objFSO.CreateTextFile "d:\Test\Project_" & DatePart("yyyy",Date) &
Right("0" & DatePart("m",Date),2) & Right("0" & DatePart("d", Date),2)
& ".csv"
Set objFile = objFSO.OpenTextFile("d:\Test\Project_" & DatePart
("yyyy",Date) & Right("0" & DatePart("m",Date),2) & Right("0" &
DatePart("d", Date),2) & ".csv", ForWriting)
strSearchString = Replace(strSearchString, VbCrLf, "")
strSearchString = Replace(strSearchString, Vbtab, "")
objFile.Write strSearchString
objFile.Close
When I read a text file I use the ReadLine method of the file object. I
loop
through all lines of the file until the AtEndOfStream condition is met. I
also check each line to see if it is blank:
======
Do Until objFile.AtEndOfStream
strLine = Trim(objFile.ReadLine)
If (strLine <> "") Then
' Process the line...
End If
Loop
=====
Alternatively, I use the ReadAll method and use the Split function to
convert into an array of lines. Again, any line can be blank, especially
the
last line:
===
arrLines = Split(objFile.ReadLine, vbCrLf)
For Each strLine In arrLines
If (strLine <> "") Then
' Process the line...
End If
Next
--
Richard Mueller
MVP Directory Services
Hilltop Lab -http://www.rlmueller.net
--- Hide quoted text -
- Show quoted text -
Here is what I have and I still see the carriage returns and line feed
Dim strLine, objFSO, objInput, objOutput
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile("d:\Test\Project.csv", ForReading)
objFSO.CreateTextFile "d:\Test\Project_" & DatePart("yyyy",Date) &
Right("0" & DatePart("m",Date),2) & Right("0" & DatePart("d", Date),2)
& ".csv"
Set objOutput = objFSO.OpenTextFile("d:\Test\Project_" & DatePart
("yyyy",Date) & Right("0" & DatePart("m",Date),2) & Right("0" &
DatePart("d", Date),2) & ".csv", ForWriting)
Do Until objInput.AtEndofStream
strLine = Trim(objInput.ReadLine)
If (strLine <> "") Then
strLine = Replace(strLine, vbCrLf, "")
strLine = Replace(strLine, vbtab,"")
objOutput.WriteLine strLine
End If
Loop
objInput.Close
objOutput.Close
-------------
Pegasus's suggestion may be best. The only thing I can think of is that your
comma delimited file has carriage returns embedded in the field values. If
the Replace function doesn't remove them, maybe they are vbCr or vbLf
characters in addition to vbCrLf. I would try:
=======
Do Until objInput.AtEndofStream
strLine = Trim(objInput.ReadLine)
If (strLine <> "") Then
strLine = Replace(strLine, vbCrLf, "")
strLine = Replaced(strLine, vbCr, "")
strLine = Replaced(strLine, vbLf, "")
strLine = Replaced(strLine, vbFormFeed, "")
strLine = Replace(strLine, vbtab,"")
objOutput.WriteLine strLine
End If
Loop
========
vbCrLf is Chr(13) & Chr(10) (ANSI carriage return and line feed)
vbCr is Chr(13) (ANSI carriage return)
vbLf is Chr(10) (ANSI line feed)
vbFormFeed is Chr(12) (ANSI form feed)
--
Richard Mueller
MVP Directory Services
Hilltop Lab -http://www.rlmueller.net
--- Hide quoted text -
- Show quoted text -
What Richard is saying is correct.....the carriage returns and line
feeds is embedded in the field values. Richard: I tried your code..it
removed the tab,but did not remove the carriage return nor the line
feed.
.
- Follow-Ups:
- Re: End of Line
- From: ekkehard.horner
- Re: End of Line
- From: Odd
- Re: End of Line
- References:
- End of Line
- From: Odd
- Re: End of Line
- From: Richard Mueller [MVP]
- Re: End of Line
- From: Odd
- Re: End of Line
- From: Richard Mueller [MVP]
- End of Line
- Prev by Date: Re: End of Line
- Next by Date: Re: End of Line
- Previous by thread: Re: End of Line
- Next by thread: Re: End of Line
- Index(es):
Relevant Pages
|