A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Function to delete a folder and its content



 
 
Thread Tools Display Modes
  #1  
Old February 28th, 2009, 07:18 AM posted to microsoft.public.access
John J.
external usenet poster
 
Posts: 160
Default Function to delete a folder and its content

I noticed that rmdir doesnt'work if there are subfolders and files in the
folder. Is there some function available that first clears all contents of
the folder and then deletes the folder itself?
Thank you,
John


  #2  
Old February 28th, 2009, 10:51 AM posted to microsoft.public.access
Arvin Meyer [MVP][_2_]
external usenet poster
 
Posts: 2,310
Default Function to delete a folder and its content

I would simple delete it and handle any errors (untested):

Sub KillDir(strPath As String)
On Error Resume Next
Kill strDir
End Sub

--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com

"John J." wrote in message
...
I noticed that rmdir doesnt'work if there are subfolders and files in the
folder. Is there some function available that first clears all contents of
the folder and then deletes the folder itself?
Thank you,
John



  #3  
Old February 28th, 2009, 11:44 AM posted to microsoft.public.access
John J.
external usenet poster
 
Posts: 160
Default Function to delete a folder and its content

Thanks, but it doesn't work. Deleting a folder with the kill statement
doesn't work (error: file cannot be found). The Help file says I should use
rmdir.
John

"Arvin Meyer [MVP]" schreef in bericht
...
I would simple delete it and handle any errors (untested):

Sub KillDir(strPath As String)
On Error Resume Next
Kill strDir
End Sub

--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com

"John J." wrote in message
...
I noticed that rmdir doesnt'work if there are subfolders and files in the
folder. Is there some function available that first clears all contents of
the folder and then deletes the folder itself?
Thank you,
John





  #4  
Old February 28th, 2009, 12:55 PM posted to microsoft.public.access
H. Druss
external usenet poster
 
Posts: 11
Default Function to delete a folder and its content


"John J." wrote in message
...
I noticed that rmdir doesnt'work if there are subfolders and files in the
folder. Is there some function available that first clears all contents of
the folder and then deletes the folder itself?
Thank you,
John

Hi John
Try this. Needs some error handling probably.
================================================== ==================================
Option Explicit
Private Declare Function RemoveDirectory Lib "kernel32" Alias
"RemoveDirectoryA" (ByVal lpPathName As String) As Long

Private Sub Command1_Click()
Dim sPath As String
sPath = "c:\DeleteThisFolder"
RemoveFilesAndFolder sPath
End Sub

Sub RemoveFilesAndFolder(sPath As String)
Dim ff As String
' need the trailing back slash
If Right(sPath, 1) "\" Then sPath = sPath & "\"
ff = Dir(sPath)
If ff "" Then
Kill sPath & "*.*"
End If
RemoveDirectory sPath
End Sub
================================================== =========================================
Good luck
Harold


  #5  
Old February 28th, 2009, 01:21 PM posted to microsoft.public.access
John J.
external usenet poster
 
Posts: 160
Default Function to delete a folder and its content

Thanks. But with this code only files in the main folder gets deleted.
The main folder itself, subfolders and subfolder files aren't deleted.
John

"H. Druss" schreef in bericht
...

"John J." wrote in message
...
I noticed that rmdir doesnt'work if there are subfolders and files in the
folder. Is there some function available that first clears all contents of
the folder and then deletes the folder itself?
Thank you,
John

Hi John
Try this. Needs some error handling probably.
================================================== ==================================
Option Explicit
Private Declare Function RemoveDirectory Lib "kernel32" Alias
"RemoveDirectoryA" (ByVal lpPathName As String) As Long

Private Sub Command1_Click()
Dim sPath As String
sPath = "c:\DeleteThisFolder"
RemoveFilesAndFolder sPath
End Sub

Sub RemoveFilesAndFolder(sPath As String)
Dim ff As String
' need the trailing back slash
If Right(sPath, 1) "\" Then sPath = sPath & "\"
ff = Dir(sPath)
If ff "" Then
Kill sPath & "*.*"
End If
RemoveDirectory sPath
End Sub
================================================== =========================================
Good luck
Harold



  #6  
Old February 28th, 2009, 03:14 PM posted to microsoft.public.access
TedMi[_3_]
external usenet poster
 
Posts: 48
Default Function to delete a folder and its content

rmdir FolderName /S
-TedMi

"John J." wrote in message
...
I noticed that rmdir doesnt'work if there are subfolders and files in the
folder. Is there some function available that first clears all contents of
the folder and then deletes the folder itself?
Thank you,
John



  #7  
Old February 28th, 2009, 03:43 PM posted to microsoft.public.access
John J.
external usenet poster
 
Posts: 160
Default Function to delete a folder and its content

Sorry, but I should have mentioned that it should run in VBA.
John

"TedMi" schreef in bericht
...
rmdir FolderName /S
-TedMi

"John J." wrote in message
...
I noticed that rmdir doesnt'work if there are subfolders and files in the
folder. Is there some function available that first clears all contents of
the folder and then deletes the folder itself?
Thank you,
John



  #8  
Old February 28th, 2009, 06:11 PM posted to microsoft.public.access
H. Druss
external usenet poster
 
Posts: 11
Default Function to delete a folder and its content


"John J." wrote in message
...
I noticed that rmdir doesnt'work if there are subfolders and files in the
folder. Is there some function available that first clears all contents of
the folder and then deletes the folder itself?
Thank you,
John

Hi John
Try this:
================================================== =================
Private Sub RemoveFoldersAndFiles(sPath As String)
If Right(sPath, 1) = "\" Then sPath = Left$(sPath, Len(sPath) - 1)
Dim fso As New Scripting.FileSystemObject
fso.DeleteFolder sPath

'You can also pass a second argument and set it to True if you want to force
the deletion of read-only files:
'fso.DeleteFolder sPath, True

End Sub
================================================== =====================
Private Sub Command1_Click()

RemoveFoldersAndFiles "c:\theFolder"

End Sub
================================================== ======================

You need to add a reference to the Microsoft Scripting Runtime.

Good luck
Harold


  #9  
Old February 28th, 2009, 08:02 PM posted to microsoft.public.access
John J.
external usenet poster
 
Posts: 160
Default Function to delete a folder and its content

Yes! it works.
Thanks so much.
John

"H. Druss" schreef in bericht
...

"John J." wrote in message
...
I noticed that rmdir doesnt'work if there are subfolders and files in the
folder. Is there some function available that first clears all contents of
the folder and then deletes the folder itself?
Thank you,
John

Hi John
Try this:
================================================== =================
Private Sub RemoveFoldersAndFiles(sPath As String)
If Right(sPath, 1) = "\" Then sPath = Left$(sPath, Len(sPath) - 1)
Dim fso As New Scripting.FileSystemObject
fso.DeleteFolder sPath

'You can also pass a second argument and set it to True if you want to
force the deletion of read-only files:
'fso.DeleteFolder sPath, True

End Sub
================================================== =====================
Private Sub Command1_Click()

RemoveFoldersAndFiles "c:\theFolder"

End Sub
================================================== ======================

You need to add a reference to the Microsoft Scripting Runtime.

Good luck
Harold



 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 06:20 PM.


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