文件夹属性问题

heshl 2002-12-09 04:09:05
如何用VB读取文件夹的完整属性,我只知道用GetFileInformationByHandle()和GetFileAttributes()函数,但不知道具体用法,请教各位大虾
...全文
105 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
heshl 2002-12-10
  • 打赏
  • 举报
回复
要获得文件或文件夹的大小该如何做 最顶上的那种方法可以实现,但我只成功了一次,再次用的时候就不行了,不知错在那里,没有提示
junwhj 2002-12-09
  • 打赏
  • 举报
回复

SetAttr 语句


为一个文件设置属性信息。

语法

SetAttr pathname, attributes

SetAttr 语句的语法含有以下这些命名参数:

部分 描述
pathname 必要参数。用来指定一个文件名的字符串表达式,可能包含目录或文件夹、以及驱动器。
Attributes 必要参数。常数或数值表达式,其总和用来表示文件的属性。


设置值

attributes 参数设置可为:

常数 值 描述
vbNormal 0 常规(缺省值)
VbReadOnly 1 只读。
vbHidden 2 隐藏。
vbSystem 4 系统文件
vbArchive 32 上次备份以后,文件已经改变


注意 这些常数是由 VBA 所指定的,在程序代码中的任何位置,可以使用这些常数来替换真正的数值。

说明

如果想要给一个已打开的文件设置属性,则会产生运行时错误。
junwhj 2002-12-09
  • 打赏
  • 举报
回复

GetAttr 函数


返回一个 Integer,此为一个文件、目录、或文件夹的属性。

语法

GetAttr(pathname)

必要的 pathname 参数是用来指定一个文件名的字符串表达式。pathname 可以包含目录或文件夹、以及驱动器。

返回值

由 GetAttr 返回的值,是下面这些属性值的总和:

常数 值 描述
vbNormal 0 常规
vbReadOnly 1 只读
vbHidden 2 隐藏
vbSystem 4 系统文件
vbDirectory 16 目录或文件夹
vbArchive 32 上次备份以后,文件已经改变
vbalias 64 指定的文件名是别名。


注意 这些常数是由 VBA 指定的,在程序代码中的任何位置,可以使用这些常数来替换真正的值。

说明

若要判断是否设置了某个属性,在 GetAttr 函数与想要得知的属性值之间使用 And 运算符与逐位比较。如果所得的结果不为零,则表示设置了这个属性值。例如,在下面的 And 表达式中,如果档案 (Archive) 属性没有设置,则返回值为零:

Result = GetAttr(FName) And vbArchive

如果文件的档案属性已设置,则返回非零的数值。
heshl 2002-12-09
  • 打赏
  • 举报
回复
FSO是什么,我不懂
OFFICE之门 2002-12-09
  • 打赏
  • 举报
回复
用FSO不是可以吗?
cngxylyh 2002-12-09
  • 打赏
  • 举报
回复
给你说明和一个例子

Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
判断指定文件的属性
Long,-1表示出错。如返回包含了标志的一个Long值,则指定文件的属性。其中的标志对应于带有FILE_ATTRIBUTE_???前缀的常数。具体参考BY_HANDLE_FILE_INFORMATION结构的File Attribute Types
table表格lpFileName ----- String,指定欲获取属性的一个文件的名字

'Create a form with a command button (command1), a list box (list1)
'and four text boxes (text1, text2, text3 and text4).
'Type in the first textbox a startingpath like c:\
'and in the second textbox you put a pattern like *.* or *.txt

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Const MAX_PATH = 260
Const MAXDWORD = &HFFFF
Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Function StripNulls(OriginalStr As String) As String
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If
StripNulls = OriginalStr
End Function

Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
'KPD-Team 1999
'E-Mail: KPDTeam@Allapi.net

Dim FileName As String ' Walking filename variable...
Dim DirName As String ' SubDirectory Name
Dim dirNames() As String ' Buffer for directory name entries
Dim nDir As Integer ' Number of directories in this path
Dim I As Integer ' For-loop counter...
Dim hSearch As Long ' Search Handle
Dim WFD As WIN32_FIND_DATA
Dim Cont As Integer
If Right(path, 1) <> "\" Then path = path & "\"
' Search for subdirectories.
nDir = 0
ReDim dirNames(nDir)
Cont = True
hSearch = FindFirstFile(path & "*", WFD)
If hSearch <> INVALID_HANDLE_VALUE Then
Do While Cont
DirName = StripNulls(WFD.cFileName)
' Ignore the current and encompassing directories.
If (DirName <> ".") And (DirName <> "..") Then
' Check for directory with bitwise comparison.
If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
dirNames(nDir) = DirName
DirCount = DirCount + 1
nDir = nDir + 1
ReDim Preserve dirNames(nDir)
End If
End If
Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
Loop
Cont = FindClose(hSearch)
End If
' Walk through this directory and sum file sizes.
hSearch = FindFirstFile(path & SearchStr, WFD)
Cont = True
If hSearch <> INVALID_HANDLE_VALUE Then
While Cont
FileName = StripNulls(WFD.cFileName)
If (FileName <> ".") And (FileName <> "..") Then
FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
FileCount = FileCount + 1
List1.AddItem path & FileName
End If
Cont = FindNextFile(hSearch, WFD) ' Get next file
Wend
Cont = FindClose(hSearch)
End If
' If there are sub-directories...
If nDir > 0 Then
' Recursively walk into them...
For I = 0 To nDir - 1
FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(I) & "\", SearchStr, FileCount, DirCount)
Next I
End If
End Function
Sub Command1_Click()
Dim SearchPath As String, FindStr As String
Dim FileSize As Long
Dim NumFiles As Integer, NumDirs As Integer
Screen.MousePointer = vbHourglass
List1.Clear
SearchPath = Text1.Text
FindStr = Text2.Text
FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
Screen.MousePointer = vbDefault
End Sub

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧