|
Hi all,
I want to select a folder from FolderBrowserDialog and write into a textbox and append all the files within the folder shown in the textbox as one file in the same folder...
Code sofar:
Private Sub BtnFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFolder.Click
Dim MyFolderBrowser As New System.Windows.Forms.FolderBrowserDialog
MyFolderBrowser.Description = "Select the Folder"
MyFolderBrowser.ShowNewFolderButton = False
Dim dlgResult As DialogResult = MyFolderBrowser.ShowDialog()
If dlgResult = Windows.Forms.DialogResult.OK Then
Textbox1.Text = MyFolderBrowser.SelectedPath
End If
End Sub
The above code opens a FolderBrowserDialog and allows to select a folder and the selected path is copied to the textbox.
Now, i want to know how to append all the files within the selected folder and write as one new textfile by clicking a button.
Thanks
Vijay
|
|
|
|
|
File.ReadAllText will read any file, you can concatenate strings.
Christian Graus
Driven to the arms of OSX by Vista.
"I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
|
|
|
|
|
|
Three things.
1 The Button1_Click should be
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AppendFiles()
End Sub
You need to tell it that Button1_Click_1 handles the Click event.
2 This is a hodge-podge of VB.Net and VBScript. You don't need all that FSO stuff. Have a look at reading and writing textfiles on MSDN or help system.
3 Be careful with the folder path. It needs to end with '\' before you join the filename, but that's not what is in the textbox.
Having said that the basic idea seems sound, it just won't work. It's relying on e.g. MainFile being a variant which does not exist in VB.NET. (Look at the two assignments: ones a string the other a file object.)
It won't even compile if you have Option Explicit and Option Strict set (as they ought to be).
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
|
The line AppendFiles(TextBox1.Text, "c:\DB\append\Append.txt") just passes the folder into the sub not the files in the folder.
You also need to loop through the files in the folder. Something like this:
Dim theDir As DirectoryInfo = new DirectoryInfo(iFile);
Dim datFiles As FileInfo() = theDir.GetFiles("*.txt");
Using sw As StreamWriter = New StreamWriter(oFile, True, System.Text.Encoding.Default)
For Each (fi As FileInfo In datFiles)
Using sa As StreamReader = New StreamReader(fi.FullName, System.Text.Encoding.Default)
'read and write text
End Using
Next
End Using
Syntax might not be quite right - copied and edited from C# code - but the general idea is ok.
This is what your friend's Do...Loop was trying to do.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Im getting 2 errors in this line
For Each ( fi As FileInfo In datFiles)
1)fi not declared
2)')' expected
I declared fi as string but still there is an error.
|
|
|
|
|
Should be For Each fi As FileInfo in datFiles i.e. no ( or ).
That's what happens when you copy and paste from C# which requires ( and )
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Thanks a lot I have written the code for Read and write and its working fine now.
I need to write the file creation date for the appended files.
Code I wrote:
GetCreationTime(iFile)
The creation time for each time is different, but its writing the same date for all the files...
Is there anyother method that could help to write the file creation date and time???
|
|
|
|
|
This would get creation time for file
For Each fi As FileInfo In datFiles
Dim created As DateTime = fi.CreationTime
Using sa As StreamReader = New StreamReader(fi.FullName, System.Text.Encoding.Default)
'read and write text
End Using
Next
I don't know where you're calling GetCreationTime() but it looks like you are passing in the folder and not the files.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Thanks a lot, that works fine.
Im using CreationTime() while writing the file.
|
|
|
|
|
|
vijay2482 wrote: If the header is found again in the next file
Does this mean that there may not be a header in a file? Or should it be When the header is found ?
In the first case (i.e. IF) then you have no guarantee that the first file read will contain a header so you need to do something like this.
Dim theDir As DirectoryInfo = new DirectoryInfo(iFile);
Dim datFiles As FileInfo() = theDir.GetFiles("*.txt");
Using sw As StreamWriter = New StreamWriter(oFile, True, System.Text.Encoding.Default)
'Write the header lines to output file
For Each fi As FileInfo In datFiles
Using sa As StreamReader = New StreamReader(fi.FullName, System.Text.Encoding.Default)
'read line from input
If 'header line' Then
'Skip it
Else
'write to output
End If
End Using
Next
End Using
You can write the header either by reading a special header.txt file that only contains header lines (and is not in the folder you're searching) or by hard coding the text as strings. The first way is better because if the headers change you don't need to change the code just the txt file.
If it's the second case (i.e. all files have header, you could do something like this.
Dim theDir As DirectoryInfo = new DirectoryInfo(iFile);
Dim datFiles As FileInfo() = theDir.GetFiles("*.txt");
Using sw As StreamWriter = New StreamWriter(oFile, True, System.Text.Encoding.Default)
Dim skipHeader as Boolean = False
For Each fi As FileInfo In datFiles
Using sa As StreamReader = New StreamReader(fi.FullName, System.Text.Encoding.Default)
'read input line
If 'header line' And Not skipHeader Then
'write output line - first line of header
'read input line - second line of header
'write output line
skipHeader = True
Else
'write output line
End If
End Using
Next
End Using
Note that this assumes there's only one or two header lines.
Note also that this is pseudocode mixed with VB not pure VB.
There's two problems with this
vijay2482 wrote: if name.contains("Nom du Modéle") or if name.contains("----") then
else
'read and write
End If
1 You don't need the second if.
2 When you find a header you still need to do a read, the best way is to do the read then the test for header as in the two bits I've done.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
|
Look at what WriteHeader() does and where it originally came from. You're not doing the same thing here.
In this code you have not done a read of the first file so there's nothing to write.
I think some of the problems come from you trying to hack one program (i.e. the one that did parent and child for a single file) into another one that concatenates files while stripping out header lines in all but the first file. The logic of the two programs is different and you need to think about it before coding.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Here's a better solution for when all files have headers.
Dim theDir As DirectoryInfo = new DirectoryInfo(iFile);
Dim datFiles As FileInfo() = theDir.GetFiles("*.txt");
Using sw As StreamWriter = New StreamWriter(oFile, True, System.Text.Encoding.Default)
'Read and write first file including headers
Dim fi As FileInfo = datFiles(0)
Using sa As StreamReader = New StreamReader(fi.FullName, System.Text.Encoding.Default)
'read input line
'write output line
End Using
' Read and write subsequent files excluding headers
For i As Integer = 1 To datFiles.Length()-1
fi = datFiles(i)
Using sa As StreamReader = New StreamReader(fi.FullName, System.Text.Encoding.Default)
'read input line
If 'header line' Then
' skip header line - do nothing
Else
'write output line
End If
End Using
Next i
End Using
Much neater and gets rid of awful skipHeaders flag.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
How is it possible to do a read befor declaring the streamreader???
Using sw As StreamWriter = New StreamWriter(oFile, True, System.Text.Encoding.Default)
'Read and write first file including headers
Dim fi As FileInfo = datFiles(0)
Using sa As StreamReader = New StreamReader(fi.FullName, System.Text.Encoding.Default)
'read input line
'write output line
End Using
|
|
|
|
|
'Read and write first file including headers
is a comment - if you read the code you'll see that it does declare sa at the appropriate place.
The comments in the using blocks do need to be replaced by code.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
|
Here's an annotated version of your code.
You need to think about the logic of the code rather than just hacking.
Roughly it is something like this NOTE this is not VB it's just looks a bit like VB:
Fill the pf array
Using streamreader for first file
read and write two header lines
Loop while data lines
read and write data line
End Loop
End Using 'Closes streamreader
For second and subsequent files in folder
Using streamreader for first file
Loop while input lines
read input line
If header line Then
'do nothing
else
'write data line
End If
End Loop
End Using 'Closes streamreader
Next
Unless you get the logic clear you'll keep stumbling around wondering why things don't work or give the output you expect.
'-------------------------------------------------------------
'Change the parameter name iFile to something more meaningful
'It's not the file name but the folder name
'--------------------------------------------------------------
Private Sub AppendFiles(ByVal iFile As String, ByVal oFile As String)
Dim temp As String
Dim name As String
Dim comp As String
Dim desF As String
Dim desAF As String
Dim desA As String
Dim desAA As String
Dim ecn As String
Dim dnf As String
Dim repDNF As String
Dim repASM As String
Dim pF(10) As Integer
Dim NbOfSpaces As Integer = 0
Dim theDir As DirectoryInfo = New DirectoryInfo(iFile)
Dim datFiles As FileInfo() = theDir.GetFiles("*.txt")
Using sw As StreamWriter = New StreamWriter(oFile, True, System.Text.Encoding.Default)
'--------------------------------------------------------------------------------------------
' Call FillPF sub here, or move the code marked XXXX to here (I've done that). You don't need any files to do what it does
'--------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------
'XXXX Why is this being done 0 to 10 times? All it does is assign entries to the pf Array
'that's why I did it as a sub called FillPF() and got rid of the loop and it can be done above
For i = 0 To 10
pF(0) = temp.IndexOf("Nom du Modèle")
pF(1) = temp.IndexOf("AEC_COMPATIBILITY")
pF(2) = temp.IndexOf("AEC_STANDARD_DESCRIPTION_FRENCH")
pF(3) = temp.IndexOf("AEC_FREE_DESCRIPTION_FRENCH")
pF(4) = temp.IndexOf("AWW_STANDARD_DESCRIPTION")
pF(5) = temp.IndexOf("AEC_FREE_DESCRIPTION_ENGLISH")
pF(6) = temp.IndexOf("AEC_ECN")
pF(7) = temp.IndexOf("DNF")
pF(8) = temp.IndexOf("REP_DNF")
pF(9) = temp.IndexOf("REP_ASM")
Next
'---------------------------------------------------------------------------------------------
'Read and write first file including headers
'*** iFile is the path, we've been through this before; StreamReader reads a file it can't read a folder
'*** Change the name to something like inputPath and it will be less misleading.
Using sa As StreamReader = New StreamReader(iFile, System.Text.Encoding.Default)
'-----------------------------------------------------------------------
'This block reads and writes the two header lines in the first file
'-----------------------------------------------------------------------
temp = sa.ReadLine()
name = temp.Substring(0, pF(1))
comp = temp.Substring(pF(1), pF(2) - pF(1))
desF = temp.Substring(pF(2), pF(3) - pF(2))
desAF = temp.Substring(pF(3), pF(4) - pF(3))
desA = temp.Substring(pF(4), pF(5) - pF(4))
desAA = temp.Substring(pF(5), pF(6) - pF(5))
ecn = temp.Substring(pF(6), pF(7) - pF(6))
dnf = temp.Substring(pF(7), pF(8) - pF(7))
repDNF = temp.Substring(pF(8), pF(9) - pF(8))
repASM = temp.Substring(pF(9), temp.Length - pF(9))
sw.WriteLine(name & comp & desF & desAF & desA & desAA & ecn & dnf & repDNF & repASM & "CREATED_DATE")
sw.WriteLine(sa.ReadLine())
'-----------------------------------------------------------------------
End Using
'------------------------------------------------------------------------------------------
' This opens the first file for reading, you can't write the headers until you've done this
' and you have to take into account the two lines you attempt to read above
'i.e. you must read and write them before the loop reads the data lines
'So you need to move these two lines to before the block that reads the header lines.
'Also get rid of the Using and End Using above.
'------------------------------------------------------------------------------------------
Dim fi1 As FileInfo = datFiles(0)
Using sa As StreamReader = New StreamReader(fi1.FullName, System.Text.Encoding.Default)
'------------------------------------------------------------------------------------------
While sa.Peek() >= 0
temp = sa.ReadLine()
name = temp.Substring(0, pF(1))
comp = temp.Substring(pF(1), pF(2) - pF(1))
desF = temp.Substring(pF(2), pF(3) - pF(2))
desAF = temp.Substring(pF(3), pF(4) - pF(3))
desA = temp.Substring(pF(4), pF(5) - pF(4))
desAA = temp.Substring(pF(5), pF(6) - pF(5))
ecn = temp.Substring(pF(6), pF(7) - pF(6))
dnf = temp.Substring(pF(7), pF(8) - pF(7))
repDNF = temp.Substring(pF(8), pF(9) - pF(8))
repASM = temp.Substring(pF(9), temp.Length - pF(9))
sw.WriteLine(name & comp & desF & desAF & desA & desAA & ecn & dnf & repDNF & repASM & Created)
End While
End Using
' Read and write subsequent files excluding headers
For i As Integer = 1 To datFiles.Length() - 1
fi1 = datFiles(i)
Using sa As StreamReader = New StreamReader(fi1.FullName, System.Text.Encoding.Default)
'read input line
While sa.Peek() >= 0
temp = sa.ReadLine()
name = temp.Substring(0, pF(1))
comp = temp.Substring(pF(1), pF(2) - pF(1))
desF = temp.Substring(pF(2), pF(3) - pF(2))
desAF = temp.Substring(pF(3), pF(4) - pF(3))
desA = temp.Substring(pF(4), pF(5) - pF(4))
desAA = temp.Substring(pF(5), pF(6) - pF(5))
ecn = temp.Substring(pF(6), pF(7) - pF(6))
dnf = temp.Substring(pF(7), pF(8) - pF(7))
repDNF = temp.Substring(pF(8), pF(9) - pF(8))
repASM = temp.Substring(pF(9), temp.Length - pF(9))
'-------------------------------------------------------------------------
' Is this test right? It means the second line in the file will be written
' out. But will only be ------------------------------------ line.
'-------------------------------------------------------------------------
If name.Contains("Nom du Modélé") Then
' skip header line - do nothing
Else
'write output line
sw.WriteLine(name & comp & desF & desAF & desA & desAA & ecn & dnf & repDNF & repASM & created)
End If
End While
End Using
Next i
End Using
'------------------------------------------------------------
'Where did the End Sub go to? Is there more code after this?
'------------------------------------------------------------
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Thanks for the explanations of the code.
Will try and let u know...
Thanks once again...
|
|
|
|
|
Good Morning David,
My question is how to call the pF() without reading the file.
Private Sub FillPF(ByVal pF() As Integer, ByVal temp As String)
pF(0) = temp.IndexOf("Nom du Modèle")
pF(1) = temp.IndexOf("AEC_COMPATIBILITY")
pF(2) = temp.IndexOf("AEC_STANDARD_DESCRIPTION_FRENCH")
pF(3) = temp.IndexOf("AEC_FREE_DESCRIPTION_FRENCH")
pF(4) = temp.IndexOf("AWW_STANDARD_DESCRIPTION")
pF(5) = temp.IndexOf("AEC_FREE_DESCRIPTION_ENGLISH")
pF(6) = temp.IndexOf("AEC_ECN")
pF(7) = temp.IndexOf("DNF")
pF(8) = temp.IndexOf("REP_DNF")
pF(9) = temp.IndexOf("REP_ASM")
End Sub
Call pF(pF,temp)
Before calling i should read the line using temp variable like "temp=sa.ReadLine()"
Since streamreader comes after this line,its not possible to call pF()
Modified version of the Code:
Private Sub AppendFiles(ByVal iFile As String, ByVal oFile As String)
Dim temp As String
Dim name As String
Dim comp As String
Dim desF As String
Dim desAF As String
Dim desA As String
Dim desAA As String
Dim ecn As String
Dim dnf As String
Dim repDNF As String
Dim repASM As String
Dim pF(10) As Integer
Dim NbOfSpaces As Integer = 0
Dim inputline As String
Dim theDir As DirectoryInfo = New DirectoryInfo(iFile)
Dim datFiles As FileInfo() = theDir.GetFiles("*.txt")
Using sw As StreamWriter = New StreamWriter(oFile, True, System.Text.Encoding.Default)
'--------------------------------------------------------------------------------------------'
'Call FillPF sub here, or move the code marked XXXX to here (I've done that). You don't need any files to do what it does
'inputline = sa.readline()
Call FillPF(pF, inputline)
'--------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------
'Read and write first file including headers
'*** iFile is the path, we've been through this before; StreamReader reads a file it can't read a folder
'*** Change the name to something like inputPath and it will be less misleading.
'Using sa As StreamReader = New StreamReader(inputfile, System.Text.Encoding.Default)
Dim fi1 As FileInfo = datFiles(0)
Using sa As StreamReader = New StreamReader(fi1.FullName, System.Text.Encoding.Default)
'-----------------------------------------------------------------------
'This block reads and writes the two header lines in the first file
'-----------------------------------------------------------------------
temp = sa.ReadLine()
name = temp.Substring(0, pF(1))
comp = temp.Substring(pF(1), pF(2) - pF(1))
desF = temp.Substring(pF(2), pF(3) - pF(2))
desAF = temp.Substring(pF(3), pF(4) - pF(3))
desA = temp.Substring(pF(4), pF(5) - pF(4))
desAA = temp.Substring(pF(5), pF(6) - pF(5))
ecn = temp.Substring(pF(6), pF(7) - pF(6))
dnf = temp.Substring(pF(7), pF(8) - pF(7))
repDNF = temp.Substring(pF(8), pF(9) - pF(8))
repASM = temp.Substring(pF(9), temp.Length - pF(9))
sw.WriteLine(name & comp & desF & desAF & desA & desAA & ecn & dnf & repDNF & repASM & "CREATED_DATE")
sw.WriteLine(sa.ReadLine())
'-----------------------------------------------------------------------
'End Using
'------------------------------------------------------------------------------------------
' This opens the first file for reading, you can't write the headers until you've done this
' and you have to take into account the two lines you attempt to read above
'i.e. you must read and write them before the loop reads the data lines
'So you need to move these two lines to before the block that reads the header lines.
'Also get rid of the Using and End Using above.
'------------------------------------------------------------------------------------------
'Dim fi1 As FileInfo = datFiles(0)
'Using sa As StreamReader = New StreamReader(fi1.FullName, System.Text.Encoding.Default)
'------------------------------------------------------------------------------------------
While sa.Peek() >= 0
temp = sa.ReadLine()
name = temp.Substring(0, pF(1))
comp = temp.Substring(pF(1), pF(2) - pF(1))
desF = temp.Substring(pF(2), pF(3) - pF(2))
desAF = temp.Substring(pF(3), pF(4) - pF(3))
desA = temp.Substring(pF(4), pF(5) - pF(4))
desAA = temp.Substring(pF(5), pF(6) - pF(5))
ecn = temp.Substring(pF(6), pF(7) - pF(6))
dnf = temp.Substring(pF(7), pF(8) - pF(7))
repDNF = temp.Substring(pF(8), pF(9) - pF(8))
repASM = temp.Substring(pF(9), temp.Length - pF(9))
sw.WriteLine(name & comp & desF & desAF & desA & desAA & ecn & dnf & repDNF & repASM & Created)
End While
End Using
' Read and write subsequent files excluding headers
For i As Integer = 1 To datFiles.Length() - 1
fi1 = datFiles(i)
Using sa As StreamReader = New StreamReader(fi1.FullName, System.Text.Encoding.Default)
'read input line
While sa.Peek() >= 0
temp = sa.ReadLine()
name = temp.Substring(0, pF(1))
comp = temp.Substring(pF(1), pF(2) - pF(1))
desF = temp.Substring(pF(2), pF(3) - pF(2))
desAF = temp.Substring(pF(3), pF(4) - pF(3))
desA = temp.Substring(pF(4), pF(5) - pF(4))
desAA = temp.Substring(pF(5), pF(6) - pF(5))
ecn = temp.Substring(pF(6), pF(7) - pF(6))
dnf = temp.Substring(pF(7), pF(8) - pF(7))
repDNF = temp.Substring(pF(8), pF(9) - pF(8))
repASM = temp.Substring(pF(9), temp.Length - pF(9))
'-------------------------------------------------------------------------
' Is this test right? It means the second line in the file will be written
' out. But will only be ------------------------------------ line.
'-------------------------------------------------------------------------
If name.Contains("Nom du Modélé") Then
' skip header line - do nothing
ElseIf name.Contains("------") Then
Else
'write output line
sw.WriteLine(name & comp & desF & desAF & desA & desAA & ecn & dnf & repDNF & repASM & Created)
End If
End While
End Using
Next i
End Using
'------------------------------------------------------------
'Where did the End Sub go to? Is there more code after this?
End Sub
modified on Wednesday, May 6, 2009 3:35 AM
|
|
|
|
|
That was an aberration on my part - it struck me that it was wrong late last night.
I've had a think about it and there's a problem if AppendFiles is called twice in same program or the program is run when the output file already exists. You'll get the headers again on second call/run.
However, I awoke early this morning and produced this code which handles the problem.
I've not tested it but think the logic is correct. It's done as a console app and this is the complete program.
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Try
Call AppendFiles("C:\thePathToFiles\", "C:\PlayPen\AppendedJunk.txt")
Call AppendFiles("C:\WayToOtherFiles\", "C:\PlayPen\AppendedJunk.txt")
Catch ex As Exception
MsgBox("ERROR: " & ex.Message(), CType(MsgBoxStyle.OkOnly, MsgBoxStyle), "APPEND FILES FAILED")
End Try
End Sub
Private Sub AppendFiles(ByVal inputPath As String, ByVal outputFile As String)
Dim columnPositions(10) As Integer
Dim theDir As DirectoryInfo = New DirectoryInfo(inputPath)
Dim theFiles As FileInfo() = theDir.GetFiles("*.txt")
'If the output file exists it already has headers, so we only need to do headers if it does not exist
Dim doHeaders As Boolean = Not File.Exists(outputFile)
Using sw As StreamWriter = New StreamWriter(outputFile, True, System.Text.Encoding.Default)
For fileCount As Integer = 0 To theFiles.Length() - 1
Dim inputFile As String = theFiles(fileCount).FullName
Dim createdAt As Date = theFiles(fileCount).CreationTime()
Dim inputLine As String
Dim outputLine As String
Using sr As StreamReader = New StreamReader(inputFile, System.Text.Encoding.Default)
If (fileCount = 0) Then
'Read the first line of first file to set up column positions array
inputLine = sr.ReadLine()
Call GetColumnPostions(columnPositions, inputLine)
If doHeaders Then
Call WriteHeaders(sr, sw, columnPositions)
Else
'Read and throw away second header line
inputLine = sr.ReadLine()
End If
Else
' Read the header lines and throw them away
For i As Integer = 1 To 2 'Assumes 2 header lines, as does WriteHeaders
inputLine = sr.ReadLine()
Next
End If
'Now read and write the data lines
While sr.Peek() >= 0
inputLine = sr.ReadLine()
outputLine = GetOutputLine(inputLine, createdAt, columnPositions)
sw.WriteLine(outputLine)
End While
End Using
Next fileCount
End Using
End Sub
Private Sub GetColumnPostions(ByVal colPos As Integer(), ByVal s As String)
colPos(0) = s.IndexOf("Nom du Modèle")
colPos(1) = s.IndexOf("AEC_COMPATIBILITY")
colPos(2) = s.IndexOf("AEC_STANDARD_DESCRIPTION_FRENCH")
colPos(3) = s.IndexOf("AEC_FREE_DESCRIPTION_FRENCH")
colPos(4) = s.IndexOf("AWW_STANDARD_DESCRIPTION")
colPos(5) = s.IndexOf("AEC_FREE_DESCRIPTION_ENGLISH")
colPos(6) = s.IndexOf("AEC_ECN")
colPos(7) = s.IndexOf("DNF")
colPos(8) = s.IndexOf("REP_DNF")
colPos(9) = s.IndexOf("REP_ASM")
End Sub
Private Sub WriteHeaders(ByVal sa As StreamReader, ByVal sw As StreamWriter, ByVal p As Integer())
Dim temp As String = sa.ReadLine()
Dim sb As StringBuilder = BuildLine(temp, p)
sb.Append("CREATED_DATE")
sw.WriteLine(sb.ToString()) 'First header line
sw.WriteLine(sa.ReadLine()) 'Second header line
End Sub
Private Function GetOutputLine(ByVal s As String, ByVal cd As Date, ByVal p As Integer()) As String
Dim sb As StringBuilder = BuildLine(s, p)
sb.Append(cd.ToString()) 'creation date
GetOutputLine = sb.ToString()
End Function
Private Function BuildLine(ByVal s As String, ByVal p As Integer()) As StringBuilder
Dim sb As StringBuilder = New StringBuilder("")
sb.Append(s.Substring(0, p(1))) 'name
sb.Append(s.Substring(p(1), p(2) - p(1))) 'comp
sb.Append(s.Substring(p(2), p(3) - p(2))) 'desF
sb.Append(s.Substring(p(3), p(4) - p(3))) 'desAF
sb.Append(s.Substring(p(4), p(5) - p(4))) 'desA
sb.Append(s.Substring(p(5), p(6) - p(5))) 'desAA
sb.Append(s.Substring(p(6), p(7) - p(6))) 'ecn
sb.Append(s.Substring(p(7), p(8) - p(7))) 'dnf
sb.Append(s.Substring(p(8), p(9) - p(8))) 'repDNF
sb.Append(s.Substring(p(9), s.Length - p(9))) 'repASM
BuildLine = sb
End Function
End Module
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Thanks a lot for supporting me so much...
You have spent ur precious time in helping me.
I have never seen a person who helps somebody who is not know well before.
The below is the sample of the output i got from ur previous version of the code:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------CREATED_DATE
01530501.ASM -- UNITE CARROSSERIE CARROSSERIE SRT2 MF HOOD HOOD SRT2 MF F9000507
01530501_SKEL.PRT -- -- 530 UNIT SKEL UNIT SKEL -- F999999 -- -- -- 06/05/2009 10:24:16
4281484.PRT M1 SUPPORT CARROSSERIE SRT2 SUPPORT INF GBA15 & CVT-ML160 SRT2 HOOD SUPPORT LOWER SUPPORT GBA15 & CVT-ML160 F8546130 12289 A -- 06/05/2009 10:24:16
3009508X1.PRT -- -- VIS H, M12x40, 8.8 VIS A TETE HEXAGONALE -- -- 12289 B -- 06/05/2009 10:24:16
.
.
.
And on more question:
If the field spacing ie., GetColumnPosition() varies for each file how to handle it?
Is it possible to rearange the position, if the second files position of the fields are higher than the previous one?
Can the rearranging of the file be done based on the field position with more spacing file?
Thanks once again for helping me...
|
|
|
|
|
I have modified the code to write the first header line as follows and it works fine.
If (fileCount = 0) Then
'Read the first line of first file to set up column positions array
inputLine = sr.ReadLine()
sw.WriteLine(inputLine) ' first header line
Call GetColumnPostions(columnPositions, inputLine)
If doHeaders Then
Call WriteHeaders(sr, sw, columnPositions)
Else
'Read and throw away second header line
inputLine = sr.ReadLine()
End If
Else
.
.
Private Sub WriteHeaders(ByVal sa As StreamReader, ByVal sw As StreamWriter, ByVal p As Integer())
Dim temp As String = sa.ReadLine()
Dim sb As StringBuilder = BuildLine(temp, p)
'sb.Append("CREATED_DATE")
sw.WriteLine(sb.ToString())
'Second header line
'First header line
'sw.WriteLine(sa.ReadLine())
End Sub
Thanks for the code.
|
|
|
|
|