Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default delete folder

I'm trying to delete several folders with vba code. At
any given time, the folders may or may not contain files
or subfolders. The files or subfolders may or may not be
read only. But none of them should be in use. The folder
that I'm trying to delete is not readonly, and I have full
security permissions on it. No one else is using these
folders or files.

I tried writing a sub using rmdir:

Public Sub KillDir(strPathName As String)
If Right(strPathName, 1) < "\" Then
strPathName = strPathName & "\"
End If
Kill strPathName & "*.*"
RmDir strPathName
End Sub

Besides the fact that it needs to allow for the
possibility that the folder might not contain any files,
it can't handle subfolders, and it's giving me an error on
rmdir: "Run-Time error '75': Path/File access error". I
also tried giving it the path without the final "\", but
that gave the same error. I also tried
inserting "chdir "C:\" just before rmdir in case Excel had
it set as the current directory, but that didn't help
either.

Then I tried a different code (I have a reference set to
Microsoft Scripting Runtime):

Public Sub KillFolder(strPathName As String)
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.DeleteFolder strPathName, force:=True
End Sub

That one gives me "Run-time error '76': Path not found" if
strPathName ends with a "\" and "Run-time error '70':
Permission denied" if it doesn't.

I tried writing it a little differently:

Public Sub KillFolder(strPathName As String)
Dim FSO As FileSystemObject
Set FSO = New FileSystemObject
FSO.DeleteFolder strPathName, force:=True
End Sub

but that worked exactly the same.

I also tried manually deleting the folder with a right
click (which usually works), but until I close Excel, I
get an error there too: "Error Deleting File or Folder
Cannot remove folder [folder name]: There has been a
sharing violation. The source or destination file may be
in use."

All I want to do is delete the !@#$%^&* folders and
files! What am I doing wrong??? How can I make this
work???
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,718
Default delete folder

I tried your code like this:

Sub a()
KillFolder "c:\aa"
End Sub

Public Sub KillFolder(strPathName As String)
Dim FSO As FileSystemObject
Set FSO = New FileSystemObject
FSO.DeleteFolder strPathName, force:=True
End Sub

and it worked perfectly. Folder "aa" had files in it and subfolders with
files.

--
Jim Rech
Excel MVP


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 78
Default delete folder

Try this
(used this approach because Dir function is not re-entrant)

Sub RecursiveRmDir(folderName As String)
Dim fList As String
Dim fName As String

'make sure no trailing backslash
If Right(folderName, 1) = "\" Then folderName = Left
(folderName, Len(folderName) - 1)
'this will remove the files
Kill folderName & "\*.*"

fList = ""
fName = Dir(folderName & "\*.*", vbDirectory)
'build a list of sub-folders
Do While fName < ""
If Left(fName, 1) < "." Then fList = fList &
fName & "\"
fName = Dir()
Loop

Do While fList < ""
fName = Left(fList, InStr(fList, "\") - 1)
RecursiveRmDir folderName & "\" & fName
fList = Mid(fList, InStr(fList, "\") + 1)
Loop
'this will remove the folder
RmDir folderName
End Sub


Kevin Beckham

-----Original Message-----
I'm trying to delete several folders with vba code. At
any given time, the folders may or may not contain files
or subfolders. The files or subfolders may or may not be
read only. But none of them should be in use. The

folder
that I'm trying to delete is not readonly, and I have

full
security permissions on it. No one else is using these
folders or files.

I tried writing a sub using rmdir:

Public Sub KillDir(strPathName As String)
If Right(strPathName, 1) < "\" Then
strPathName = strPathName & "\"
End If
Kill strPathName & "*.*"
RmDir strPathName
End Sub

Besides the fact that it needs to allow for the
possibility that the folder might not contain any files,
it can't handle subfolders, and it's giving me an error

on
rmdir: "Run-Time error '75': Path/File access error". I
also tried giving it the path without the final "\", but
that gave the same error. I also tried
inserting "chdir "C:\" just before rmdir in case Excel

had
it set as the current directory, but that didn't help
either.

Then I tried a different code (I have a reference set to
Microsoft Scripting Runtime):

Public Sub KillFolder(strPathName As String)
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.DeleteFolder strPathName, force:=True
End Sub

That one gives me "Run-time error '76': Path not found"

if
strPathName ends with a "\" and "Run-time error '70':
Permission denied" if it doesn't.

I tried writing it a little differently:

Public Sub KillFolder(strPathName As String)
Dim FSO As FileSystemObject
Set FSO = New FileSystemObject
FSO.DeleteFolder strPathName, force:=True
End Sub

but that worked exactly the same.

I also tried manually deleting the folder with a right
click (which usually works), but until I close Excel, I
get an error there too: "Error Deleting File or Folder
Cannot remove folder [folder name]: There has been a
sharing violation. The source or destination file may be
in use."

All I want to do is delete the !@#$%^&* folders and
files! What am I doing wrong??? How can I make this
work???
.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default delete folder

Thanks for your testing and suggestions.

I did finally figure out the problem. The function I use
to make sure the folders exist in the first place uses the
Dir() function. And apparently, that function was
retaining possession (for lack of a better word) of the
folder that was tested last. So I tossed in a line at the
bottom of the function that points Dir() to something
different...something I know I won't need to delete... C:\

Now everything works great!

-----Original Message-----
I'm trying to delete several folders with vba code. At
any given time, the folders may or may not contain files
or subfolders. The files or subfolders may or may not be
read only. But none of them should be in use. The

folder
that I'm trying to delete is not readonly, and I have

full
security permissions on it. No one else is using these
folders or files.

I tried writing a sub using rmdir:

Public Sub KillDir(strPathName As String)
If Right(strPathName, 1) < "\" Then
strPathName = strPathName & "\"
End If
Kill strPathName & "*.*"
RmDir strPathName
End Sub

Besides the fact that it needs to allow for the
possibility that the folder might not contain any files,
it can't handle subfolders, and it's giving me an error

on
rmdir: "Run-Time error '75': Path/File access error". I
also tried giving it the path without the final "\", but
that gave the same error. I also tried
inserting "chdir "C:\" just before rmdir in case Excel

had
it set as the current directory, but that didn't help
either.

Then I tried a different code (I have a reference set to
Microsoft Scripting Runtime):

Public Sub KillFolder(strPathName As String)
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.DeleteFolder strPathName, force:=True
End Sub

That one gives me "Run-time error '76': Path not found"

if
strPathName ends with a "\" and "Run-time error '70':
Permission denied" if it doesn't.

I tried writing it a little differently:

Public Sub KillFolder(strPathName As String)
Dim FSO As FileSystemObject
Set FSO = New FileSystemObject
FSO.DeleteFolder strPathName, force:=True
End Sub

but that worked exactly the same.

I also tried manually deleting the folder with a right
click (which usually works), but until I close Excel, I
get an error there too: "Error Deleting File or Folder
Cannot remove folder [folder name]: There has been a
sharing violation. The source or destination file may be
in use."

All I want to do is delete the !@#$%^&* folders and
files! What am I doing wrong??? How can I make this
work???
.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
UNABLE TO DELETE FOLDER hitesh Excel Discussion (Misc queries) 4 January 26th 10 12:26 AM
Time based file/folder delete don Excel Discussion (Misc queries) 1 March 30th 09 02:20 PM
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven Excel Discussion (Misc queries) 1 January 24th 06 03:28 PM
delete all the contents (sub folders and files) in the temp folder Joseph Excel Discussion (Misc queries) 0 June 6th 05 08:01 AM
fso delete folder shockley Excel Programming 5 December 24th 03 04:43 PM


All times are GMT +1. The time now is 04:34 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"