Solved

Folders

Posted on 2012-03-25
17
276 Views
Last Modified: 2012-03-27
Hi EE

Is ther a way via code to delete Subfolders but keep the root folder

chestera
0
Comment
Question by:chestera
  • 8
  • 6
  • 2
  • +1
17 Comments
 
LVL 19

Expert Comment

by:n2fc
Comment Utility
Root folder CAN'T ever be deleted!
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
Comment Utility
This will delete all subfolders (and their contents), but leave the root folder and anything at the rool level that is not a folder.

*** Please be very careful in choosing your 'root folder' when testing this code! ***


Sub DeleteSubFolders()

Dim strFolder As String
Dim strSubfolder As String
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

' This is the "root folder".  You can alternatively include this as a pass parameter in the
' Sub declaration.
strFolder = "C:\test\"    '<---- Folder name goes here

' This gets the first file under the root folder
strSubfolder = dir(strFolder, vbDirectory)

' This loops through the files/subfolders under the root folder, ending when the empty
' string is returned by the dir function - meaning that there are no more files to loop
' through.
Do While Len(strSubfolder) > 0
If strSubfolder <> "." And strSubfolder <> ".." Then

' Deletes the file - if it is a folder
fso.deletefolder strFolder & strSubfolder
End If

' Gets the next file under the root folder
strSubfolder = dir()

Loop

End Sub
0
 

Author Comment

by:chestera
Comment Utility
mbizup

Thank you for that info I will give it a try now and get back to you

Alan
0
 

Author Comment

by:chestera
Comment Utility
mbizup

I created C:\AAtest\Fold1

It loops but bypasses    fso.deletefolder strFolder & strSubfolder. After Fold1 I placed a back slash is that correct

Alan
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
Yes.   You need the backslash.
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
Did you change the line where I specify my test foler appropriately to specify your own folder?
0
 

Author Comment

by:chestera
Comment Utility
mbizup

strFolder = "C:\AAtest\Folder1\"    '<---- Folder name goes here

I have this

when it reaches  fso.deletefolder strFolder & strSubfolder it look like
fso.deletefolder C:\AAtest\Fold1\Fold1 so then I get path not found


Alan
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
I've tested this, and it definitely behaves the way I would expect it to - deleting subfolders and their contents under "Folder1", but not deleting individual files at the root level (such as C:\AAtest\Folder1\test1.mdb).

I've reposted the code - the only modification is the path (c:\AAText\Folder1)

If there are subfolders under Folder1, in my tests they all get deleted.  

Please double-check your foldername.  You mentioned that you chenged the code to:
strFolder = "C:\AAtest\Folder1\"    '<---- Folder name goes here

However, in your description of the touble you are having, you are specifying
C:\AAtest\Fold1\Fold1

Should it be Folder1 or Fold1?



Sub DeleteSubFolders()

Dim strFolder As String
Dim strSubfolder As String
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

' This is the "root folder".  You can alternatively include this as a pass parameter in the
' Sub declaration.
strFolder = "C:\AAtest\Folder1\"     '<---- Folder name goes here

' This gets the first file under the root folder
strSubfolder = Dir(strFolder, vbDirectory)

' This loops through the files/subfolders under the root folder, ending when the empty
' string is returned by the dir function - meaning that there are no more files to loop
' through.
Do While Len(strSubfolder) > 0
    If strSubfolder <> "." And strSubfolder <> ".." Then

    ' Deletes the file - if it is a folder
    fso.deletefolder strFolder & strSubfolder

End If

' Gets the next file under the root folder
strSubfolder = Dir()

Loop

End Sub

Open in new window

0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
mbizup

Does your code search for sub-subfolders (recursively)?
I was involved in two posts a while back were my solution (do something for all files in all subfolders)...did not work for sub-sub folders
ex:
It would work for:
C:\
C:\Sub1
C:\Sub2
C:\Sub3

but not for:
C:\Sub1\SubSub001\test\
C:\Sub2\Images\2011\March\Jake\
C:\sub3\Archive\Janet\2012\Sales\Spain\

I had to apply the same "Subfolder" search for each SubSubfolder...

Jeff
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
Jeff,

>> Does your code search for sub-subfolders (recursively)?

No, and for this question I don't think it needs to.

It completely wipes out folder structures under:

C:\AAtest\Folder1\

Which automatically includes

C:\AAtest\Folder1\folder2

C:\AAtest\Folder1\folder2\folder3

C:\AAtest\Folder1\folder2\folder3\folder4

... etc


Test it out...     :-)
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
Oh, I see...

(Test it out...     :-))
yeah, I did right after I posted...

Instead of "searching" for subfolders, (as I did in my thread), yours simply "Deletes" anything below C:\X

So it seems I was looking at this from the wrong angle...

;-)

Jeff
0
 

Author Comment

by:chestera
Comment Utility
mbizup

Just copied your code above and pasted behind a button click event. My original Folder was called Fold1. Changed to "C:\AAtest\Folder1\". Still no go.
 using Shift F8

strFolder = "C:\AAtest\Folder1\"     '<---- Folder name goes here
strSubfolder = Dir(strFolder, vbDirectory)
Do While Len(strSubfolder) > 0
    If strSubfolder <> "." And strSubfolder <> ".." Then

I get
strSubfolder = "." then loops and = ".." so never get to the delete

Sorry for this problem looking at the code it should work

I am away for a few hours to day so wont be able to respond immediately

Alan
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
What does your actual folder structure look like (what do you have under Folder1)?

If I could pinpoint the differences, I might be able to tell why this is working here but not for you.
0
 

Author Comment

by:chestera
Comment Utility
mbizup

This is what I have. I setup C:\AAtest\Folder1 just to test your code and below is exactly what I am running. There is nothing in Folder1


Dim strFolder As String
Dim strSubfolder As String
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

' This is the "root folder".  You can alternatively include this as a pass parameter in the
' Sub declaration.
strFolder = "C:\AAtest\Folder1\"     '<---- Folder name goes here

' This gets the first file under the root folder
strSubfolder = Dir(strFolder, vbDirectory)

' This loops through the files/subfolders under the root folder, ending when the empty
' string is returned by the dir function - meaning that there are no more files to loop
' through.
Do While Len(strSubfolder) > 0
    If strSubfolder <> "." And strSubfolder <> ".." Then

    ' Deletes the file - if it is a folder
    fso.deletefolder strFolder & strSubfolder

End If

' Gets the next file under the root folder
strSubfolder = Dir()

Loop



Alan
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
With that setup,  what are the results you are expecting?

Folder1 deleted?

Or nothing deleted?
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
If you are expecting folder1 to be deleted then you need to specify the parent path in the code.

Strfolder= "c:\aatest\"
0
 

Author Comment

by:chestera
Comment Utility
mbizup

Thank you for your patience I have finally got it just misunderstood the set up

Alan
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now