Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert tab delimited files to comma delimited files 1

Status
Not open for further replies.

vb1720

Programmer
Jul 26, 2007
8
US
Hi, I'm a newbie on this forum. So please bear with me! :) I am at my wits end with my project using VB.Net 2005. I am trying to convert a tab delimited file to a comma delimited file. I will be using .txt file with data where they are seperated with tabs. I want to replace the tabs with commas. I have tried the replacer code but no luck. I have tried the readline and writeline with the "," in there but it's not doing anything. Any sugguestions?

Thanks!
vb1720
 
Try:

Code:
Dim ReplaceCode as String
ReplaceCode = txt1.Text
ReplaceCode = ReplaceCode.Replace(vbTab,",")

In other words, I loaded your file into a text box and did the replace there. Worked for me.

HTH.

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
Thanks for your help! Maybe I was not clear with the file format. I cannot have the notepad file (.txt) loaded in a text box. I need the program to read a notepad file that is tab delimited and replace the tabs with commas and then save it on the computer as a comma delimited file. Is this possible with VB.Net?

Thanks!
 
System.IO is your friend. This is in the 2.0 framework:
Code:
  Private Sub TabToComa(ByVal FileName As String)
    Dim sContents As String = IO.File.ReadAllText(FileName)
    sContents = sContents.Replace(ControlChars.Tab, ","c)
    IO.File.WriteAllText(FileName, sContents)
  End Sub

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I tried to install .net framework 2.0 and I do see it on my computer. I am confused as to how to put the system.IO code on VB.Net 2005 with the .net framework 2.0? Any advice would be greatly appreciated. Thanks!
 
system.io is included in the base libraries. If you have the 2.0 .Net framework installed, and you are working in VS 2005, you should be able to type system.io. and get a list of available objects, methods, and namespaces.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks ThatRickGuy!! I get the concept now! :) So basically I use the class library application and start typing system.io then I get the list of objects, methods and namespaces. I am assuming this system.IO is best used with the class library application, not the windows application?

Many thanks!
 
I can be used by anything, it is a public namespace included with the .Net core libraries. You can use it in a Windows app, Class Library, Web Service, Windows Service, Console App, etc...

.Net is two primary things:

1) A compiler that turns your high level code into something a computer can understand.
2) A collection of libraries of commonly used objects and functionality.

That means that you don't have to create your own String class from scratch (like you do in C++). The creators of the .Net framework already did a lot of work for you. They created Strings, datasets, file manipulation, etc... All of the stuff they created is organized in to namespaces. Everything related to Input/Output is in the System.IO namespace. Everything related to data is in the System.Data namespace. Everything about networks is in the System.Net namespace.

Even the basic types are all in the System namespace (System.String, System.Integer, etc...) Luckily though, the System namespace is imported for us, so we don't have to type the word 'system' 500 time in each method ;)

These namespaces are available to you any time you are working in .Net. If you are using Visual Studio to write your code, you can type "system." and a list will pop-up showing you all of the things in the System namespace.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Hello, I have tried the code where I replace TABS with commas in a notepad file (.txt) but it does not seem to be working. Here is the code I wrote, let me know if I am missing something or if my logic is a bit off. :) Thanks for your help!

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click




Dim FILE_NAME As String = "C:\test.txt"
Dim TextLine As String
Dim OldText As String
Dim NewText As String


If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StringReader(FILE_NAME)

Do While objReader.Peek() <> -1
TextLine = objReader.ReadLine() & vbNewLine

'Dim objWriter As New System.IO.StreamWriter(FILE_NAME)

OldText = "TAB"
NewText = OldText.Replace("TAB", ",")

Loop

objReader.Close()


Else
MsgBox("File Does Not Exist")

End If


End Sub


 
Look at Rick's code again

>> .Replace(ControlChars.Tab, ","c)

That looks almost enough like VB6 to be scary.

Use the methods Rick provided. Leave the Do While in the 6 ages :)


[sub]____________ signature below ______________
I am Tedward Keyboardhands!!!
You are a amateur developer until you realize all your code sucks.
Jeff Atwood[/sub]
 
I see where you are saving the coma delimited text to a string, but I'm not seeing where you write that string to a file... If you want to iterate through the file on your own, you're going to need to write those strings back out to a temp file, then overwrite the original file with your temp file.

Or... you could use the 3 line solution posted above.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I want a 2 line solution


[sub]____________ signature below ______________
I am Tedward Keyboardhands!!!
You are a amateur developer until you realize all your code sucks.
Jeff Atwood[/sub]
 
I could write that in 1 line with vb6. [smile]

-George

"the screen with the little boxes in the window." - Moron
 
How about one?

Code:
  Private Sub TabToComa(ByVal FileName As String)
    IO.File.WriteAllText(FileName, IO.File.ReadAllText(FileName).Replace(ControlChars.Tab, ","c))
  End Sub
[code]

But [b]I'm[/b] not going to be the one maintaining that mess :P

-Rick

VB.Net Forum forum796    forum855 ASP.NET Forum
    [monkey][url=http://www.ringdev.com]I believe in killer coding ninja monkeys.[/url][monkey]
 
Thanks to the replies on my question! I wish I had VB6 with the ONE line code! :) But I am stuck with a free edition of vb 2005 express but it is not too bad.

What would the one line code be like in vb6?

Rick, I will try the three line code....and let you know how that goes!

Off to the land of coding....and another cup of coffee!


 
Rick, your awesome 3 line of code WORKED!!!! My jaw was all the way to the floor when I saw the result I wanted!! :) Thank you very much!! You rock!!!
 
Bad news. :( Even though my code worked on a test file using notepad file in tab delimited format, converting the tabs to commas, it did not work on the actual file. I wonder if the actual files are not tab delimited but possibly just spaces in between? Here is the actual file in its actual format (names have been modified for security purposes):


FONT KENDRA L D100829463 G AD 06/14/2007 06/30/2007

06/30/2010
Primary Address: 268 KEEN AVE ANYTOWN TX 60087

PH: 310-338-4162

JONESS ANDREW P D100829023 G AD 06/09/2007 06/26/2007

06/30/2010
Primary Address: 2811 SPRINGFIELD RD #106 ANYTOWN TX 60087

PH: 705-574-5506

SMITHY SHEBRA S D100829316 G AD 06/12/2007 06/14/2007

06/30/2010
Primary Address: 217 JUSTICE DR ANYTOWN TX 60087

PH: 310-712-5280

TILLY ANGELA S D100829081 G AD 06/11/2007 06/28/2007

06/30/2010
Primary Address: 213 FREEDOM TRATX ANYTOWN TX 60087

PH: 310-427-1515

JONESE TERRIE SAM D100829992 G AD 06/22/2007 06/22/2007

06/30/2010
Primary Address: 121 FREEDOM TRATX ANYTOWN TX 60087

PH: 310-694-4028
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top