Checking for directories not quite working right 
Author Message
 Checking for directories not quite working right

I'm writing an app that allows a person to delete a directory chosen
by a DirListBox.  However, I don't want to allow the user to perform
the deletion if any files or directories exist in the chosen
directory.  If any exist, the subroutine is exited.  

So far I'm having no problems detecting files with the Dir command
like so:

strCheckForFiles = Dir(filAll.Path & "\" & "*.*")

However, I'm having trouble with checking for directories.  The
following code works find for getting the number of directories under
the directory chosen by the user with the DirListBox "dirAll" as long
as the person has chosen a root directory such as "C:\" or "D:\".  If
he's chosen any other directory like "C:\My Documents", the code
fails.  I'm on brain fry after several hours hitting a brick wall and
am wondering what I'm doing wrong.  If someone could point me in the
right direction I wouild be grateful.  

Here's the code:  

Sub GetDirectories()

Dim strArray() As String
Dim intDirCount As Integer
Dim strTempPathName As String
intDirCount = 0
strTempPathName = Dir(strThePath & "*.*", vbDirectory)
Do While strTempPathName <> ""
   If strTempPathName <> "." And strTempPathName <> ".." Then
       'make sure SubDir is a directory.

       If (GetAttr(strThePath & strTempPathName) And vbDirectory) =
vbDirectory Then
           intDirCount = intDirCount + 1
           ReDim Preserve strArray(intDirCount)
           strArray(intDirCount) = strTempPathName & "\"
       End If
   End If
   strTempPathName = Dir    ' Get next entry.
Loop
    MsgBox "Number of directories: " & intDirCount
End Sub

Private Sub dirAll_Change()
    strThePath = dirAll.Path
End Sub



Wed, 08 Jun 2005 08:06:37 GMT  
 Checking for directories not quite working right
On Sat, 21 Dec 2002 00:06:37 GMT, Tom_ZC

At a quick glance you may be falling into the trailing "\" problem
with strTempPathName = Dir(strThePath & "*.*", vbDirectory)

If Right$(RTrim$(strTempPathName, 1)) <> "\" Then
        strTempPathName = Rtrim$(strTempPathName) & "\"
End If

Then the trailing backslash is guaranteed whe you add the "*.*"

Sorry for being vague and top posting, but i have some lovely pancakes
to attend to...  :-p

Quote:

>I'm writing an app that allows a person to delete a directory chosen
>by a DirListBox.  However, I don't want to allow the user to perform
>the deletion if any files or directories exist in the chosen
>directory.  If any exist, the subroutine is exited.  

>So far I'm having no problems detecting files with the Dir command
>like so:

>strCheckForFiles = Dir(filAll.Path & "\" & "*.*")

>However, I'm having trouble with checking for directories.  The
>following code works find for getting the number of directories under
>the directory chosen by the user with the DirListBox "dirAll" as long
>as the person has chosen a root directory such as "C:\" or "D:\".  If
>he's chosen any other directory like "C:\My Documents", the code
>fails.  I'm on brain fry after several hours hitting a brick wall and
>am wondering what I'm doing wrong.  If someone could point me in the
>right direction I wouild be grateful.  

>Here's the code:  

>Sub GetDirectories()

>Dim strArray() As String
>Dim intDirCount As Integer
>Dim strTempPathName As String
>intDirCount = 0
>strTempPathName = Dir(strThePath & "*.*", vbDirectory)
>Do While strTempPathName <> ""
>   If strTempPathName <> "." And strTempPathName <> ".." Then
>       'make sure SubDir is a directory.

>       If (GetAttr(strThePath & strTempPathName) And vbDirectory) =
>vbDirectory Then
>           intDirCount = intDirCount + 1
>           ReDim Preserve strArray(intDirCount)
>           strArray(intDirCount) = strTempPathName & "\"
>       End If
>   End If
>   strTempPathName = Dir    ' Get next entry.
>Loop
>    MsgBox "Number of directories: " & intDirCount
>End Sub

>Private Sub dirAll_Change()
>    strThePath = dirAll.Path
>End Sub

--
Regards, Frank


Wed, 08 Jun 2005 08:34:15 GMT  
 Checking for directories not quite working right
Throw this into a form with a label (Label1) and set the path of interest in
the command click sub. The label will reflect whether the path entered
contains files or is safe to delete. To test I created a series of nested
folders:

D:\ test \ folderB \ FolderC \ FolderD \ FolderE

and set the initial search point to d:\test.

When no subfolder contained files, the method (SubsContainFiles) returned
False. By placing a files in any subfolder starting with \test or below, the
function returned True.

To use in practice, just modify the Select Case statement in the command
click routine as required.  The result is instantaneous, even when checking
over 12,000 files and subfolders (ie my XP c:\windows folder and all
subfolders).

Private Const vbDot = 46
Private Const MAX_PATH = 260
Private Const INVALID_HANDLE_VALUE = -1
Private Const vbBackslash = "\"
Private Const ALL_FILES = "*.*"

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

Private Type FILE_PARAMS
   bRecurse As Boolean
   sFileNameExt As String
   sFileRoot As String
End Type

Private Declare Function FindClose Lib "kernel32" _
  (ByVal hFindFile As Long) As Long

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 lstrlen Lib "kernel32" _
    Alias "lstrlenW" (ByVal lpString As Long) As Long

Private Sub Command1_Click()

   Dim sPath As String

   sPath = QualifyPath("D:\test\")

   Select Case SubsContainFiles(sPath)
      Case True:
         Label1.Caption = "Subfolders are NOT empty!"
      Case False:
         Label1.Caption = "Subfolders empty - safe to delete"
   End Select

End Sub

Private Function SubsContainFiles(sRoot As String) As Boolean

   Dim WFD As WIN32_FIND_DATA
   Dim hFile As Long

   hFile = FindFirstFile(sRoot & ALL_FILES, WFD)

   If hFile <> INVALID_HANDLE_VALUE Then

      Do

        'if a folder, call method again
         If (WFD.dwFileAttributes And vbDirectory) Then
            If Asc(WFD.cFileName) <> vbDot Then
             'watch line wrap on next line
              SubsContainFiles = SubsContainFiles(sRoot &
TrimNull(WFD.cFileName) & vbBackslash)
            End If
         Else

            SubsContainFiles = True
            Exit Do

         End If 'If WFD.dwFileAttributes

      Loop While FindNextFile(hFile, WFD)

   End If 'If hFile

   Call FindClose(hFile)

End Function

Private Function QualifyPath(sPath As String) As String

   If Right$(sPath, 1) <> vbBackslash Then
      QualifyPath = sPath & vbBackslash
   Else:
      QualifyPath = sPath
   End If

End Function

Private Function TrimNull(startstr As String) As String

   TrimNull = Left$(startstr, lstrlen(StrPtr(startstr)))

End Function

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.


|
| I'm writing an app that allows a person to delete a directory chosen
| by a DirListBox.  However, I don't want to allow the user to perform
| the deletion if any files or directories exist in the chosen
| directory.  If any exist, the subroutine is exited.
|
| So far I'm having no problems detecting files with the Dir command
| like so:
|
| strCheckForFiles = Dir(filAll.Path & "\" & "*.*")
|
| However, I'm having trouble with checking for directories.  The
| following code works find for getting the number of directories under
| the directory chosen by the user with the DirListBox "dirAll" as long
| as the person has chosen a root directory such as "C:\" or "D:\".  If
| he's chosen any other directory like "C:\My Documents", the code
| fails.  I'm on brain fry after several hours hitting a brick wall and
| am wondering what I'm doing wrong.  If someone could point me in the
| right direction I wouild be grateful.
|
| Here's the code:
|
| Sub GetDirectories()
|
| Dim strArray() As String
| Dim intDirCount As Integer
| Dim strTempPathName As String
| intDirCount = 0
| strTempPathName = Dir(strThePath & "*.*", vbDirectory)
| Do While strTempPathName <> ""
|    If strTempPathName <> "." And strTempPathName <> ".." Then
|        'make sure SubDir is a directory.
|
|        If (GetAttr(strThePath & strTempPathName) And vbDirectory) =
| vbDirectory Then
|            intDirCount = intDirCount + 1
|            ReDim Preserve strArray(intDirCount)
|            strArray(intDirCount) = strTempPathName & "\"
|        End If
|    End If
|    strTempPathName = Dir    ' Get next entry.
| Loop
|     MsgBox "Number of directories: " & intDirCount
| End Sub
|
| Private Sub dirAll_Change()
|     strThePath = dirAll.Path
| End Sub
|
|



Wed, 08 Jun 2005 13:15:37 GMT  
 Checking for directories not quite working right
I presume you are using the RmDir statement to remove your directories.
I'm pretty sure that command can't delete non-empty directories. So,
just trap the error...

On Error Resume Next
RmDir Dir1.Path
If Err.Number Then
  MsgBox "Either directory not empty or it doesn't exist"
End If

Rick - MVP


Quote:

> I'm writing an app that allows a person to delete a directory chosen
> by a DirListBox.  However, I don't want to allow the user to perform
> the deletion if any files or directories exist in the chosen
> directory.  If any exist, the subroutine is exited.

> So far I'm having no problems detecting files with the Dir command
> like so:

> strCheckForFiles = Dir(filAll.Path & "\" & "*.*")

> However, I'm having trouble with checking for directories.  The
> following code works find for getting the number of directories under
> the directory chosen by the user with the DirListBox "dirAll" as long
> as the person has chosen a root directory such as "C:\" or "D:\".  If
> he's chosen any other directory like "C:\My Documents", the code
> fails.  I'm on brain fry after several hours hitting a brick wall and
> am wondering what I'm doing wrong.  If someone could point me in the
> right direction I wouild be grateful.

> Here's the code:

> Sub GetDirectories()

> Dim strArray() As String
> Dim intDirCount As Integer
> Dim strTempPathName As String
> intDirCount = 0
> strTempPathName = Dir(strThePath & "*.*", vbDirectory)
> Do While strTempPathName <> ""
>    If strTempPathName <> "." And strTempPathName <> ".." Then
>        'make sure SubDir is a directory.

>        If (GetAttr(strThePath & strTempPathName) And vbDirectory) =
> vbDirectory Then
>            intDirCount = intDirCount + 1
>            ReDim Preserve strArray(intDirCount)
>            strArray(intDirCount) = strTempPathName & "\"
>        End If
>    End If
>    strTempPathName = Dir    ' Get next entry.
> Loop
>     MsgBox "Number of directories: " & intDirCount
> End Sub

> Private Sub dirAll_Change()
>     strThePath = dirAll.Path
> End Sub



Wed, 08 Jun 2005 17:53:06 GMT  
 Checking for directories not quite working right
Many thanks to all who helped.  I have some good options now.  

Randy, I like your approach much better than my attempting to count up
all those directories.  Your code will run much faster than what I was
doing, had I gotten it to run right.  

I'm wondering if there is a way to differentiate in your code between
these two things:

1. An empty directory with no subdirectories under it
2. An empty directory with one or more empty directories under it.  

Your snipet treats these two situations the same -- as safe to delete.
However, it's conceivable that a user may not wish to delete the empty
subdirectories and should therefore be prompted before doing so.  

Quote:

>Throw this into a form with a label (Label1) and set the path of interest in
>the command click sub. The label will reflect whether the path entered [snip]



Thu, 09 Jun 2005 00:21:55 GMT  
 Checking for directories not quite working right
Every time the code executes to drill down another level, you could set a
flag as such. Don't have time right now to work up the demo -- Christmas
shopping and all -- but that's the basics of it.  But the parameter will
have to be defined outside the procedure (private to the form) and reset as
appropriate.

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.


| Many thanks to all who helped.  I have some good options now.
|
| Randy, I like your approach much better than my attempting to count up
| all those directories.  Your code will run much faster than what I was
| doing, had I gotten it to run right.
|
| I'm wondering if there is a way to differentiate in your code between
| these two things:
|
| 1. An empty directory with no subdirectories under it
| 2. An empty directory with one or more empty directories under it.
|
| Your snipet treats these two situations the same -- as safe to delete.
| However, it's conceivable that a user may not wish to delete the empty
| subdirectories and should therefore be prompted before doing so.
|

|
| >Throw this into a form with a label (Label1) and set the path of interest
in
| >the command click sub. The label will reflect whether the path entered
[snip]
|



Thu, 09 Jun 2005 00:54:04 GMT  
 Checking for directories not quite working right
Okay, thanks, I'll check into that.  Many thanks for your help.  :)
Quote:

>Every time the code executes to drill down another level, you could set a
>flag as such. Don't have time right now to work up the demo -- Christmas
>shopping and all -- but that's the basics of it.  But the parameter will
>have to be defined outside the procedure (private to the form) and reset as
>appropriate.



Thu, 09 Jun 2005 01:05:13 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Virtual List View not quite working

2. SORT method not quite working

3. Can't quite get these events right

4. ByRef doesn't seem to be quite right

5. Checking whether or not a directory exists.

6. Parameters not working right from form and report

7. Deleting a Public Folder Mail Item Does not work - Even with right permissions

8. KeyBindings not working right?

9. Tab Order VB.NET Not working right

10. Tab Order VB.NET Not working right

11. can not get cancelBubble to work on right-click

12. MSDN VB index does not work right.

 

 
Powered by phpBB® Forum Software