Click here to Skip to main content
15,889,867 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Append ALL Textfiles within a folder to a Single Textfile Pin
riced5-May-09 1:18
riced5-May-09 1:18 
GeneralRe: Append ALL Textfiles within a folder to a Single Textfile Pin
vijay24825-May-09 1:59
vijay24825-May-09 1:59 
GeneralRe: Append ALL Textfiles within a folder to a Single Textfile Pin
riced5-May-09 2:12
riced5-May-09 2:12 
GeneralMessage Closed Pin
5-May-09 4:04
vijay24825-May-09 4:04 
GeneralRe: Append ALL Textfiles within a folder to a Single Textfile Pin
riced5-May-09 6:47
riced5-May-09 6:47 
GeneralRe: Append ALL Textfiles within a folder to a Single Textfile Pin
vijay24825-May-09 10:24
vijay24825-May-09 10:24 
GeneralRe: Append ALL Textfiles within a folder to a Single Textfile [modified] Pin
vijay24825-May-09 21:29
vijay24825-May-09 21:29 
GeneralRe: Append ALL Textfiles within a folder to a Single Textfile Pin
riced5-May-09 22:08
riced5-May-09 22:08 
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. Smile | :) 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

GeneralRe: Append ALL Textfiles within a folder to a Single Textfile Pin
vijay24825-May-09 22:40
vijay24825-May-09 22:40 
GeneralRe: Append ALL Textfiles within a folder to a Single Textfile Pin
vijay24825-May-09 23:02
vijay24825-May-09 23:02 
GeneralRe: Append ALL Textfiles within a folder to a Single Textfile Pin
riced5-May-09 23:35
riced5-May-09 23:35 
GeneralRe: Append ALL Textfiles within a folder to a Single Textfile Pin
vijay24825-May-09 23:53
vijay24825-May-09 23:53 
GeneralRe: Append ALL Textfiles within a folder to a Single Textfile Pin
vijay24826-May-09 0:08
vijay24826-May-09 0:08 
GeneralRe: Append ALL Textfiles within a folder to a Single Textfile Pin
Dave Kreskowiak4-May-09 4:07
mveDave Kreskowiak4-May-09 4:07 
QuestionCOM port Pin
Subjugate3-May-09 21:07
Subjugate3-May-09 21:07 
AnswerRe: COM port [modified] Pin
Wankel Maggot3-May-09 21:48
Wankel Maggot3-May-09 21:48 
AnswerRe: COM port Pin
Bharat Jain3-May-09 21:50
Bharat Jain3-May-09 21:50 
GeneralRe: COM port Pin
Subjugate4-May-09 15:00
Subjugate4-May-09 15:00 
QuestionVisual Data Manager Pin
KULKING3-May-09 21:03
KULKING3-May-09 21:03 
AnswerRe: Visual Data Manager Pin
Henry Minute4-May-09 2:45
Henry Minute4-May-09 2:45 
Questioncan picture boxes be mathmatically different then they appear Pin
Wankel Maggot3-May-09 20:34
Wankel Maggot3-May-09 20:34 
AnswerRe: can picture boxes be mathmatically different then they appear Pin
Christian Graus3-May-09 22:19
protectorChristian Graus3-May-09 22:19 
GeneralRe: can picture boxes be mathmatically different then they appear Pin
Wankel Maggot3-May-09 22:30
Wankel Maggot3-May-09 22:30 
GeneralRe: can picture boxes be mathmatically different then they appear Pin
Christian Graus3-May-09 22:48
protectorChristian Graus3-May-09 22:48 
AnswerRe: can picture boxes be mathmatically different then they appear Pin
Luc Pattyn3-May-09 23:05
sitebuilderLuc Pattyn3-May-09 23:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.