473,473 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

RemoveDirectory Fails

Lex
Hi,

below is the code I wrote (VB6) to delete a folder with it contents.
It is basically recursive function which uses FindFirstFile,
FindNextFile, FindClose and RemoveDirectory APIs.

The problem with the code is that it wouldn't delete
folders containing other folders (the innermost ones get deleted ok
though).

Although I made sure I close handles obtained with
FindFirstFile it still looks like somethig in my code
holds handles or something as I get error:
32 - The process cannot access the file because it is being used
by another process. The upper folders could not be deleted
until VB is closed.

Does anyone have an idea what's wrong?

Thanks,
Lex
Private Const MAX_PATH = 260
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const INVALID_HANDLE_VALUE = -1
Private Const DBG_FILENAME As String = "Test.bas"
Private Const ERROR_NO_MORE_FILES = 18&

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Declare Function FormatMessage Lib "Kernel32" Alias
"FormatMessageA" _
(ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As
Long, _
ByVal dwLanguageId As Long, ByVal lpBuffer As String, _
ByVal nSize As Long, Arguments 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 FindClose Lib "Kernel32" (ByVal hFindFile As
Long) As Long

Public Declare Function RemoveDirectoryAPI Lib "Kernel32" Alias
"RemoveDirectoryA" _
(ByVal lpPathName As String) As Long

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
Public Sub RemoveDirectory(ByVal i_sPath As String)
On Error GoTo ErrorHandler

Dim nAttr As Integer
Dim fData As WIN32_FIND_DATA
Dim i_sPathS As String
Dim sCurFile As String
Dim bExitLoop As Boolean
Dim hFind As Long
Dim lError As Long
Dim sErrorDetails As String
Dim bOpen As Boolean

If Not Dir(i_sPath, vbNormal Or vbDirectory Or vbHidden Or
vbReadOnly Or vbSystem) = "" Then
nAttr = GetAttr(i_sPath)
If (nAttr And vbReadOnly) Then SetAttr i_sPath, nAttr And Not
vbReadOnly
If (nAttr And vbDirectory) = vbDirectory Then
i_sPathS = i_sPathS & IIf(Right(i_sPath, 1) = "\", "",
"\")
hFind = FindFirstFile(i_sPathS & "*.*", fData)
If hFind = INVALID_HANDLE_VALUE Then
lError = Err.LastDllError
sErrorDetails = String(512, 0)
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, 0, lError,
0, _
sErrorDetails, Len(sErrorDetails),
0
Err.Raise lError, DBG_FILENAME, sErrorDetails
Else
bOpen = True
End If

Do
sCurFile = Left$(fData.cFileName,
InStr(fData.cFileName, vbNullChar) - 1)
If sCurFile <> "." And sCurFile <> ".." Then
If (fData.dwFileAttributes And vbDirectory) =
vbDirectory Then
RemoveDirectory i_sPathS & sCurFile
Else
If (fData.dwFileAttributes And vbReadOnly)
Then
SetAttr i_sPathS & sCurFile,
fData.dwFileAttributes And Not vbReadOnly
End If
Kill i_sPathS & sCurFile
End If
End If
If FindNextFile(hFind, fData) = 0 Then
lError = Err.LastDllError
hFind = FindClose(hFind)
bOpen = False
If lError <> ERROR_NO_MORE_FILES Then
sErrorDetails = String(512, 0)
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, 0,
lError, 0, _
sErrorDetails, Len(sErrorDetails),
0
Err.Raise lError, DBG_FILENAME, sErrorDetails
End If
bExitLoop = True
End If
Loop Until bExitLoop
If RemoveDirectoryAPI(i_sPath) = 0 Then
lError = Err.LastDllError
sErrorDetails = String(512, 0)
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, 0, lError,
0, _
sErrorDetails, Len(sErrorDetails), 0
Err.Raise lError, DBG_FILENAME, sErrorDetails
End If
Else
Kill i_sPath
End If
End If

Exit Sub
ErrorHandler:
If bOpen Then FindClose hFind
End Sub
Jul 17 '05 #1
4 4996
Use ChDrive and ChDir and to change the drive and directory to a known path
that is not in the folders enumerated, such as app.path. You app has
probably made one of the enumerated folders the current folder, which
subsequently can't be deleted.

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
"Lex" <le***@newmail.ru> wrote in message
news:69*************************@posting.google.co m...
: Hi,
:
: below is the code I wrote (VB6) to delete a folder with it contents.
: It is basically recursive function which uses FindFirstFile,
: FindNextFile, FindClose and RemoveDirectory APIs.
:
: The problem with the code is that it wouldn't delete
: folders containing other folders (the innermost ones get deleted ok
: though).
:
: Although I made sure I close handles obtained with
: FindFirstFile it still looks like somethig in my code
: holds handles or something as I get error:
: 32 - The process cannot access the file because it is being used
: by another process. The upper folders could not be deleted
: until VB is closed.
:
: Does anyone have an idea what's wrong?
:
: Thanks,
: Lex
:
:
: Private Const MAX_PATH = 260
: Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
: Private Const INVALID_HANDLE_VALUE = -1
: Private Const DBG_FILENAME As String = "Test.bas"
: Private Const ERROR_NO_MORE_FILES = 18&
:
: Private Type FILETIME
: dwLowDateTime As Long
: dwHighDateTime As Long
: End Type
:
: Private Declare Function FormatMessage Lib "Kernel32" Alias
: "FormatMessageA" _
: (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As
: Long, _
: ByVal dwLanguageId As Long, ByVal lpBuffer As String, _
: ByVal nSize As Long, Arguments 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 FindClose Lib "Kernel32" (ByVal hFindFile As
: Long) As Long
:
: Public Declare Function RemoveDirectoryAPI Lib "Kernel32" Alias
: "RemoveDirectoryA" _
: (ByVal lpPathName As String) As Long
:
: 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
: Public Sub RemoveDirectory(ByVal i_sPath As String)
: On Error GoTo ErrorHandler
:
: Dim nAttr As Integer
: Dim fData As WIN32_FIND_DATA
: Dim i_sPathS As String
: Dim sCurFile As String
: Dim bExitLoop As Boolean
: Dim hFind As Long
: Dim lError As Long
: Dim sErrorDetails As String
: Dim bOpen As Boolean
:
: If Not Dir(i_sPath, vbNormal Or vbDirectory Or vbHidden Or
: vbReadOnly Or vbSystem) = "" Then
: nAttr = GetAttr(i_sPath)
: If (nAttr And vbReadOnly) Then SetAttr i_sPath, nAttr And Not
: vbReadOnly
: If (nAttr And vbDirectory) = vbDirectory Then
: i_sPathS = i_sPathS & IIf(Right(i_sPath, 1) = "\", "",
: "\")
: hFind = FindFirstFile(i_sPathS & "*.*", fData)
: If hFind = INVALID_HANDLE_VALUE Then
: lError = Err.LastDllError
: sErrorDetails = String(512, 0)
: FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, 0, lError,
: 0, _
: sErrorDetails, Len(sErrorDetails),
: 0
: Err.Raise lError, DBG_FILENAME, sErrorDetails
: Else
: bOpen = True
: End If
:
: Do
: sCurFile = Left$(fData.cFileName,
: InStr(fData.cFileName, vbNullChar) - 1)
: If sCurFile <> "." And sCurFile <> ".." Then
: If (fData.dwFileAttributes And vbDirectory) =
: vbDirectory Then
: RemoveDirectory i_sPathS & sCurFile
: Else
: If (fData.dwFileAttributes And vbReadOnly)
: Then
: SetAttr i_sPathS & sCurFile,
: fData.dwFileAttributes And Not vbReadOnly
: End If
: Kill i_sPathS & sCurFile
: End If
: End If
: If FindNextFile(hFind, fData) = 0 Then
: lError = Err.LastDllError
: hFind = FindClose(hFind)
: bOpen = False
: If lError <> ERROR_NO_MORE_FILES Then
: sErrorDetails = String(512, 0)
: FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, 0,
: lError, 0, _
: sErrorDetails, Len(sErrorDetails),
: 0
: Err.Raise lError, DBG_FILENAME, sErrorDetails
: End If
: bExitLoop = True
: End If
: Loop Until bExitLoop
: If RemoveDirectoryAPI(i_sPath) = 0 Then
: lError = Err.LastDllError
: sErrorDetails = String(512, 0)
: FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, 0, lError,
: 0, _
: sErrorDetails, Len(sErrorDetails), 0
: Err.Raise lError, DBG_FILENAME, sErrorDetails
: End If
: Else
: Kill i_sPath
: End If
: End If
:
: Exit Sub
: ErrorHandler:
: If bOpen Then FindClose hFind
: End Sub

Jul 17 '05 #2
Lex
I tried this, still no luck...

"Randy Birch" <rg************@mvps.org> wrote in message news:<uV***************@news04.bloor.is.net.cable. rogers.com>...
Use ChDrive and ChDir and to change the drive and directory to a known path
that is not in the folders enumerated, such as app.path. You app has
probably made one of the enumerated folders the current folder, which
subsequently can't be deleted.

--

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

Jul 17 '05 #3
How about saving the fully-qualified folders into an array where you
currently execute the

RemoveDirectory i_sPathS & sCurFile

line, and then enumerate through the array after the search and delete the
folders you've identified. This will ensure you've executed a Close for each
of the FindFirstFile handles you've opened during the loop.

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
"Lex" <le***@newmail.ru> wrote in message
news:69**************************@posting.google.c om...
:I tried this, still no luck...
:
: "Randy Birch" <rg************@mvps.org> wrote in message
news:<uV***************@news04.bloor.is.net.cable. rogers.com>...
: > Use ChDrive and ChDir and to change the drive and directory to a known
path
: > that is not in the folders enumerated, such as app.path. You app has
: > probably made one of the enumerated folders the current folder, which
: > subsequently can't be deleted.
: >
: > --
: >
: > Randy Birch
: > MVP Visual Basic
: > http://vbnet.mvps.org/
: > Please respond only to the newsgroups so all can benefit.
: >

Jul 17 '05 #4
Lex
tried this, still no luck :(

"Randy Birch" <rg************@mvps.org> wrote in message news:<uV***************@news04.bloor.is.net.cable. rogers.com>...
Use ChDrive and ChDir and to change the drive and directory to a known path
that is not in the folders enumerated, such as app.path. You app has
probably made one of the enumerated folders the current folder, which
subsequently can't be deleted.

--

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

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: me | last post by:
Hello python world. Since yesterday I'm using Python 2.4/PythonWin (build 203) on my Win98 system. My problem - that did not occured in the previous version Python 2.3.4/PythonWin (build 163) -...
0
by: Andy Todd | last post by:
Hi We have just moved an ASP.NET application into the live environment which is as follows: Sun Proxy Server / Firewall Windows 2000 Server / IIS5 The URL for the site maps to the Sun...
6
by: kenneth fleckenstein nielsen | last post by:
Hi guru's It runs ok on my developmaschine, and on the test server that i've set up. but fails after installing on the customers server. I made a XML webservice that does these steps: a) access a...
7
by: SevDer | last post by:
Hi I have a class library that needs to download the HTML in a specific page of ours with provided querystring. When I open this URL with any browser, it loads fine. When I do WebRequest from Web...
1
by: comp.lang.php | last post by:
Consider my code: if ($this->isSuccessful && is_file($_FILES)) { // STEP 6: MOVE RESUME TO DIRECTORY $uuid = $this->sfug->getUUID(); if (!$uuid) $this->sfug->setUUID(); $uuid =...
2
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
2
by: Anbu | last post by:
Sorry for cross posting the query. But I need a resolution as early as possible. I have developed an application to authenticate the user based on LDAP Search and authentication. The Windows...
12
by: Jim Rodgers | last post by:
I have a big asp file that has an error under certain conditions -- totally repeatable. However, it only fails when I set response.buffer = True at the top. WHen I set it False in order to debug...
2
by: =?Utf-8?B?YWxiZXJ0b3Nvcmlh?= | last post by:
Hi, I'm using Threads, and when I try to do Server.Transfer, I recieved an error. (child object does not exist...) My Code: Dim t As New Thread(AddressOf Hilo) Private Sub Hilo()...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.