Solved

VBScript for deleting nested subfolders every 30 days except one.

Posted on 2010-08-25
14
1,264 Views
Last Modified: 2012-05-10
The following script was intended to delete all subfolders and nested subfolders from a parent folder with the exception of one subfolder named "Log". These subfolders and nested subfolders do contain files as well. To clarify, I want to preserve the parent folder, preserve the Log subfolder (and it's contents), while deleting all other subfolders (and their contents) from the parent. Can someone identify how to correct this script?

Dim fso, OlderThanDate

Dim folder, folderCollection, subFolder

 

OlderThanDate = DateAdd("d", -60, Date) 

Set fso = CreateObject("Scripting.FileSystemObject")

Set folder = fso.GetFolder("D:\archive\")

Set folderCollection = folder.SubFolders

For Each subFolder In folderCollection

    If subFolder.Name <> "Log" Then

        If subFolder.DateCreated < OlderThanDate Then

            fso.DeleteFolder subFolder.Path, True

	Else 

	    fso.DeleteFolder subFolder.Path, False    	

    End If

End If   

Next

Open in new window

0
Comment
Question by:jcb431
  • 8
  • 5
14 Comments
 
LVL 18

Expert Comment

by:exx1976
ID: 33524525
This should work.  I'd recommend testing it first though.


HTH,
exx
OlderThanDate = DateAdd("d", -30, Date) 

Set fso = CreateObject("Scripting.FileSystemObject")

deleteFolders "D:\archive\", True



Function deleteFolders(path, parent)

	Set folder = fso.GetFolder(path)

	Set folderCollection = folder.SubFolders

	For Each subFolder In folderCollection

	    If subFolder.Name <> "Log" Then

	    	deleteFolders subfolder.path, False

	    	For Each file In subfolder.files

	    		If file.datecreated < olderthandate Then fso.DeleteFile file.path

	    	Next

	    	If subFolder.DateCreated < OlderThanDate and not parent Then fso.DeleteFolder subFolder.Path, True

	    End If

	Next

End Function

Open in new window

0
 

Author Comment

by:jcb431
ID: 33525702
Resulted in the following runtime error:

Line: 15
Char:7
Error: Object required: 'subFolder'
0
 
LVL 18

Expert Comment

by:exx1976
ID: 33525850
I don't know what you did, but line 15 of the code I posted is  "End If"...  ??
0
 

Author Comment

by:jcb431
ID: 33542790
I had inserted an additional empty line. I've since removed it. The script now matches the posted one line for line. The same error is now referencing line 14.
0
 

Author Comment

by:jcb431
ID: 33559345
Resulted in the following runtime error:

Line: 14
Char:7
Error: Object required: 'subFolder'
0
 
LVL 18

Expert Comment

by:exx1976
ID: 33559902
Ahhh..  I see what's going on.  My apologies, I sort of wrote that in a hurry.  I'll have a new one up for you in a few minutes.


-exx
0
 
LVL 18

Accepted Solution

by:
exx1976 earned 250 total points
ID: 33559964
Try this one.  Also, it doesn't look at the age of the files/folders.  I think I mis-read your original question.  You want this to happen every 30 days, and to remove everything but the root and the logs, right?  The first one was looking at everything checking to see if it was 30 days old.  This one ignores file/folder age, and just deletes.  Schedule it to run every 30 days.


HTH,
exx
Set fso = CreateObject("Scripting.FileSystemObject")

deleteFolders "D:\archive\", True



Function deleteFolders(path, parent)

	Set folder = fso.GetFolder(path)

	If folder.Name <> "Log" Then

		For Each file In folder.Files

			fso.DeleteFile file.path

		Next

	End If

	For Each subFolder In folder.subfolders

	    If subFolder.Name <> "Log" Then

	    	deleteFolders subfolder.path, False

	    	If Not parent Then fso.DeleteFolder subFolder.Path, True

	    End If

	Next

End Function

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:jcb431
ID: 33598151
New WSH error:

Line: 14
Char: 26
Error: Invalid procedure call or argument
Code: 800A0005
Source: Microsoft VBscript runtime error

*** I need to clarify somethings.  The Root folder contains a Log folder and a number of User folders. Each /User should be deleted 30 days from the date when they were first moved into Root. The script would run nightly and check the age of each /User folder. Only the /Users that have existed in Root for 30 days, would be deleted.  So it really is a matter of age, not interval. Take a look at the following:

/Root - - -
              - - - /Log
              - - - /User1
                               - - - /User1/SubDirectory#1
                               - - - /User1/SubDirectory#2
             - - - /User2
                               - -  - /User2/SubDirectory#1
                                - - - /User2/SubDirectory#2

In this case, the only folders whose age we are conerned about are /Users1 and /Users2. When those 2 specific directories reach 30 days old, they and all of their sub directories and files should be deleted. If /Users2 reaches 30 days old before /Users1, then this is what the directory structure should look like after the script executes:

/Root - - -
              - - - /Log
              - - - /User1
                               - - - /User1/SubDirectory#1
                               - - - /User1/SubDirectory#2
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 33634527
let's see:
OlderThanDate = DateAdd("d", -30, Date) 

Set fso = CreateObject("Scripting.FileSystemObject")

deleteFolders "D:\archive\", True



Function deleteFolders(path, parent)

        Set folder = fso.GetFolder(path)

        Set folderCollection = folder.SubFolders

        For Each subFolder In folderCollection

            If subFolder.Name <> "Log" Then

               deleteFolders subFolder.path, False

               If subFolder.DateCreated < OlderThanDate and not parent Then  

                  For Each file In subfolder.files

                     If file.datecreated < olderthandate Then 

                       file.delete

                     end if

                  Next

                  subFolder.delete

               end if

            End If

        Next

End Function

Open in new window

0
 

Author Comment

by:jcb431
ID: 33682153
Invalid procedure call or argument on line 17, character 19.  
0
 

Author Comment

by:jcb431
ID: 33978014
anyone?
0
 
LVL 18

Expert Comment

by:exx1976
ID: 33982532
Sorry I've been MIA..   Code: 800A0005 is "Access Denied".  Check the file permissions of the directories you are trying to delete and make sure the user you're running the code as has the proper access to all of them.


-exx
0
 

Author Comment

by:jcb431
ID: 34070199
Thanks for the reply. I'll give it a shot and reply soon.
0
 

Author Comment

by:jcb431
ID: 34535679
Still unresolved. There are workarounds. I appreciate both of your efforts. Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

15 Experts available now in Live!

Get 1:1 Help Now