-2

I have a text file contains delimited records.

1243;jhhf';982u4k;9u2349;huf8
kij;9238u;98ur23;jfwf;03i24

I need to replace the value of 4th part from every record with the value returned from database or someother source.

Any clue ? expecting VB CODE

ann
  • 576
  • 1
  • 10
  • 19
Santhosh_ms3
  • 110
  • 2
  • 3
  • 15
  • How do you want to approach this? What programming languages (shell, PHP, C++, etc) are available? –  Jan 19 '12 at 07:31
  • How do you want to join both, does every fourth part gets the same new value or do you need to lookup it(f.e. first part is an identifier)? Do you need to replace every fourth part in line, in other words, is it splitted by `Environment.NewLine` at all or is it one single line? – Tim Schmelter Jan 19 '12 at 10:09
  • Yes , every fourth part by a value from DB based on the value present in the flatfile not the same value for all. – Santhosh_ms3 Jan 20 '12 at 11:56
  • Possible duplicate of http://stackoverflow.com/questions/8941276/text-file-handling-with-sql-database-visual-basic – Joel Coehoorn Jan 25 '12 at 17:12

2 Answers2

0

Take a look at here( for C# language ); Split string in C#

Lost_In_Library
  • 3,265
  • 6
  • 38
  • 70
0

You could try this (written in C#):

C# release

List<string> newLines = new List<string>();
string[] lines = File.ReadAllLines(filename);
foreach (string line in lines)
{
     string[] parts = line.Split(";".ToCharArray());
     parts[3] = string_from_db;
     newLines.Add(String.Join(";", parts));
}
File.WriteAllLines(filename, newLines.ToArray());

VB.NET release

Dim newLines As List(Of String) = New List(Of String)
Dim lines As String() = File.ReadAllLines(filename)
For Each line As String In lines
    Dim parts As String() = line.Split(";")
    parts(3) = string_from_db
    newLines.Add(String.Join(";", parts))
Next
File.WriteAllLines(filename, newLines.ToArray())
Marco
  • 56,740
  • 14
  • 129
  • 152
  • Applogies, I need it for VB.NET application. – Santhosh_ms3 Jan 19 '12 at 08:37
  • it works. Thanks. But i'm struggling to get the value from database now. Can i get a code sample for fetching the value from DB using the string value from the flatfile as stated in my original Question ? – Santhosh_ms3 Jan 20 '12 at 11:53
  • Your info is very useful. but i need the answer for the next one also. you want me to post it as new one ? – Santhosh_ms3 Jan 20 '12 at 12:03
  • @user1157902: you can't ask several questions on a single post. You asked to show you a method to substitute a part of a string in every line of a file you have to load: this was answered. Now you ask (in same post) how to access a database and you did not provide anything to reply: which database? which structure? what do you want to query? what do you want to get? You should refactor your question adding every single think I'm telling you, but it's the second question in the same post. More: you'll find tons of examples on how to query a database from VB.NET... – Marco Jan 20 '12 at 12:06
  • Appologies. i'm very new to the VB and to this forum. let me reframe my Question and post it as new. – Santhosh_ms3 Jan 20 '12 at 12:09