用VB编写自动备份
文件程序 -------------------------------------- Public Sub BackupFile(Filename As String, Drive As String, Folder As String) Dim Fso As New FileSystemObject '创建 FSO 对象实例 Dim Dest_path As String, Counter As Long Dim StrDay As String, StrMonth As String, NewFilename As String Counter = 0 Do While Counter < 6 '如果
驱动器没准备好,继续检测。共检测 6 秒 Counter = Counter + 1 Call Waitfor(1) '间隔 1 秒 If FsoDrives(Drive)IsReady = True Then Exit Do End If Loop If FsoDrives(Drive)IsReady = False Then '6 秒后
目标盘仍未准备就绪,退出 MsgBox " 目标驱动器 " & Drive & " 没有准备好! ", vbCritical Exit Sub End If If FsoGetDrive(Drive)FreeSpace < FsoGetFile(Filename)Size Then MsgBox "目标驱动器空间太小!", vbCritical '目标驱动器空间不够,退出 Exit Sub End If If Right(Drive, 1) <> ":" Then Drive = Drive & ":" End If If Left(Folder, 1) <> "\" Then Folder = "\" & Folder End If If Right(Folder, 1) <> "\" Then Folder = Folder & "\" End If If Day(Date) < 10 Then StrDay = "0" & Day(Date) Else StrDay = Day(Date) End If If Month(Date) < 10 Then StrMonth = "0" & Month(Date) Else StrMonth = Month(Date) End If 'FsoFileExists () NewFilename = "newfilename" Dest_path = Drive & Folder If Not FsoFolderExists(Dest_path) Then '如果目标文件夹不存在,创建之 FsoCreateFolder Dest_path End If FsoCopyFile Filename, Dest_path & NewFilename, True '拷贝,直接覆盖同名文件 Set Fso = Nothing End Sub Private Sub Waitfor(Delay As Single) '延时过程,Delay 单位约为 1 秒 Dim StartTime As Single StartTime = Timer Do Until (Timer - StartTime) > Delay Loop End Sub Private Sub Form_Load() BackupFile "filename", "d:", "filebackupdir" End End Sub添加一个timer控件,多长时间做一次自己设置,hour(time)=18(设置每天在18点是进行备份,如果要没星期一就改为week(date),自己设置),语法:
FileCopy
source,
destination
例子:
Private
Sub
Timer1_Timer()
If
hour(Time)=18
Then
FileCopy
"D:\Base\db1mdb","E:\CopyBase"
'将D:\Base\里的db1mdb数据库拷贝到E:\CopyBase里。
End
If
End
SubVB工程引用的某个文件丢失。vb组态软件的安装包里面包含很多个文件,工程引用的某个文件丢失,会引起其他莫名其妙的错误,包括没有库。VisualBasic(简称VB)是Microsoft公司开发的一种通用的基于对象的程序设计语言,为结构化的、模块化的、面向对象的、包含协助开发环境的事件驱动为机制的可视化程序设计语言,是一种可用于微软自家产品开发的语言。
评论列表(0条)