 |
|
จะให้โปรแกรมอ่าน textfile โดยสามารถกำหนดคำที่เราต้องการให้อ่านได้ |
|
 |
|
|
 |
 |
|
ลองศึกษาเรื่อง regular expression ดูครับ
หรือเอาแบบง่ายๆ
Code (VB.NET)
dim A As Boolean
dim B As Boolean
if source.IndexOf("คำค้น1") < 0
then A = False
else A = True
end if
if source.IndexOf("คำค้น2") < 0
then B = False
else B = True
end if
if (A=True) and (B=True)
then
....
end if
|
 |
 |
 |
 |
Date :
2017-10-27 18:01:42 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับขอบคุณครับ แต่ผมก็ยังติดตรง เราจะกำหนดให้ว่า เมื่อเจอคำนี้ก็อ่านแค่บรรทัดนี้ยังไงครับ เพราะว่า ตอนดึงมาแสดง มันเอามาอ่านหมดเลยครับ ที่วนลูปมา
|
 |
 |
 |
 |
Date :
2017-10-28 14:06:47 |
By :
bankguszo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ แต่ผมไล่มาครึ่งวันแล้วก็ไม่ได้ครับ ผมทำไม่ได้เอง คือผมจะกำหนดยังไงว่า ถ้าเจอ keyword นี้ ในบรรทัดท้ายๆของ array (ที่มี keyword) จะให้แสดงบรรทัดนั้นยังไงครับ เพราะข้อมูลผมไม่สามารถกำหนด array ตายตัวได้ครับ
Code (VB.NET)
Dim fileinfo As New System.IO.FileStream("D:\CountOutput.txt", IO.FileMode.Open)
Dim Reader As New System.IO.StreamReader(fileinfo)
Dim List As New List(Of String)
Do While Reader.Peek > 0
List.Add(Reader.ReadLine)
Loop
Dim Array1 As String() = List.ToArray
fileinfo.Close()
Reader.Close()
fileinfo = New System.IO.FileStream("D:\CountOutput.txt", IO.FileMode.Open)
Reader = New System.IO.StreamReader(fileinfo)
Dim sArray As String()
Dim Index As Integer
Do While Reader.Peek > 0
ReDim Preserve sArray(Index)
sArray(Index) = Reader.ReadLine
Index += 1
Loop
Dim i As Integer
For i = 0 To UBound(sArray)
Label20.Visible = True
Label20.Text = sArray(i)
Next
fileinfo.Close()
Reader.Close()
End Sub
|
ประวัติการแก้ไข 2017-10-30 15:44:43 2017-10-30 15:45:00 2017-10-30 16:01:34
 |
 |
 |
 |
Date :
2017-10-30 15:43:02 |
By :
bankguszo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Dim fileinfo = New System.IO.FileStream("D:\CountOutput.txt", IO.FileMode.Open)
Dim Reader = New System.IO.StreamReader(fileinfo)
Dim sArray As String()
Dim Index As Integer
Dim tmp As String
Do While Reader.Peek > 0
tmp = Reader.ReadLine;
if tmp.IndexOf("คำค้น1") >= 0
then
begin
ReDim Preserve sArray(Index)
sArray(Index) = tmp
Index += 1
end
end if
Loop
Dim i As Integer
Dim c As Integer = sArray.Length-1;
For i = 0 To c
Label20.Visible = True
Label20.Text = sArray(i)
Next
fileinfo.Close()
Reader.Close()
|
 |
 |
 |
 |
Date :
2017-10-30 19:45:34 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ข้อมูลใน "D:\CountOutput.txt format จะประมาณนี้ครับ
Box. G1709/02918 10/27/2017 2:55:33 PM
จำนวน 504 10/27/2017 2:55:48 PM
สแกนยังไม่ครบ Box G1709/02918 24/504 10/27/2017 2:58:57 PM
Box. G1709/02918 10/27/2017 3:10:03 PM
ผมต้องการดึงตัวแดงมาแสดงผลในโปรแกรมครับ เหมือนเซฟไว้ แล้วเรียกมาใช้งานต่อได้โดยกำหนดให้ใส่เลข Gxxxx/xxxxx เพื่อดึงมาใช้งานต่อ เพราะเลข Gxxxx/xxxxx จะไม่ซํ้ากันครับ หรือถ้าซํ้ากันก็ให้อ่านค่าล่าสุดครับ (Gxxxx/xxxxx = เลขกล่อง / 24/504 คือจำนวนของครับ )
|
ประวัติการแก้ไข 2017-10-31 07:58:15
 |
 |
 |
 |
Date :
2017-10-31 07:56:08 |
By :
bankguszo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จัดไปครับ
Code (VB.NET)
Dim strRegex as String = "(สแกนยังไม่ครบ\s?Box\s?G\d{4}/\d{5}\s?\d{2}/\d{3})" //หรือ "(.*?Box\s?G\d{4}/\d{5}\s?\d{2}/\d{3})"
Dim myRegex As New Regex(strRegex, RegexOptions.None)
Dim strTargetString As String = My.Computer.FileSystem.ReadAllText("D:\CountOutput.txt",System.Text.Encoding.UTF32)
For Each myMatch As Match In myRegex.Matches(strTargetString)
messagebox.show(myMatch.Group[1].Value)
Next
มันจะได้ออกมาเป็นแบบนี้เลย

Regex จะจัดการ ข้อความได้ง่ายกว่า string ครับ ลองศึกษาดู
คำค้น vb.net Regex Match
|
 |
 |
 |
 |
Date :
2017-10-31 08:26:59 |
By :
Dr.K |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|