Click here to Skip to main content
15,885,909 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Append ALL Textfiles within a folder to a Single Textfile Pin
riced5-May-09 0:30
riced5-May-09 0:30 
GeneralMessage Closed Pin
5-May-09 1:45
vijay24825-May-09 1:45 
GeneralRe: Append ALL Textfiles within a folder to a Single Textfile Pin
riced5-May-09 2:30
riced5-May-09 2:30 
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 
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

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 
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 

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.