Method for enumerating all files in the specified directory in VB

xiaoxiao2021-03-06  113

I have a classmate asked me, I will post the code here. Module1.bas file: public const Max_path = 260

Public const file_attribute_archive = & h20public const file_attribute_hidden = & h2public const invalid_handle_value = -1

Public Type Filetime DwlowDatetime As Long DWHighDatetime As Longend Type

Public 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 * 14End Type

Public Declare Function FindfirstFile LIB "kernel32" Alias ​​"FindfirsTfilea" (Byval LPFILENAME AS STRING, LPFINDFILEDATA AS WIN32_FIND_DATA) AS Long

Public Declare Function FindnextFile LIB "kernel32" Alias ​​"FindNextFilea" (Byval Hfindeta As Win32_Find_Data) AS LONG

Public Declare Function FindClose LIB "Kernel32" (Byval Hfindfile As Long) As Long

Public Function GetFullAllFileFilter (Directory As String) As String GetFullAllFileFilter = Directory "/*.*"End FunctionfrmMain.frm File: VERSION 5.00Begin VB.Form frmMain BorderStyle = 3 'Fixed Dialog Caption =" Enum Files Demo "ClientHeight = 4770 ClientLeft = 3210 ClientTop = 1905 ClientWidth = 4920 BeginProperty Font Name = "Arial" Size = 9 Charset = 134 Weight = 400 Underline = 0 'False Italic = 0' False Strikethrough = 0 'False EndProperty LinkTopic = "Form1" MaxButton = 0' False Minbutton = 0 'false scaleheight = 4770 scalewidth = 4920 showintaskbar = 0' false begin vb.commandbutton cmdlist caption = "& enumfiles" Height = 375 left = 3600 TabINDEX = 3 TOP = 1800 Width = 1215 End Begin VB.DirListBox Dir Height = 1140 Left = 0 TabIndex = 2 Top = 480 Width = 4935 End Begin VB.DriveListBox Drive Height = 300 Left = 0 TabIndex = 1 Top = 120 Width = 4935 End Begin VB.ListBox lstFileNames HEIGHT = 2400 itemdata = "frmmain.frx"

: 0000 Left = 0 List = "frmMain.frx": 0002 TabIndex = 0 Top = 2280 Width = 4935 End Begin VB.Label Label1 Caption = "Click EnumFiles button to list all files in specified directory." Height = 495 Left = 120 TabIndex = 4 Top = 1680 Width = 3375 EndEndAttribute VB_Name = "frmMain" Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = FalseOption ExplicitPrivate Sub cmdList_Click () Dim lpFileData As WIN32_FIND_DATA 'information Dim hFindFile As save files found Long' file find handle Dim FindPattern As String 'file mode queries Dim tmp As Boolean' acquisition mode FindPattern = GetFullAllFileFilter (Dir.Path) 'finds the first file hFindFile = FindFirstFile (FindPattern, lpFileData)' If you do not find If hFindFile = INVALID_HAN DLE_VALUE THEN EXIT SUB END IF 'Clear List Lstfilenames.clear' Set Temperature Variable TMP = True Do WHILE TMP 'If the file is found, it is not a directory' (if you have a hidden file, you can find it: ' If lpFileData.dwFileAttributes And (FILE_ATTRIBUTE_ARCHIVE OR FILE_ATTRIBUTE_HIDDEN) Then ...) If lpFileData.dwFileAttributes And FILE_ATTRIBUTE_ARCHIVE Then 'add file names to the list lstFileNames.AddItem lpFileData.cFileName End If' to find the next file tmp = FindNextFile (hFindFile, lpFileData ) Loop 'All queries, close the handle Findclose HfindFileEnd Sub

PRIVATE SUB Drive_Change () Dir.path = Drive.driveEnd Sub

PRIVATE SUB FORM_LOAD () Dir.path = Drive.driveEnd Sub

转载请注明原文地址:https://www.9cbs.com/read-102592.html

New Post(0)