473,888 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error removing last vbCrLf character from a text file created by FSO CreateTextFile

I'm using FileSystemObjec t to open and write to a tab delimited text
file.

First, I connect to a database and select some data. Then I create the
text file and insert each record in the text file. At the end of each
record I insert a vbCrLf character in the text file to create a new
line. Thus each record is in a new line.

The problem is that I get a tab delimited text file where the LAST LINE
in the text file is always a blank line. I need to remove the LAST
vbCrLf character from the text file so that there is no blank line at
the end of the text file.

I've tried checking for AtEndOfStream in a Loop to try and remove the
LAST vbCrLf character, but there's something wrong with the code...

The Do While Not objStream.AtEnd OfStream loop is crashing with
"Microsoft VBScript runtime (0x800A0036) Bad file mode" errors.

What have I done wrong?

Set objFSO = Server.CreateOb ject("Scripting .FileSystemObje ct")
strDataFileName = Server.MapPath( "SomeFolder\Tex tFile.txt")

'Delete any existing Format 01 text file (from previous batches)
If (objFSO.FileExi sts(strDataFile Name)) Then
objFSO.DeleteFi le(strDataFileN ame)
End If

'Create blank text file
strDataPath = Server.MapPath( "SomeFolder \")
Set strCreateFile = objFSO.CreateTe xtFile("TextFil e.txt", True)
Set strCreateFile = Nothing

Const ForWriting = 2
Set objStream = objFSO.OpenText File(strDataFil eName, ForWriting, True)

strSelect = "SELECT X,Y,Z FROM SomeTale WHERE SomeCondition = '12345'"

Set conn = Server.CreateOb ject("ADODB.Con nection")
conn.Open Application("co nnstring")
Set rs = conn.Execute(st rSelect)

'Add the data to the text file
Do While Not rs.eof
strText = rs("SURNAME") & VbTab & rs("FIRSTNAME" ) & VbTab &
rs("DSINITIALS" ) & VbTab & VbTab & VbTab & "0.00" & VbTab & rs("ID") &
VbTab & rs("BLAH") & VbTab & "X"
objStream.Write (strText & vbCrLf) 'This inserts a vbCrLf
character at the end of each record
rs.MoveNext
Loop

rs.Close : Set rs = nothing
conn.Close : Set conn = nothing

'Remove the last VbCrlf from the text file.
Do While Not objStream.AtEnd OfStream
CLine = objStream.ReadL ine
CLine = Replace(CLine,V bCrlf,"")
strDataFileName .Write CLine
Loop

objStream.Close : Set objStream = Nothing
Set objFSO = Nothing

Feb 15 '06 #1
4 7406
Did you try it like this:-
objStream.Write strText
rs.MoveNext
If Not rs.EOF Then objStream.write vbCrLf
Loop


Feb 15 '06 #2
Perfect!
Thanks

Feb 16 '06 #3
> Did you try it like this:-

objStream.Write strText
rs.MoveNext
If Not rs.EOF Then objStream.write vbCrLf
Loop


Thanks. It worked perfectly.

But then I tried moving the code into a Function to make it reusable:

Function PrintCategory(C ategory)
strText = strRecordNumber & VbTab & rs("somefield" ) & VbTab &
Category & VbTab & rs("anotherfiel d") & VbTab & "X"
objStream.Write (strText)
If Not rs.eof Then
objStream.Write vbCrLf
End If
End Function

And I call it from the main body of the page using:

Set rs = conn.Execute("S ELECT X1,Y2,Z3 FROM SomeTable WHERE Something
= '12345'")
Do While Not rs.eof
If rs("X1") = "Yes" Then
PrintCategory(0 1)
End If

If rs("Y2") = "Yes" Then
PrintCategory(0 2)
End If

If rs("Z3") = "Yes" Then
PrintCategory(0 3)
End If

rs.MoveNext
Loop
The problem is that I now get a blank line/carriage return at the end
of the file. As in my original post, I don't want the text file to have
this blank line / carriage return after the LAST record.

Where have I gone wrong?

Feb 17 '06 #4
In my code it was important that the MoveNext method on the recordset be
called before testing EOF.

Your code is no longer doing that.
Feb 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
609
by: Kevin Ingram | last post by:
Ok, this is probably a silly question but I just keep hitting a brick wall here. I usually develop my sites entirely in ASP and use a database for data storage, works great for me. I also sometimes save information to a text file on my server like c:\invoices\customername.txt for batch processing later... I use the FSO commands and it all works slick. Now, I have a new project: A client wants us to log in to their website on another...
18
22014
by: William Payne | last post by:
Hello, I need to write a program that opens a text file and scans the entire file for a specific line and when that line is found, a particular word on that line is to be replaced. The new word is given as an argument to the program. I wrote a small test program that doesn't work because strcmp() fails to find a matching line. Here's the code: #include <stdio.h> #include <string.h> int main(int argc, char** argv)
2
1555
by: GMK | last post by:
Dear all in my asp.net application i have a text file that is installed with my application on the server. this text file is filled with data through a web interface in my application. i need to know how could i update this text file programatically and using a web interface. what i need is not to over write the content of the text file but to delete it's content and then write a new content. id there any way to do this?
10
6810
by: Backwards | last post by:
Hello all, I'll start by explaining what my app does so not to confuss you when i ask my question. ☺ I have a VB.Net 2.0 app that starts a process (process.start ...) and passes a prameter through from a combo box. The combo box items are made up of IP address and computer host name. Anything a user places in this combo box it writes this to a txt file called history.txt
2
18040
by: Ellen | last post by:
Hi All - I have some data that I export from Access to a pipe-delimited text file. Unfortunately, the text file always has a carriage return symbol at the end. I need to export this file frequently and send to a system that cannot accept this character at the end of the file. I am looking for a way, perhaps with VB code, to automate this process. I found this code on another Google Group, but I can't get it to work.
1
1468
by: thescotsman | last post by:
Newbie to .net the following is the first 20 lines from a text file % different categories found in input file: frontpage news tech local opinion on-air misc weather msn-new health living % sequences: 1 1 2
0
1310
by: ghostface | last post by:
**How do I parse a textfile and edit only a certain part of it. Specifically, just the last column. My textfile looks like this. #Server Group 1 !Name01,192.168.2.201,5901,123456,description01,\\p4d1,01,on !Name02,192.168.2.202,5902,123456,description02,\\p4d1,01,on **Now I have an ASP page with buttons. **example. **When I click button1.. It means it will parse row 1 and last column. Changing
1
5002
by: wish | last post by:
Hello, I have problem is when i append new data in text file always append at the last of the text file. Am i can append the new data in beginning of the text file? Mean when i have update data the text file and always write at the beginning of the text file and the previous data at the below of the new data.. Can anyone have idea about it? Thanks in advance...
0
2002
by: rahulsapra | last post by:
hello everyone m new to j2me plzz help I have a text file that contains data line by line.. format is : response:<some text><space>Pin:<some text><space>time:<hh:mm:ss> eg response:invalid pin Pin:1111 time:04:04:16
0
9961
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9597
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5817
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4244
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.