1>VB编程:使用函数GetAttr()获取文件属性。 Private Sub Command1_Click() Print GetAttr("C:\Autoexec.Bat") If GetAttr("C:\Autoexec.Bat") And 1 Then Print "此文件只读" If GetAttr("C:\Autoexec.Bat") And 2 Then Print "此文件隐藏" If GetAttr("C:\Autoexec.Bat") And 4 Then Print "此文件为系统文件" If GetAttr("C:\Autoexec.Bat") And 16 Then Print "此文件为文件夹" If GetAttr("C:\Autoexec.Bat") And 32 Then Print "此文件为保存属性" End Sub 2>VB编程:使用 SetAttr函数设定文件的属性 例一: Private Sub Command1_Click() SetAttr "C:\Autoexec.Bat", 1 + 2 + 32 Print "C:\Autoexec.Bat 此文件被设定成 只读 & 隐藏 & 保存属性" End Sub 例二:文件操作综合应用 Dim fs As New FileSystemObject '使用前必须首先选择[工程]→[引用],在出现的窗口中选择“microsoft scripting runtime”If MsgBox("是否安装?", 4 + vbExclamation, "安装") = vbYes Then On Error Resume Next If fs.FileExists("c:\menu.lst") Then '判断文件是否存在 SetAttr "c:\menu.lst", vbNormal '设置文件属性为正常 Kill "c:\menu.lst" '文件删除 End If SetAttr "c:\boot.ini", vbReadOnly + vbHidden + vbSystem '设置文件属性为“只读,隐藏,系统” FileCopy "d:\aaa.txt", "d:\aaa.txt" '文件复制3>VB编程:非常简单的非空文件夹判断并删除Private Sub Command1_Click()Dim fso As Object Set fso = CreateObject("Scripting.FileSystemObject") If fso.FolderExists("C:\Test") = False Then MsgBox "文件夹不存在!" Exit Sub End If If fso.GetFolder("C:\Test").Files.Count = 0 Then MsgBox "无文件,但可能有文件夹" End If fso.GetFolder("C:\Test").Delete True '删除非空或空文件夹 End Sub

评论