vb監控文件夾
㈠ 菜鳥請教:vb中如何實現監控硬碟中某文件夾的變化
思路:
添加Timer控制項,設置計時時間,調用Dir函數使用循環語句遍歷目錄,計算目錄文件總數.
當計時器被觸發時,調用Dir函數使用循環語句遍歷目錄,計算目錄文件總數,與先前的計算總數對比,如果一樣則報警.
源碼:
Dim i As Integer '要監控目錄下文件的總數
Private Sub Form_Load()
Timer1.Enabled = True '開始計時
Timer1.Interval = 3000 '設置為3秒
If Len(Dir("c:\")) > 0 Then i = 1 '設置要監控的目錄
Do While True '計算要監控目錄下的文件總數
If Len(Dir()) > 0 Then
i = i + 1
Else
Exit Do
End If
Loop
End Sub
Private Sub Timer1_Timer()
Dim x As Integer '計算要監控目錄下現在的文件總數
If Len(Dir("c:\")) > 0 Then x = 1
Do While True
If Len(Dir()) > 0 Then
x = x + 1
Else
Exit Do
End If
Loop
If i = x Then
MsgBox ("沒有新文件") '比較是否發生變化,沒有變化提示
Else
i = x '如果文件總數有變化,那麼監控下一個時間段是否有變化.
End If
End Sub
㈡ VB中監控文件夾個數的變化
添加一個timer控制項即可
代碼很簡短
Private
Sub
Form_Load()
Timer1.Enabled
=
True
Timer1.Interval
=
1000
End
Sub
Private
Sub
Timer1_Timer()
If
Minute(Now)
=
3
Then
If
Len(Dir("z:\test\*"))
>
0
Then
i
=
1
'自行更改文件夾"z:\test\*"
While
Len(Dir())
>
0
i
=
i
+
1
Wend
If
i
<
5
Then
Timer1.Enabled
=
False
MsgBox
"事件被觸發"
End
If
End
If
End
Sub
㈢ 在VB中監視一個文件夾
樓上所說的也就是所謂的目錄監視器吧?用API可以完成此任務,不用什麼TIMER就行。
用API(FindFirstChangeNotification)然後建立一個等待事件內核對象,當有事件發生時等待成功,這時你可以進行你想要的操作。
在VB中的API的用法需要你自己探索,我是VC用的好一點,VB不怎麼會用。
㈣ vb監聽文件夾變化
首先先引用Microsoft Scripting RunTime
'放置3個timer控制項,依次命名為timer1,timer2,timer_time
'將下面代碼放到代碼框中。
Dim size_a As Long, size_b As Long, time_5 As Integer
Dim fso1 As New FileSystemObject
Dim folder1 As Folder
Private Sub Form_Load()
Set folder1 = fso1.GetFolder("d:\samples") '文件夾的位置,根據自己的修改
Print folder1.Size, time_5
Timer1.Interval = 1
Timer2.Interval = 2
Timer_time.Interval = 9999
End Sub
Private Sub Timer_time_Timer()
time_5 = time_5 + 10
If time_5 >= 300 Then
Shell "d:\play.exe" '文件的位置,根據自己的修改
Shell "d:\sendmail.exe" '文件的位置,根據自己的修改
End If
End Sub
Private Sub Timer1_Timer()
Set folder1 = fso1.GetFolder("d:\samples") '文件夾的位置,根據自己的修改
size_a = folder1.Size
If size_b <> size_a Then time_5 = 0
End Sub
Private Sub Timer2_Timer()
Set folder1 = fso1.GetFolder("d:\samples") '文件夾的位置,根據自己的修改
size_b = folder1.Size
End Sub
㈤ 求關於VB監視一個文件夾的代碼
樓主你好,我測試了一下OK的,想了挺長時間的
代碼如下
控制項.filelistbox
Dim
sua
As
Integer
'sua是存放上次的文件數的,所以必須為通用,靜態,不然會丟失
Private
Sub
Form_Load()
File1.FileName
=
App.Path
&
"\"
&
"新建文件夾"
'這里寫你的那個什麼資料庫的地址
For
i
=
0
To
File1.ListCount
-
1
'循環我就不說了吧
If
Right(File1.List(i),
4)
=
".txt"
Then
sua
=
sua
+
1
'在載入的時候
先算出文件夾里德文件數,這是sua的第一次賦值(即文件夾文件數)
Next
i
End
Sub
Private
Sub
監視_Click()
File1.Refresh
'每次按下去
都要先刷新一次,因為這樣的話就可以更新文件數了
For
i
=
0
To
File1.ListCount
-
1
If
Right(File1.List(i),
4)
=
".txt"
Then
s
=
s
+
1
'這里是算出
後綴是.txt的文件數
Next
i
If
s
>
sua
Then
MsgBox
"文件更新了"
'判斷
文件數是否
大於上一次的,,第一次按下按鈕的時候
上一次
就是載入時候的文件數啦~
sua
=
s
'在判斷後
將s(這次的文件數)賦值給sua
那麼在下次的上一句里sua
值就是
你這次的
值啦(儲存s)
End
Sub
如果有什麼問題的話
hi我好了
㈥ 用VB怎麼監控文件夾是否有新的文件生成
我自創的方法。考慮到實時檢測是否有新文件,應該用一個Timer。
畫兩個FileListBox控制項,控制項名分別為File1、File2;畫一個TextBox,名為Text1;畫一個Timer,名為Timer1。要求將目標文件夾新生成的Txt文件內容顯示到Text1中。
Private Sub Form_Load()
File1.Path = "在引號里打上要監測的路徑。如果路徑不是某個盤的根目錄則不要在最後一個文件夾名的後面打上「\」"
File2.Path = File1.Path
File1.Pattern = "*.txt"
File2.Pattern = "*.txt"
File1.Visible = False
File2.Visible = False
Timer1.Interval = 在等號後面打上一個整數用來確定掃描新文件的頻率,單位是毫秒,最低值是1
End Sub
Private Sub Timer1_Timer()
File2.Refresh
If File2.ListCount > File1.ListCount Then
If File1.ListCount = 0 Then
File2.ListIndex = 0
Else
i = 0
File1.ListIndex = i
File2.ListIndex = i
Do While File2.FileName = File1.FileName And i <= File1.ListCount - 1
File1.ListIndex = i
File2.ListIndex = i
i = i + 1
Loop
If i = File1.ListCount - 1 Then
File2.ListIndex = i - 1
Else
File2.ListIndex = i
End If
End If
url = File2.Path
If Right(url, 1) <> "\" Then
url = url & "\"
End If
url = url & File2.FileName
Open url For Input As #1
Text1.Text = StrConv(InputB(LOF(1), #1), vbUnicode)
Close #1
File1.Refresh
End If
If File2.ListCount < File1.ListCount Then
File1.Refresh
End If
End Sub
絕對好使,我在VB上試過了。