现需要同时读取多个文件文件,格式是一样的,然后将记录入库。
但在测试时发现,读第一个文件正常,到第二个时,出现了重复的记录(就是第一个文件内容还有),第三个文件依旧如此。
我的问题是:有没有办法在读完某个文件后不要再出现它的内容了。代码: For start = starthour To endhour
If start < 10 Then start = "0" & start
Close #1: Open App.Path & "\gprsfiles\" & Format(Now(), "yyyymmdd") & start & ".txt" For Input As #1
Do While Not EOF(1)
Line Input #1, files(i)
i = i + 1
Loop
Close #1
For j = 0 To i
mobile(j) = Mid(Trim(CStr(files(j))), 22, 11) '取得手机号码
mydate(j) = Mid(Trim(CStr(files(j))), 33, 8) '取得年月日
myhour(j) = Mid(Trim(CStr(files(j))), 41, 2) '取得时
myminute(j) = Mid(Trim(CStr(files(j))), 43, 2) '取得分
If mobile(j) = Text2.Text Then '只判断这个号码
List1.AddItem mobile(j) & "/" & mydate(j) & "/" & myhour(j) & "/" & myminute(j)
sql_add2 = "insert into changchun (卡号,日期,时次,分钟) values ('" & mobile(j) & "','" & mydate(j) & "','" & myhour(j) & "','" & myminute(j) & "')"
conn2.Execute (sql_add2)
End If
Next j
'----------------------------
'这里应该进行怎样的操作,将原来的所有内容清空???
'----------------------------
mobile(j) = ""
mydate(j) = ""
myhour(j) = ""
myminute(j) = ""
rs1.Close
Next start
但在测试时发现,读第一个文件正常,到第二个时,出现了重复的记录(就是第一个文件内容还有),第三个文件依旧如此。
我的问题是:有没有办法在读完某个文件后不要再出现它的内容了。代码: For start = starthour To endhour
If start < 10 Then start = "0" & start
Close #1: Open App.Path & "\gprsfiles\" & Format(Now(), "yyyymmdd") & start & ".txt" For Input As #1
Do While Not EOF(1)
Line Input #1, files(i)
i = i + 1
Loop
Close #1
For j = 0 To i
mobile(j) = Mid(Trim(CStr(files(j))), 22, 11) '取得手机号码
mydate(j) = Mid(Trim(CStr(files(j))), 33, 8) '取得年月日
myhour(j) = Mid(Trim(CStr(files(j))), 41, 2) '取得时
myminute(j) = Mid(Trim(CStr(files(j))), 43, 2) '取得分
If mobile(j) = Text2.Text Then '只判断这个号码
List1.AddItem mobile(j) & "/" & mydate(j) & "/" & myhour(j) & "/" & myminute(j)
sql_add2 = "insert into changchun (卡号,日期,时次,分钟) values ('" & mobile(j) & "','" & mydate(j) & "','" & myhour(j) & "','" & myminute(j) & "')"
conn2.Execute (sql_add2)
End If
Next j
'----------------------------
'这里应该进行怎样的操作,将原来的所有内容清空???
'----------------------------
mobile(j) = ""
mydate(j) = ""
myhour(j) = ""
myminute(j) = ""
rs1.Close
Next start
for j=0 to i
mobile(j) = ""
mydate(j) = ""
myhour(j) = ""
myminute(j) = ""
next
Dim mobilex As String, mydatex As String, myhourx As String, Myminutex As strimg, filesx As String
For start = starthour To endhour
If start < 10 Then start = "0" & start
Open App.Path & "\gprsfiles\" & Format(Now(), "yyyymmdd") & start & ".txt" For Input As #1
Do While Not EOF(1)
Line Input #1, filesx
mobilx = Mid(Trim(CStr(filesx)), 22, 11) '取得手机号码
If mobilex = Text2.Text Then '只判断这个号码
mydatex = Mid(Trim(CStr(filesx)), 33, 8) '取得年月日
myhourx = Mid(Trim(CStr(filesx)), 41, 2) '取得时
Myminutex = Mid(Trim(CStr(filesx)), 43, 2) '取得分
List1.AddItem mobilex & "/" & mydatex & "/" & myhourx & "/" & Myminutex
sql_add2 = "insert into changchun (卡号,日期,时次,分钟) values ('" & mobilex & "','" & mydatex & "','" & myhourx & "','" & Myminutex & "')"
conn2.Execute (sql_add2)
End If
Loop
Close #1
Next
End Sub
Private Sub Command1_Click()
Dim x(3) As strimg, filesx As String
For start = starthour To endhour
If start < 10 Then start = "0" & start
Open App.Path & "\gprsfiles\" & Format(Now(), "yyyymmdd") & start & ".txt" For Input As #1
Do While Not EOF(1)
Line Input #1, filesx
x(0) = Mid(Trim(CStr(filesx)), 22, 11) '取得手机号码
If x(0) = Text2.Text Then '只判断这个号码
x(1) = Mid(Trim(CStr(filesx)), 33, 8) '取得年月日
x(2) = Mid(Trim(CStr(filesx)), 41, 2) '取得时
x(3) = Mid(Trim(CStr(filesx)), 43, 2) '取得分
List1.AddItem Join(x, "/")
sql_add2 = "insert into changchun (卡号,日期,时次,分钟) values ('" & Join(x, "','") & "')"
conn2.Execute (sql_add2)
End If
Loop
Close #1
Next
End Sub
写的不是很精典,但总体解决问题了,若有改进意见还麻烦大家提出。
Private Sub Command1_Click() '读取文件入库,某时间段
On Error Resume Next Dim conn2 As New ADODB.Connection
Dim rs2 As ADODB.Recordset
Dim s2 As String, i2 As Long
conn2.CursorLocation = 3
conn2.CommandTimeout = 30
conn2.Open "Provider=SQLOLEDB;Data Source=172.21.86.182;User ID=sa;Password=;Initial Catalog=data;Persist Security Info=True"
sql1 = "select * from changchun"
Set rs1 = conn2.Execute(sql1)
For start = starthour To endhour
i = 0
If start < 10 Then start = "0" & start '这里主要是考虑读取的文件名,高位补0
If Dir(App.Path & "\gprsfiles\" & Format(Now(), "yyyymmdd") & start & ".txt") <> "" Then
Close #1: Open App.Path & "\gprsfiles\" & Format(Now(), "yyyymmdd") & start & ".txt" For Input As #1
Do While Not EOF(1)
Line Input #1, files(i)
mobile_boyd = Mid(Trim(CStr(files(i))), 22, 11)
mydate_boyd = Mid(Trim(CStr(files(i))), 33, 8)
myhour_boyd = Mid(Trim(CStr(files(i))), 41, 2)
myminute_boyd = Mid(Trim(CStr(files(i))), 43, 2)
If mobile_boyd = Text2.Text Then
List1.AddItem mobile_boyd & "/" & mydate_boyd & "/" & myhour_boyd & "/" & myminute_boyd
sql_add2 = "insert into changchun (卡号,日期,时次,分钟) values ('" & mobile_boyd & "','" & mydate_boyd & "','" & myhour_boyd & "','" & myminute_boyd & "')"
conn2.Execute (sql_add2)
End If
i = i + 1
Loop
Close #1
DoEvents
Else
List1.AddItem "----------------------------------"
List1.AddItem "gprsfiles目录下" & Format(Now(), "yyyymmdd") & start & ".txt" & "文件不存在!"
List1.AddItem "----------------------------------"
End If
rs1.Close
Next start
Set conn2 = Nothing
End Sub