LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #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???
 
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 02:01 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"