473,758 Members | 5,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help With Tab Delimited File

29 New Member
I would like some expert advice. I am writing in VB6. I am opening a tab delimited file, deleting the first 50 lines, and rewriting the file to a temp file. The temp file has about 20 columns with x number of lines. What I am trying to do, is open the tab delimited file, and read each column into a named variable, ex. depth<column1>m pf<column2>tg<c olumn3>,etc. I have written a function to take the variable from column2(mpf) & create another variable(fph) to insert back into the file, at column2. This would move everything from column2(mpf) on, up one column. The columns and variables are as follows:
depth(column1), mpf(column2),tg (column3),c1(co lumn4),etc. with the variables. I need to create the variable fph and insert it into column2, where the new file will read as follows:
depth(column1), fph(column2),mp f(column3),tg(c olumn4),c1(colu mn5),etc to the end of the line. The code I have is as follows:
Expand|Select|Wrap|Line Numbers
  1. Public tFile As String
  2. Public mpf As Double
  3. Public fSave As String
  4.  
  5. Public Function TrimAll(Str)
  6. '***remove all non ASCI chrs and reduce internal whitespace to single
  7.     Dim i, strTemp, strOut, strCh
  8.  
  9.     strTemp = Str
  10.     For i = 1 To Len(strTemp)
  11.         strCh = Mid(strTemp, i, 1) '***look at each character in turn
  12.         '***if the chr is a space and the previous added chr was a space ignore it
  13.         '***otherwise add it on
  14.         If Not (strCh = " " And Right(strOut, 1) = " ") Then
  15.             strOut = strOut & strCh
  16.         End If
  17.     Next
  18.     TrimAll = strOut
  19. End Function
  20.  
  21. Private Sub cmdConvert_Click()
  22.     Dim Script As Object
  23.     Dim ObjStream As Object
  24.     Dim strLine As String
  25.     Dim a As Integer
  26.  
  27.     'check for .las file
  28.     Set Script = CreateObject("Scripting.FileSystemObject")
  29.      If Script.FileExists(tFile) Then
  30.         a = True
  31.         Set ObjStream = Script.opentextfile(tFile, 1, False, 0)
  32.      Else:
  33.         MsgBox "There is no LAS data in file. Please update with new LAS data.", vbInformation + vbOKOnly
  34.         a = False
  35.         Exit Sub
  36.      End If
  37.  
  38.     If tFile > "" Then
  39.     'open file
  40.     Set Script = CreateObject("Scripting.filesystemobject")
  41.     Open tFile For Input As #1 'read from this file
  42.     Open "C:DB_CUETemp.dat" For Output As #2 'write to this file
  43.  
  44.     'delete header and print rest of data to temp file
  45.     If a = True Then
  46.         Dim i As Integer
  47.         i = 0
  48.             Do While i < 50 And Not ObjStream.AtEndOfStream
  49.                 strLine = ObjStream.Readline
  50.         i = i + 1
  51.         Loop
  52.  
  53.         Do While Not ObjStream.AtEndOfStream
  54.           strLine = TrimAll(ObjStream.Readline)
  55.           YourArray = Split(strLine, "  ") '***use the tab key to create space between quotes.***
  56.         Print #2, strLine
  57.         Loop
  58.     End If
  59.  
  60.     Close #1
  61.     Close #2
  62.  
  63.     'save file
  64.     With CommonDialog1
  65.         .Filter = "All Files|*.las*|"
  66.         .InitDir = "C:DB_CUE"
  67.         .DialogTitle = "Save File As"
  68.         .ShowSave
  69.     End With
  70.     fSave = CommonDialog1.FileName
  71.     'copy temp file
  72.     FileCopy "C:DB_CUETemp.dat", fSave '& ".las"
  73.     'delete temp file once copied
  74.     Kill "C:DB_CUETemp.dat"
  75.  
  76.         If MsgBox("File is complete. Would you like to view the file?", vbQuestion + vbYesNo, "LAS iGenerator") = vbYes Then
  77.             Shell ("Explorer.exe tFile"), vbNormalFocus
  78. '            Unload Me
  79.         End If
  80.     Else:
  81.     If MsgBox("You must select a file to convert before you can continue.", vbExclamation + vbOKOnly) = vbOK Then
  82.         Exit Sub
  83.     End If
  84.     End If
  85. End Sub
Could you help shed some light on this, or refer me to someone who may? Thanks for your time.
Attached Files
File Type: txt testfile.txt (10.5 KB, 411 views)
Dec 29 '09 #1
11 1907
vb5prgrmr
305 Recognized Expert Contributor
This should be faster and easier to read...
Expand|Select|Wrap|Line Numbers
  1. Dim FName As String, FNumb As Integer
  2. Dim InnerForLoopCounter As Integer, OuterForLoopCounter As Integer
  3. Dim LineArray() As String, ElementArray() As String
  4. Dim InputText As String, OutputText As String
  5.  
  6. FName = "InputPathFileName" 'need to change
  7. FNumb = FreeFile
  8.  
  9. Open FName For Input As #FNumb
  10. InputText = Input(FileLen(FName), #FNumb)
  11. Close #FNumb
  12.  
  13. LineArray = Split(InputText, vbNewLine)
  14.  
  15. 'Skip First 50
  16. For OuterForLoopCounter = 50 To UBound(LineArray)
  17.   ElementArray = Split(LineArray(ForLoopCounter), vbTab)
  18.   OutputText = OutputText & ElementArray(0) & vbTab "item to insert"
  19.   For InnerForLoopCounter = 1 To UBound(ElementArray)
  20.     OutputText = OutputText & vbTab & ElementArray(InnerForLoopCounter)
  21.   Next InnerForLoopCounter
  22.   OutputText = OutputText & vbNewLine
  23. Next OuterForLoopCounter
  24.  
  25. FName = "OutputPathFileName" 'need to change
  26. FNumb = FreeFile
  27.  
  28. Open FName For Output As #FNumb
  29. Print #FNumb, OutputText
  30. Close #FNumb
  31.  
NOTE: Done freehand and so is untested and from memory so if I happened to flip something around, well Oopps! Sorry!

Also, since inner for loop is looping through a constant number of elements you could get the upper bounds of the array prior to entering the outer for loop and thus speed up this algorithm by a considerable amount (upwards of 50% faster).

Good Luck
Dec 29 '09 #2
kimmelsd33
29 New Member
Thanks for the response. This code is not putting the tab delimited file back together, and I have a function that the data in the second column, needs to run through, thus creating data for another variable and column of data. This new column of data needs to be inserted behind the first column, once the file is being written to the temp file. Does that make sense? If I can place each column of data into certain variable, pertained to each column, then I believe it would be easier to run the named variable through the function, and that value stored in another variable, and inserted into the temp file in a tab delimited text file. ? Not sure, but I think the logic would work. I am having problems creating it as I need it. Does this make sense?
Dec 31 '09 #3
vb5prgrmr
305 Recognized Expert Contributor
Okay, then... Where I have "item to insert" is where you would put your function that returns a string and I think you said it does its thing from the 2nd colume so it would be...
Expand|Select|Wrap|Line Numbers
  1.   OutputText = OutputText & ElementArray(0) & vbTab & MyFunction(ElementArray(1))
  2.  
As for not reassembling the file... It works here for me but if you are wanting the first 50 lines to be reassembled back into the file then you will need another string variable... or...
Expand|Select|Wrap|Line Numbers
  1. ReDim Preserve LineArray(49) As String
  2.  
  3. FName = "OutputPathFileName" 'need to change 
  4. FNumb = FreeFile 
  5.  
  6. Open FName For Output As #FNumb 
  7. Print #FNumb, Join(LineArray, vbNewLine) & vbNewLine & OutputText 
  8. Close #FNumb 
  9.  
Not sure about the second vbNewLine...



Good Luck
Dec 31 '09 #4
Guido Geurs
767 Recognized Expert Contributor
dear,

I this a solution? see attachment

br,
Attached Files
File Type: zip Add column in TXT data file.zip (7.2 KB, 66 views)
Dec 31 '09 #5
kimmelsd33
29 New Member
Thanks again vbprgrmr. I must be doing something wrong. It is giving me an error message. This is what I have:
[code]
Public Sub Perform()
Dim FName As String, FNumb As Integer
Dim InnerForLoopCou nter As Integer, OuterForLoopCou nter As Integer
Dim LineArray() As String, ElementArray() As String
Dim InputText As String, OutputText As String

FName = "C:\DB_CUE\test file.las" 'need to change
FNumb = FreeFile

Open FName For Input As #FNumb
InputText = Input(FileLen(F Name), #FNumb)
Close #FNumb

LineArray = Split(InputText , vbNewLine)

'Skip First 50
For OuterForLoopCou nter = 50 To UBound(LineArra y)
ElementArray = Split(LineArray (ForLoopCounter ), vbTab)
OutputText = OutputText & ElementArray(0) & vbTab & CalcFPH(Element Array(1)) ' "item to insert"
For InnerForLoopCou nter = 1 To UBound(ElementA rray)
OutputText = OutputText & vbTab & ElementArray(In nerForLoopCount er)
Next InnerForLoopCou nter
OutputText = OutputText & vbNewLine
Next OuterForLoopCou nter

FName = "C:\DB_CUE\TEST 1.txt" 'need to change
FNumb = FreeFile

Open FName For Output As #FNumb
Debug.Print OutputText
Print #FNumb, OutputText
Close #FNumb
End Sub

Public Function CalcFPH() As String
CalcFtPerHr = 60 / mpf
End Function
[code]

I don't need it to reassemble adding the first 50 lines of data. I just need it to reassemble adding the extra function variable in column 2. Thanks for all your help. It is coming along though with your great help and patience. Thanks again
Dec 31 '09 #6
kimmelsd33
29 New Member
Thanks again vbprgrmr. I must be doing something wrong. It is giving me an error message. This is what I have:
Expand|Select|Wrap|Line Numbers
  1. Public Sub Perform()
  2. Dim FName As String, FNumb As Integer
  3. Dim InnerForLoopCounter As Integer, OuterForLoopCounter As Integer
  4. Dim LineArray() As String, ElementArray() As String
  5. Dim InputText As String, OutputText As String
  6.  
  7. FName = "C:\DB_CUE\testfile.las" 'need to change
  8. FNumb = FreeFile
  9.  
  10. Open FName For Input As #FNumb
  11. InputText = Input(FileLen(FName), #FNumb)
  12. Close #FNumb
  13.  
  14. LineArray = Split(InputText, vbNewLine)
  15.  
  16. 'Skip First 50
  17. For OuterForLoopCounter = 50 To UBound(LineArray)
  18. ElementArray = Split(LineArray(ForLoopCounter), vbTab)
  19. OutputText = OutputText & ElementArray(0) & vbTab & CalcFPH(ElementArray(1)) ' "item to insert"
  20. For InnerForLoopCounter = 1 To UBound(ElementArray)
  21. OutputText = OutputText & vbTab & ElementArray(InnerForLoopCounter)
  22. Next InnerForLoopCounter
  23. OutputText = OutputText & vbNewLine
  24. Next OuterForLoopCounter
  25.  
  26. FName = "C:\DB_CUE\TEST1.txt" 'need to change
  27. FNumb = FreeFile
  28.  
  29. Open FName For Output As #FNumb
  30. Debug.Print OutputText
  31. Print #FNumb, OutputText
  32. Close #FNumb
  33. End Sub
  34.  
  35. Public Function CalcFPH() As String
  36. CalcFtPerHr = 60 / mpf
  37. End Function
  38.  
I don't need it to reassemble adding the first 50 lines of data. I just need it to reassemble adding the extra function variable in column 2. Thanks for all your help. It is coming along though with your great help and patience. Thanks again
Dec 31 '09 #7
vb5prgrmr
305 Recognized Expert Contributor
okay, with this code (which is the same)...
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Sub Command1_Click()
  4. Dim FName As String, FNumb As Integer
  5. Dim InnerForLoopCounter As Integer, OuterForLoopCounter As Integer
  6. Dim LineArray() As String, ElementArray() As String
  7. Dim InputText As String, OutputText As String
  8.  
  9. FName = "e:\temp\testfile.txt" 'need to change
  10. FNumb = FreeFile
  11.  
  12. Open FName For Input As #FNumb
  13. InputText = Input(FileLen(FName), #FNumb)
  14. Close #FNumb
  15.  
  16. LineArray = Split(InputText, vbNewLine)
  17.  
  18. 'Skip First 50
  19. For OuterForLoopCounter = 50 To UBound(LineArray)
  20.   ElementArray = Split(LineArray(OuterForLoopCounter), vbTab)
  21.   OutputText = OutputText & ElementArray(0) & vbTab & "item to insert"
  22.   For InnerForLoopCounter = 1 To UBound(ElementArray)
  23.     OutputText = OutputText & vbTab & ElementArray(InnerForLoopCounter)
  24.   Next InnerForLoopCounter
  25.   OutputText = OutputText & vbNewLine
  26. Next OuterForLoopCounter
  27.  
  28. FName = "e:\temp\output.txt" 'need to change
  29. FNumb = FreeFile
  30.  
  31. Open FName For Output As #FNumb
  32. Print #FNumb, OutputText
  33. Close #FNumb
  34.  
  35. End Sub
  36.  
the attached file is the output...

Which looks just fine (NOTE: there was one change in the code on this line (ElementArray = Split(LineArray (OuterForLoopCo unter), vbTab)) the loop counter was previously just ForLoopCounter and needed to be OuterForLoopCou nter and that is what option explicit is for to catch those undeclared variables...Too ls>Options>Requ ire Variable declaration>ok)



Good Luck
Attached Files
File Type: txt output.txt (8.1 KB, 355 views)
Jan 1 '10 #8
Guido Geurs
767 Recognized Expert Contributor
dear,

Maybe this tool will do it. see attachment
Please call me if you want any modifications.

br,
Attached Files
File Type: zip Add column in TXT data file_v2.0.0.zip (7.9 KB, 80 views)
Jan 1 '10 #9
kimmelsd33
29 New Member
The code works great. The problem I am having problems with is the "item to insert". I need the ElementArray(1) to process through a function, and return a value stored in a variable, and reinserted. I have tried a couple of ways to modify your code to make it happen, but it's not working. The function ElementArray(1) needs to go through is:
Expand|Select|Wrap|Line Numbers
  1. Function CalcFPH(ElementArray) as String
  2. CalcFPH=60/ElementArray(1)
  3. End Function
  4.  
I've tried creating a variable such as FPH to store the value of the function in, and then insert that, but my output is coming up blank. The key is to process the second column through the function and reinsert the value back into the second column when it writes the file. Other than that, it all works well. Thanks for your help
Jan 1 '10 #10

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

Similar topics

3
1557
by: waters | last post by:
I seem to have hit a snag. I am trying to add the list (l_local) as an item in the output list (l_output). "l_local" is a modified copy of the format-list "l_format", which will be updated by index position within the function. The problem occurs in the following statement block: def create_apid_list(l_format,l_values): l_output = for t_curr_line in l_values: # ...for each list in the values list
8
5479
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
6
1795
by: Skc | last post by:
I am trying to import a file using a custom VB.net procedure, but the problem is it works on a file with pure comma separation and not inverted commas and commas, i.e. it works for AAA,BBB,CCC,DDD but not for "AAA","BBB","CCC","DDD". Here is an extract from the code which needs to be modified for the """: Sub LoadTextFile(ByVal strFilePath As String) Dim oDS As New DataSet() Dim strFields As String Dim oTable As New DataTable()
3
7033
by: Ben | last post by:
Hi all - I am having a bit of trouble and thought maybe someone in this group could shed some light. Here's the skinny... I am creating an automated process to import a bunch of text files into Access. I want to avoid creating a separate "Spec" for each file (there are over 180 files) and instead want to code my own dynamic importing rules. So far it's been going fine, except for one item...
17
2083
by: freemann | last post by:
Can anyone provide example code showing how to send form results to a results page, email and a comma delimited file? Notice that I need it going to all three locations. Details: I have forms created and working. The first form the user fills out and submits. The form properties are set to Send to other: "Custom ISAPI, NSAPI, CGI, OR ASP SCRIPTING. The method is "POST" and the action is "secondpage.asp". I had to go this route because...
0
1681
by: Masa Ito | last post by:
I have pipe delimited (and comma/tab) files that I read with JET using a schema file. Occasionally a field has multiple quotes (") inside a single field - which chokes the line (the rest of the columns get read as nulls). An example of an offending line is: J" Invalid " .... This is a text field that is between the two 'pipes' so I need it read exactly as is. I have reverted to schema files, trying everything I can to overcome this...
6
22565
by: =?Utf-8?B?UmljaA==?= | last post by:
'--this code works but only reads text into one column when contains multiple cols Dim ds1x As New DataSet Dim ConStr As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\dir1A\;Extended Properties=""Text;HDR=No;FMT=Delimited\""" Dim conn1x As New OleDb.OleDbConnection(ConStr) Dim dax1 As New OleDbDataAdapter("Select * from testabc1x.txt", conn1x)
5
2331
by: RyanL | last post by:
I'm a newbie! I have a non-delimited data file that I'd like to convert to delimited. Example... Line in non-delimited file: 0139725635999992000010100534+42050-102800FM-15+1198KAIA Should be: 0139,725635,99999,2000,01,01,00,53,4,+42050,-102800,FM-15,+1198,KAIA
7
3336
by: kimmelsd33 | last post by:
I am using VB6. I want to read a tab delimited file, and assign each column value into a variable. If the variable is "-999.25", I want to make it a "0". I then want to reassemble the values, and write them to a temp text file. Starting with line 64, I have about 28 columns and x number of rows. Ex: 1000<tab>23.3<tab>-999.25<tab> etc. The value of -999.25 can be in any column. I need to replace it before it is stored in the variable. I need to...
0
9489
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
9906
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...
1
9885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
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
5172
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...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
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.