Question : Strip CRLF from txt file using existing query

matthewspatrick had written this code for me to clean up some CRLF's in an import file we have which works very well. Found that we also just need to delete the last CRLF from our file as that is still causing our DTS import to fail as it tries to import the next row which is blank.

Looking for a solution to add to this to just strip the last CRLF from our TXT file import.


Dim fso, tsIn, tsOut, TheLine, arr

Set fso = CreateObject("Scripting.FileSystemObject")
Set tsIn = fso.OpenTextFile("c:\EE-Sample.txt")
Set tsOut = fso.CreateTextFile("c:\Corrected.txt", True)

Do Until tsIn.AtEndOfStream
      TheLine = tsIn.ReadLine
      arr = Split(TheLine, "^")
      If TheLine <> "" Then
            If UBound(arr) <> 15 Then TheLine = TheLine & " " & tsIn.ReadLine
            tsOut.WriteLine TheLine
      End If
Loop

tsIn.Close
tsOut.Close

Set tsIn = Nothing
Set tsOut = Nothing
Set fso = Nothing

Answer : Strip CRLF from txt file using existing query

I have no idea why you want to do this as it is simply not necessary, but if you want to remove the CrLf on the last line, then this is how to do it:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
Dim fso, tsIn, tsOut, TheLine, arr

Set fso = CreateObject("Scripting.FileSystemObject")
Set tsIn = fso.OpenTextFile("c:\EE-Sample.txt")
Set tsOut = fso.CreateTextFile("c:\Corrected.txt", True)

Do Until tsIn.AtEndOfStream
      TheLine = tsIn.ReadLine
      arr = Split(TheLine, "^")
      If TheLine <> "" Then
            If UBound(arr) <> 15 Then 
				TheLine = TheLine & " " & tsIn.ReadLine
			End if
			If Not tsIn.AtEndOfStream Then
				tsOut.WriteLine TheLine
			Else
				tsOut.Write TheLine
			End If			
      End If
Loop

tsIn.Close
tsOut.Close

Set tsIn = Nothing
Set tsOut = Nothing
Set fso = Nothing
Random Solutions  
 
programming4us programming4us