我正在尝试将几个逗号分隔的文件合并为一个,它正在逃避我。我甚至没有开始查看第3组文件,因为我甚至无法获得前2个文件。
" "" "," Style1"," Style1描述"," OK",10,10,0,120,120,0," WH& #34;," WHITE"
" "" "," Style2"," Style2描述"," OK",0,0,0,0,0,0,&# 34; WH"," WHITE"
" "" "," Style3"," Style3描述"," OK",0,0,0,0,0,0,&# 34; WH"," WHITE"
" "" "," Style4"," Style4描述"," OK",0,0,0,0,0,0,&# 34; WH"," WHITE"
" "" "," Style5"," Style5描述"," OK",0,0,0,0,0,0,&# 34; WH"," WHITE"
" "" "," Style6"," Style6描述"," OK",0,0,0,0,0,0,&# 34; WH"," WHITE"
蓝紫魅力:
" "" "," SA"," Part1"," Part1描述",2,""," W + 0.25"" H"" {R}"" 0",0,"",12,1 ""
" "" "," SA"," Part2"," Part2描述",2,""," H + 0.25"" H"" {R}"" 0",0,"",12,1 ""
" "" "," SA"," Part3"," Part3描述",1,""," W- 3"" L"" {R}"" 0",0,"",3,1, ""
" "" "," SA"," Part4"," Part4描述",1,""," W- 3"" L"" {R}"" 0",0,"",3,1, ""
节日礼物:
" "" "," SA"," Part1"," Part1描述",2,""," W + 0.25"" H"" {R}"" 0",0,"",12,1 ""
" "" "," SA"," Part2"," Part2描述",2,""," H + 0.25"" H"" {R}"" 0",0,"",12,1 ""
" "" "," SA"," Part3"," Part3描述",1,""," W- 3"" L"" {R}"" 0",0,"",3,1, ""
" "" "," SA"," Part4"," Part4描述",1,""," W- 3"" L"" {R}"" 0",0,"",3,1, ""
" "" "," SA"," Part5"," Part5描述",1,""," H- 3.75"" L"" {R}"" 0",0,"",3,1 ""
" "" "," SA"," Part6"," Part6描述",1,""," H- 3.75"" L"" {R}"" 0",0,"",3,1 ""
" "" "," SA"," Part7"," Part7描述",1,""," W&# 34;," L"" {R}"" 0",0,"",3,1,&# 34;"
青青草地:
" "" "," SA"," Part1"," Part1描述",2,""," W + 0.25"" H"" {R}"" 0",0,"",12,1 ""
" "" "," SA"," Part2"," Part2描述",2,""," H + 0.25"" H"" {R}"" 0",0,"",12,1 ""
" TAN"
" Part1WH"" Part1TN"
" Part2WH"" Part2TN"
" Part3WH"" Part3TN"
正如您所看到的,他们都依赖于“Style *”来确定哪些要添加到一行。 这是预期的结果:
Style1,Style1描述,白色
Style2,Style2描述,白色,SA,第1部分描述,2,,W + .25,H,{R},0,0 ,, 12,1
Style2,Style2描述,白色,SA,第1部分描述,2,,W + .25,H,{R},0,0 ,, 12,1
Style2,Style2描述,TAN,SA,Part1描述,2,,W + .25,H,{R},0,0 ,, 12,1
Style2,Style2描述,WHITE,SA,Part2描述,2,,H + .25,H,{R},0,0 ,, 12,1
Style2,Style2描述,TAN,SA,Part2描述,2,,H + .25,H,{R},0,0 ,, 12,1
Style2,Style2描述,WHITE,SA,Part3描述,1,W-3,L,{R},0,0,,3,1
Style2,Style2描述,TAN,SA,Part3描述,1,W-3,L,{R},0,0,,3,1
Style2,Style2描述,WHITE,SA,Part4描述,1,W-3,L,{R},0,0,,3,1
Style3,Style3描述,WHITE,SA,Part1描述,2,,W + .25,H,{R},0,0 ,, 12,1
Style3,Style3描述,WHITE,SA,Part2描述,2,,H + .25,H,{R},0,0 ,, 12,1
Style3,Style3描述,WHITE,SA,Part3描述,1,W-3,L,{R},0,0,,3,1
Style3,Style3描述,WHITE,SA,Part4描述,1,W-3,L,{R},0,0,,3,1
Style3,Style3描述,WHITE,SA,Part5描述,1,,H-3.75,L,{R},0,0,,3,1
Style3,Style3描述,WHITE,SA,Part6描述,1,,H-3.75,L,{R},0,0,,3,1
Style3,Style3描述,WHITE,SA,Part7描述,1,W,L,{R},0,0,,3,1
Style4,Style4描述,白色
Style5,Style5描述,WHITE,SA,Part1描述,2,,W + .25,H,{R},0,0 ,, 12,1
Style5,Style5描述,WHITE,SA,Part2描述,2,,H + .25,H,{R},0,0 ,, 12,1
Style6,Style6描述,白色
文件实际上相当大,所以我需要最传出的方法来做到这一点。 我试过了,所有的尝试都结束了。我有循环问题,我跳过线和重复行。 在这次尝试中,我试图写出2个临时文件,然后尝试将它们组合起来。我的结果不准确。 我怎样才能做到这一点?
这是我现在的代码:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Declare files we are working with
Dim inputFileIndex As String = "D:\Testing\StyleDir\Index.dat"
Dim inputFileParts As String = "D:\Testing\StyleDir\"
Dim OutFileResult As String = "D:\Testing\Output\Result.txt"
Dim outTempFile1 As String = "D:\Testing\Output\Temp1.txt"
Dim outTempFile2 As String = "D:\Testing\Output\Temp2.txt"
'Declare Cleanup
Dim fileToDelete As String
'Delcare Readers and Writers
Dim sw1 As StreamWriter
Dim sw2 As StreamWriter
'Declare Variables for File 1
Dim linesFile1 As String()
Dim styleLine1 As String
Dim linetoWrite1 As String
Dim c As Integer = 1
'Declare Variables for File 2
Dim fileEntries As String() = Directory.GetFiles(inputFileParts, "*")
Dim fileName As String
Dim fileName2
Dim ignoreName As String = "D:\Testing\StyleDir\Index.dat"
Dim elementsOfFileParts As String()
Dim linetoWrite2 As String
Dim fileLoophold As String
'Declare for loop
Dim l As Integer = 1
Dim l2 As Integer = 2
Dim temp1Hold As String
Dim temp2Hold As String
'*****************************************************************************'
'*****************************************************************************'
'Create BackUp of Result.txt if it exists
'*****************************************************************************'
'*****************************************************************************'
If System.IO.File.Exists(OutFileResult) = True Then
My.Computer.FileSystem.CopyFile(OutFileResult, "D:\Testing\Output\Result.bck", overwrite:=True)
End If
'*****************************************************************************'
'*****************************************************************************'
'Delete Existing Files
'*****************************************************************************'
'*****************************************************************************'
fileToDelete = OutFileResult
If System.IO.File.Exists(fileToDelete) = True Then
System.IO.File.Delete(fileToDelete)
End If
fileToDelete = outTempFile1
If System.IO.File.Exists(fileToDelete) = True Then
System.IO.File.Delete(fileToDelete)
End If
fileToDelete = outTempFile2
If System.IO.File.Exists(fileToDelete) = True Then
System.IO.File.Delete(fileToDelete)
End If
'*****************************************************************************'
'*****************************************************************************'
'Read In Index.dat parse and write out temp file
'*****************************************************************************'
'*****************************************************************************'
Using sr As New Microsoft.VisualBasic.FileIO.TextFieldParser(inputFileIndex)
sr.TextFieldType = FileIO.FieldType.Delimited
sr.SetDelimiters(",")
While Not sr.EndOfData
Try
linesFile1 = sr.ReadFields()
For Each currentfield In linesFile1
Select Case c
Case 3
styleLine1 = currentfield
Case 4
styleLine1 = styleLine1 & "," & currentfield
Case 13
styleLine1 = styleLine1 & "," & currentfield
End Select
linetoWrite1 = styleLine1
c += 1
Next
sw1 = My.Computer.FileSystem.OpenTextFileWriter(outTempFile1, True)
sw1.WriteLine(linetoWrite1)
sw1.Close()
c = 1
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("line in Index.Dat = " & ex.Message & "is not valid and will be skipped.")
End Try
End While
End Using
'Get file names
For Each fileName In fileEntries
'Loop to get each file name
fileName2 = fileName.Remove(0, 20)
'MsgBox(fileName)
'Skip Index.dat
If fileName2.indexof(ignoreName.Remove(0, 20), 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
'Ignore file
Else
'Loop to parse out double quotes and read in comma delmited file
Using sr2 As New Microsoft.VisualBasic.FileIO.TextFieldParser(fileName)
sr2.TextFieldType = FileIO.FieldType.Delimited
sr2.SetDelimiters(",")
c = 1
While Not sr2.EndOfData
Try
elementsOfFileParts = sr2.ReadFields()
For Each currentfield In elementsOfFileParts
Select Case c
Case 3
linetoWrite2 = fileName2 & "," & currentfield
Case 5
linetoWrite2 = linetoWrite2 & "," & currentfield
Case 6
linetoWrite2 = linetoWrite2 & "," & currentfield
Case 7
linetoWrite2 = linetoWrite2 & "," & currentfield
Case 8
linetoWrite2 = linetoWrite2 & "," & currentfield
Case 9
linetoWrite2 = linetoWrite2 & "," & currentfield
Case 10
linetoWrite2 = linetoWrite2 & "," & currentfield
Case 11
linetoWrite2 = linetoWrite2 & "," & currentfield
Case 12
linetoWrite2 = linetoWrite2 & "," & currentfield
Case 13
linetoWrite2 = linetoWrite2 & "," & currentfield
Case 14
linetoWrite2 = linetoWrite2 & "," & currentfield
Case 15
linetoWrite2 = linetoWrite2 & "," & currentfield
End Select
c += 1
Next
sw1 = My.Computer.FileSystem.OpenTextFileWriter(outTempFile2, True)
sw1.WriteLine(linetoWrite2)
sw1.Close()
c = 1
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox(fileName & "Element in Parts file = " & ex.Message & "is not valid and will be skipped.")
End Try
End While
End Using
End If
Next
'Read through each line of Temp1
Dim linesTemp1 = IO.File.ReadAllLines(outTempFile1)
For i = 0 To linesTemp1.Length - 1
Dim lineTemp1 = linesTemp1(i)
Dim fields = lineTemp1.Split(","c)
temp1Hold = linesTemp1(i).Split(",")(0)
Dim linesTemp2 = IO.File.ReadAllLines(outTempFile2)
For j = 0 To linesTemp1.Length - 1
Dim lineTemp2 = linesTemp1(i)
Dim field2 = lineTemp2.Split(","c)
temp2Hold = linesTemp2(i).Split(",")(0)
'Write out only lines from Temp1 that do not have match in Temp2
If temp1Hold = temp2Hold Then
'Read line from temp 2 And ADD columns 2 And 3 from Index
Else
sw1 = My.Computer.FileSystem.OpenTextFileWriter(OutFileResult, True)
sw1.WriteLine(linesTemp1(i))
sw1.Close()
End If
Next
Next
Label1.Text = "Finished Writing Looping Through StyleDir"
'Loop through to remove duplicates in file result
Dim lines As New HashSet(Of String)()
'Read to file
Using sr As StreamReader = New StreamReader(OutFileResult)
Do While sr.Peek() >= 0
lines.Add(sr.ReadLine())
Loop
End Using
'Write to file
Using sw As StreamWriter = New StreamWriter(OutFileResult)
For Each line As String In lines
sw.WriteLine(line)
Next
End Using
End Sub
End Class