[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Folders

Posted on 2012-03-25
17
Medium Priority
?
296 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 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
Industry Leaders: 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!

 

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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

834 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