473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Read last 7 lines from text box and store into string variables

Hi, I am try to read last seven line from text file and try to stored into string variable but receiving error. I am not sure what error but something wrong in my logic. please help if you can.

Thanks.

here is my code
Expand|Select|Wrap|Line Numbers
  1.   Dim fileName As String = "C:\text.txt"
  2.  
  3.   Dim lines As String() = IO.File.ReadAllLines(fileName)  ' Here I am trying to read only last 7 lines not entire file
  4.  
  5.   MsgBox(lines)
  6.  
  7.   Dts.Variables("email_txt").Value = lines
Oct 6 '14 #1
1 1134
Frinavale
9,735 Expert Mod 8TB
When you use the IO.File.ReadAllLines method it returns an array of strings that are the lines in the file.

You are attempting to display an array of strings in the message box...but I think this will simply display something like System.Array and probably isn't crashing anything.

You are also attempting to do this:
Expand|Select|Wrap|Line Numbers
  1. Dts.Variables("email_txt").Value = lines
Which is probably where you are having a problem. I am going assume that Dts.Variables("email_txt") is a TextBox....and I doubt that a TextBox has an Value property which is probably your issue.

You should change your code so that it knows that the type is a TextBox you can set it's Text Property.

Like this:
Expand|Select|Wrap|Line Numbers
  1. DirectCast(Dts.Variables("email_txt"), TextBox).Text = lines
However, the above code will only display the first line in the array in the TextBox and you want to display the last 7.

Keeping in mind that the TextBox can only display 1 string, you will need to create a string with the appropriate content for it to display.

I recommend that you use the http://msdn.microsoft.com/en-us/libr...gBuilder Class to help you with this.

Loop through your array backwards from the end 7 positions and add the line to the StringBuilder.

Expand|Select|Wrap|Line Numbers
  1.   Dim sb As New StringBuilder
  2.  
  3.   If lines.Count >= 7 Then
  4.     Dim linePosition As Integer= lines.Count - 1 
  5.     Dim numLinesRead As Integer = 0
  6.     While numLinesRead < 7
  7.      sb.AppendLine(lines(linePosition))
  8.      linePosition -= 1
  9.      numLinesRead += 1
  10.     Loop
  11.   End If
Loop forwards through your array starting 7 positions back from the end and add the line to the StringBuilder.


Expand|Select|Wrap|Line Numbers
  1.   Dim sb As New StringBuilder
  2.  
  3.   If lines.Count >= 7 Then
  4.     Dim linePosition as Integer =  lines.Count - 7 
  5.  
  6.     For linePosition To lines.Count - 1
  7.      sb.AppendLine(lines(linePosition))
  8.     Next
  9.   End If
Please note that none of the above code is tested ;)

-Frinny
Oct 6 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
2
by: Tarren | last post by:
Hi: I want to check my text file to ensure the last line has only DONE on it, prior to loading the file. How can I do this with a StreamReader object to go to the last line so I can perform...
8
by: kd | last post by:
Hi All, I have a string variable, whose length is not constant. I need to extract the whole string, except the last character in the string. Is there any string function that can do this? ...
2
by: John Wildes | last post by:
hello I was wondering if someone could point me in the direction of information on using app.config to store string variables. I have a couple of variables that store path information for file...
2
by: Marcus Kwok | last post by:
I am writing a program to read in a file, do some processing, then write it out to a different file. I really like the idiom I use for output: // std::vector<std::string> vec; // std::ofstream...
7
by: Thirsty Traveler | last post by:
I have a question regarding the use of guery string variables. I understand this can be a security risk subject to "brute force" attacks. Is this true and, if so, what is the proper way to handle...
7
by: Malcolm | last post by:
This is a program to convert a text file to a C string. It is offered as a service to the comp.lang.c community. Originally I thought it would be a five minute job to program. In fact there are...
5
shrek123
by: shrek123 | last post by:
How to read last to lines of a file present at remote server.
3
by: sharan250 | last post by:
Hi all, I have to read severals line of a document in perl. I wrote a program but with that am able to read only lines by line. eg if i have a file in which contents are: process jkdgf jdk jhfk...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.