Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Folders

Posted on 2012-03-25
17
Medium Priority
?
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

618 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