|
-
Jul 2nd, 2001, 03:02 AM
#1
Thread Starter
Addicted Member
finding directories and subdirectories
I need to write a programm that finds all the directories on a my c-drive with a certain name. For example I need to find all directory or subdirectorynames which end with \temp. On my drive I have like c:\temp, c:\games\temp and c:\internet\temp. I would like to write a programm that lists these 3 directories in say a listbox.
I need some hints.
Anybody????
Last edited by reznor; Jul 2nd, 2001 at 04:56 AM.
"Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination." - Albert Einstein
-
Jul 2nd, 2001, 04:55 AM
#2
Hyperactive Member
I have been slagged off before in this forum for mentioning this, but here I go again !!
scrrun.DLL is the Microsoft Scripting Runtime library which neatly packages alot of Disk/DIR & file utilities. Once you investigate some of the functions it provides, you may well find it invaluable.
Also, as it's shipped with Windows, you only need reference it, & not package it with your app.
There are lots of different methods & functions, the one you may find useful is FolderExists
FATBOY

Best Bar.....
-
Jul 2nd, 2001, 05:03 AM
#3
Thread Starter
Addicted Member
Thanx!
Thanx,
I can loop through the folders collection to solve my problem!!
"Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination." - Albert Einstein
-
Jul 2nd, 2001, 05:04 AM
#4
Conquistador
I have done this in C++, but I will try and translate some of it into vb for u
-
Jul 2nd, 2001, 07:14 AM
#5
Thread Starter
Addicted Member
Damn, still can't get it to work. That scripting stuff is better used for VBScript in html.
What I would like is the functionality every windows PC has. I mean under start->find->Files or Folders... This little programm gives me almost what I want. In it you select a drive and in the advanced tab you select 'of type: folder' and it displays all directories on that drive.
This programm uses API calls I think, but I can't get FindFirstFile and FindNextFile to work to mimick it.
If I could get a listbox with all directory names of a drive then I could work from that.
Help needed!
"Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination." - Albert Einstein
-
Jul 2nd, 2001, 07:22 AM
#6
Hyperactive Member
reznor,
At the risk of sounding 'churlish', would the commondialog not do it for you ?
the Scripting runtime does use the API's but perhaps what you want to do is a tad deeper. Try:
www.vbapi.com/ref/f/findfirstfile.html
unless you have already.
or for NextFile:
www.vbapi.com/ref/f/Findnextfile.html
Each of which are full vb snippets. I've used both before to great success though I cannot find the VBP at the mo, otherwise I'd sent it on.
FATBOY.

Best Bar.....
-
Jul 2nd, 2001, 07:27 AM
#7
Hyperactive Member

Best Bar.....
-
Jul 2nd, 2001, 07:30 AM
#8
Hyperactive Member

Best Bar.....
-
Jul 2nd, 2001, 07:32 AM
#9
Thread Starter
Addicted Member
thanks,
I use www.vbapi.com a lot, but I had some problems with the WIN32_FIND_DATA type. It contains another type called FILETIME. I got a compile error. Does it matter which type is declared first? Haven't tried that yet.
The commondialog will not do.
My problem is this. On a network drive, somewhere deep down in the directory structure there are a few subdir's with identical names. What I need to build is a little programm that does a filecount on those subdir's and gives a total. I want to make it generic, so they can give a diectory name, and the programm searches the drive and counts all the files that are in a directory with that name. They just don't want to do a manual count.
Hope it's a little more clear.
"Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination." - Albert Einstein
-
Jul 2nd, 2001, 07:34 AM
#10
Thread Starter
Addicted Member
FATBOYPEE,
thanx for the links, looks promising!
"Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination." - Albert Einstein
-
Jul 2nd, 2001, 10:13 AM
#11
Registered User
Try this.
In a module:
VB Code:
Option Explicit
Private Const MAX_PATH = 260
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End 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 * 14
End Type
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
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 hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Dirs() As String
Public Function FindDirectories(ByVal Path As String, DirName As String) As Variant
'Nucleus
ReDim Dirs(1 To 1)
Call EnumDirs(Path, DirName)
If Len(Dirs(1)) Then FindDirectories = Dirs Else FindDirectories = Null
Erase Dirs
End Function
Private Sub EnumDirs(ByVal Path As String, DirName As String)
Dim hFirstFound As Long
Dim hFound As Long
Dim WFD As WIN32_FIND_DATA
Dim fname As String
If Right$(Path, 1) <> "\" Then Path = Path & "\"
hFirstFound = FindFirstFile(Path & "*.*", WFD)
hFound = (hFirstFound > 0)
Do While hFound
fname = Left(WFD.cFileName, InStr(1, WFD.cFileName, Chr(0)) - 1)
If WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then
If fname <> "." And fname <> ".." Then
Call EnumDirs(Path & fname, DirName)
If fname = DirName Then
If Len(Dirs(1)) Then ReDim Preserve Dirs(1 To UBound(Dirs) + 1)
Dirs(UBound(Dirs)) = Path & fname
End If
End If
End If
hFound = FindNextFile(hFirstFound, WFD)
Loop
End Sub
Public Function EnumerateFiles(ByVal Path As String) As Variant
Dim hFirstFound As Long
Dim hFound As Long
Dim WFD As WIN32_FIND_DATA
Dim fname As String
Dim Files() As String: ReDim Files(1 To 1)
If Right$(Path, 1) <> "\" Then Path = Path & "\"
hFirstFound = FindFirstFile(Path & "*.*", WFD)
hFound = (hFirstFound > 0)
Do While hFound
fname = Left(WFD.cFileName, InStr(1, WFD.cFileName, Chr(0)) - 1)
If fname <> "." And fname <> ".." Then
If Len(Files(1)) Then ReDim Preserve Files(1 To UBound(Files) + 1)
Files(UBound(Files)) = Path & fname
End If
hFound = FindNextFile(hFirstFound, WFD)
Loop
If Len(Files(1)) Then EnumerateFiles = Files Else EnumerateFiles = Null
End Function
Example usage:
VB Code:
Private Sub Command1_Click()
Dim a
Dim i&
a = FindDirectories("c:\tmp", "Custom Titlebar")
If Not IsNull(a) Then
For i = 1 To UBound(a)
Debug.Print a(i)
Next
Else
Debug.Print "No directories"
End If
End Sub
What this does is find and list directories with the same name.
Once you have this you can pass each directory to the EnumerateFiles function I included which enumerates all the files in a directory.
Now you have all the code you need.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|