Solved

Folders

Posted on 2012-03-25
17
277 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 20

Expert Comment

by:n2fc
ID: 37763972
Root folder CAN'T ever be deleted!
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 37763981
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
ID: 37764274
mbizup

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

Alan
0
 

Author Comment

by:chestera
ID: 37764300
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
ID: 37764613
Yes.   You need the backslash.
0
 
LVL 61

Expert Comment

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

Author Comment

by:chestera
ID: 37765223
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
ID: 37765262
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37766443
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
ID: 37766641
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
ID: 37766739
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
ID: 37768442
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
ID: 37769322
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
ID: 37770627
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
ID: 37770642
With that setup,  what are the results you are expecting?

Folder1 deleted?

Or nothing deleted?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37770673
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
ID: 37771077
mbizup

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

Alan
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

930 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

16 Experts available now in Live!

Get 1:1 Help Now