This question is locked. New answers and comments are not allowed.
Hello everyone,
I write the first time in this forum! I have write the code bellow and it worked well for two years. I must write that I really like linq but I don't have the opportunity to work very often with linq because I primary am a DB developer. So, the code bellow reads a text file, splits it in columns and then the Linq query selects and groups the second column value. The propose for this code is to split the files based the value of the second column. I want to add a second column to that grouping, the column 1. The target is to have this result -->
column1_second_value_column2_first_value.txt
column1_second _value_column2_second_value.txt ...etc
Can anyone help me?
Thank you!!!
I write the first time in this forum! I have write the code bellow and it worked well for two years. I must write that I really like linq but I don't have the opportunity to work very often with linq because I primary am a DB developer. So, the code bellow reads a text file, splits it in columns and then the Linq query selects and groups the second column value. The propose for this code is to split the files based the value of the second column. I want to add a second column to that grouping, the column 1. The target is to have this result -->
column1_second_value_column2_first_value.txt
column1_second _value_column2_second_value.txt ...etc
Can anyone help me?
Thank you!!!
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="FileDelimiter" value=";" />
<add key="FilePreffix" value="invoice_" />
<add key="IndexGroup" value="2" />
</appSettings>
</configuration>
Sub splittFiles()
Dim FileIn As String
Dim bPath As String
bPath = AppSettings.Get("BackupPath") & DateFormat(Today, "_")
If Dir(bPath) = "" Then Directory.CreateDirectory(bPath)
FileIn = Dir(AppSettings.Get("InputPath") & "*.txt", vbDirectory)
Try
Do While FileIn <> ""
Dim fileA As String() = System.IO.File.ReadAllLines(AppSettings.Get("InputPath") & FileIn)
' Concatenate and remove duplicate names based on
Dim mergeQuery As IEnumerable(Of String) = fileA
' Group the names by the Invoice number
Dim groupQuery = From name In mergeQuery _
Let n = name.Split(New Char() {(AppSettings.Get("FileDelimiter").ToString)}) _
Group By groupKey = n(CType(AppSettings.Get("IndexGroup").ToString, Integer)) _
Into groupName = Group
' Create a new file for each group that was created
' Note that nested foreach loops are required to access
' individual items with each group.
For Each gGroup In groupQuery
Dim fileName As String = AppSettings.Get("OutputPath") & AppSettings.Get("FilePreffix") & Trim(gGroup.groupKey) & ".txt"
''If the File Already exists add Date to the end of the file
If File.Exists(fileName) Then
fileName = AppSettings.Get("OutputPath") & AppSettings.Get("FilePreffix") & Trim(gGroup.groupKey) _
& Year(Now) & Month(Now) & Day(Now) & "_" & Hour(Now) & Minute(Now) & Second(Now) & ".txt"
End If
Dim sw As New System.IO.StreamWriter(fileName)
Console.WriteLine(gGroup.groupKey)
For Each item In gGroup.groupName
Console.WriteLine(" " & item.name)
sw.WriteLine(item.name)
Next
sw.Close()
Next
File.Move(AppSettings.Get("InputPath") & FileIn, bPath & "\" & FileIn.Replace(".txt", "_") _
& Year(Now) & Month(Now) & Day(Now) & "_" & Hour(Now) & Minute(Now) & Second(Now) & ".txt")
FileIn = Dir()
Loop
Catch ex As Exception
SendMail(ex.Message)
End Try
End Sub
<configuration>
<appSettings>
<add key="FileDelimiter" value=";" />
<add key="FilePreffix" value="invoice_" />
<add key="IndexGroup" value="2" />
</appSettings>
</configuration>
Sub splittFiles()
Dim FileIn As String
Dim bPath As String
bPath = AppSettings.Get("BackupPath") & DateFormat(Today, "_")
If Dir(bPath) = "" Then Directory.CreateDirectory(bPath)
FileIn = Dir(AppSettings.Get("InputPath") & "*.txt", vbDirectory)
Try
Do While FileIn <> ""
Dim fileA As String() = System.IO.File.ReadAllLines(AppSettings.Get("InputPath") & FileIn)
' Concatenate and remove duplicate names based on
Dim mergeQuery As IEnumerable(Of String) = fileA
' Group the names by the Invoice number
Dim groupQuery = From name In mergeQuery _
Let n = name.Split(New Char() {(AppSettings.Get("FileDelimiter").ToString)}) _
Group By groupKey = n(CType(AppSettings.Get("IndexGroup").ToString, Integer)) _
Into groupName = Group
' Create a new file for each group that was created
' Note that nested foreach loops are required to access
' individual items with each group.
For Each gGroup In groupQuery
Dim fileName As String = AppSettings.Get("OutputPath") & AppSettings.Get("FilePreffix") & Trim(gGroup.groupKey) & ".txt"
''If the File Already exists add Date to the end of the file
If File.Exists(fileName) Then
fileName = AppSettings.Get("OutputPath") & AppSettings.Get("FilePreffix") & Trim(gGroup.groupKey) _
& Year(Now) & Month(Now) & Day(Now) & "_" & Hour(Now) & Minute(Now) & Second(Now) & ".txt"
End If
Dim sw As New System.IO.StreamWriter(fileName)
Console.WriteLine(gGroup.groupKey)
For Each item In gGroup.groupName
Console.WriteLine(" " & item.name)
sw.WriteLine(item.name)
Next
sw.Close()
Next
File.Move(AppSettings.Get("InputPath") & FileIn, bPath & "\" & FileIn.Replace(".txt", "_") _
& Year(Now) & Month(Now) & Day(Now) & "_" & Hour(Now) & Minute(Now) & Second(Now) & ".txt")
FileIn = Dir()
Loop
Catch ex As Exception
SendMail(ex.Message)
End Try
End Sub