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. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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 | |
|
|