Delete Empty Folder Once files are moved. (1 Viewer)

Lochwood

Registered User.
Local time
Today, 07:17
Joined
Jun 7, 2017
Messages
130
I have a button that moves files from location 1 to location 2. If the folder doesn't exist in location 2, it creates it before moving the files. What i am trying to do is delete the empty folder from location 1 once the files have been moved to location 2. How would i do this?

Here is my current working code that moves the files perfectly but leaves the empty folder behind in location 1.

Private Sub Command63_Click()


Const Prep = "\\servername\dart$\Document-Library\Doc-Production"
Const Pub = "\\servername\DART$\Document-Library\Public"



Dim OldPath As String
Dim NewPath As String
Dim LoopFile As String
Dim FileExt() As String
'Dim Doc_Folder As String

Dim Folder As String
Dim fso As Object


Folder = Replace(Pub & Doc_Number, "/", "-")



' Create FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")

' Check whether folder exists
If fso.FolderExists(Folder) = False Then

' If not, create it
fso.CreateFolder Folder
End If

' Open it
Shell "explorer.exe " & Folder, vbNormalFocus


OldPath = Replace(Prep & Doc_Number & "", "/", "-")
NewPath = Replace(Pub & Doc_Number & "", "/", "-")
LoopFile = Dir(OldPath & "*.*")
Do While LoopFile <> ""
FileExt = Split(LoopFile, ".")
Select Case FileExt(UBound(FileExt))
Case "docx", "doc"
Name OldPath & LoopFile As NewPath & LoopFile
End Select
LoopFile = Dir
Loop



End Sub
 

Minty

AWF VIP
Local time
Today, 15:17
Joined
Jul 26, 2013
Messages
10,368
To delete a file use
Code:
Kill FullFilePath

To Delete a folder (it has to be empty)
Code:
RmDir FolderPath
 

Lochwood

Registered User.
Local time
Today, 07:17
Joined
Jun 7, 2017
Messages
130
Thanks,

Is there any way to only delete the folder if it is empty. happy for it to delete all empty subfolders rather than specific folder.
 

Lochwood

Registered User.
Local time
Today, 07:17
Joined
Jun 7, 2017
Messages
130
Ok the only issue i can find with this command is it also deletes the parent folder. i need it to delete only the empty subfolders.
 

Lochwood

Registered User.
Local time
Today, 07:17
Joined
Jun 7, 2017
Messages
130
Heres the code that worked.

Private Sub Command65_Click()
On Error Resume Next ' <-- Comment out for troubleshooting.

Const Prep = "\\Servername\DART$\Document-Library\Doc-Production"

Dim objfolder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objfolder = objFSO.getFolder(Prep)

Call Search(objfolder)
End Sub
Sub Search(objfolder)

Const Prep = "\\Servername\DART$\Document-Library\Doc-Production"
Dim objsubfolder, objfile

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objfolder = objFSO.getFolder(Prep)

'For Each objfile In objfolder.Files
'If DateDiff("d", objfile.dateLastModified, Now) > 30 Then objfile.Delete
'Next

For Each objsubfolder In objfolder.SubFolders
If objsubfolder.Files.Count = 0 Then objsubfolder.Delete
Next

End Sub
 

Users who are viewing this thread

Top Bottom