Solved

Folders

Posted on 2012-03-25
17
280 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

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…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

726 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