If Right(a, 4) = ".txt" Then
......
......
End If
Option Explicit
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 Long, DirCount As Long)
'KPD-Team 1999
'E-Mail: KPDTeam@Allapi.net
'URL: http://www.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
List1.Refresh
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 I&, FileSize&, NumFiles&, NumDirs&, SearchPath$, NumFiles2&, NumDirs2&
Screen.MousePointer = vbHourglass
List1.Clear
For I = Asc("A") To Asc("Z")
SearchPath = Chr(I) & ":\"
FileSize = FindFilesAPI(SearchPath, "*.txt", NumFiles, NumDirs)
NumFiles2 = NumFiles2 + NumFiles: NumDirs2 = NumDirs2 + NumDirs
Next
DoEvents
MsgBox "Найдено " & NumFiles2 & " файлов *.txt в " & NumDirs2 + 1 & " папках"
Screen.MousePointer = vbNormal
End Sub
примерно так:
поочередно перебераешь все файлы в папке или на диске
и заносишь имя каждого файла в переменную например а
далее пишешь:
Код:
If Right(a, 4) = ".txt" Then
......
......
End If
A.A.Z. писал(а):Добавь кнопку и ListBox
- Код: Выделить всё
Option Explicit
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 Long, DirCount As Long)
'KPD-Team 1999
'E-Mail: KPDTeam@Allapi.net
'URL: http://www.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
List1.Refresh
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 I&, FileSize&, NumFiles&, NumDirs&, SearchPath$, NumFiles2&, NumDirs2&
Screen.MousePointer = vbHourglass
List1.Clear
For I = Asc("A") To Asc("Z")
SearchPath = Chr(I) & ":"
FileSize = FindFilesAPI(SearchPath, "*.txt", NumFiles, NumDirs)
NumFiles2 = NumFiles2 + NumFiles: NumDirs2 = NumDirs2 + NumDirs
Next
DoEvents
MsgBox "Найдено " & NumFiles2 & " файлов *.txt в " & NumDirs2 + 1 & " папках"
Screen.MousePointer = vbNormal
End Sub
tyomitch писал(а):Заменить вызов List1.AddItem на требуемое действие.
ну смотря когда применять - можно ведь и сразу во время поиска файл грохать, а можно просто в лист добавлять с путем (я не вчитывался в код, возможно там так и делается) и дальше пребирая лист делать kill каждому элементу для удаления или чего атм еще такого хочется уже после нахождения...Saturn.65 писал(а):А как сделать, чтоб при нахождении всех файлов с нужным расширением можно было бы применить к ним какое нибудь действие, например удалить?
Private Sub Command1_Click()
SSpat = "D:" & "\" & "*.txt" & RaS
sFile = Dir$(SSpat)
While sFile <> ""
List1.AddItem sFile 'сюда добавит имена файлов
sFile = Dir$
Wend
End Sub
for x=0 to List1.ListCount-1
kill List1.text 'файл уже никогда не вернется...
next x
Wolfie писал(а):
- Код: Выделить всё
for x=0 to List1.ListCount-1
kill List1.text 'файл уже никогда не вернется...
next x
Dim X as Integer
if msgbox("Правда удалить? ведь не восстановите потом! " _
,vbQuestion or vbYesNoCancel or vbdefaultButton3 _
,"Удаление...") = vbYes then
for x=0 to List1.ListCount-1
kill List1.text 'файл уже никогда не вернется...
next x
end if
Wolfie писал(а):маленькая рекомендация - вместо "" для пустой строки использовать лучше vbNullString
Wolfie писал(а)::D в процедуру кнопки "удалить", или туда где будет такая процедура... еще не забудь про то что Dim X as Integer надо написать перед циклом, и пользователя спросить... короче так вот:
- Код: Выделить всё
Dim X as Integer
if msgbox("Правда удалить? ведь не восстановите потом! " _
,vbQuestion or vbYesNoCancel or vbdefaultButton3 _
,"Удаление...") = vbYes then
for x=0 to List1.ListCount-1
kill List1.text 'файл уже никогда не вернется...
next x
end if
kill List1.text 'файл уже никогда не вернется...
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 33