Jump to content

Script to delete folder and all sub-folders in a shared folder


Recommended Posts

Posted

Hey guys,

 

This one should get the juices flowing. Here's what i need.

 

I need a script that will go into a shared folder where all our students home folders are and delete a folder and all it's sub-folders inside of each users home folder. So it will have to support wildcards because i can't put a line of code for each 1200 students. Also it has to run on a windows 2003 server, and be able to be called automatically before our backup jobs run every night.

 

Here's the path:

 

e:\students\(usernames)\library\caches

e:\students\(username)\library\application support\firefox\profiles

 

(username) being the wildcard so it will parse every folder and find the sub-folders and delete them.

 

Also last year i started grouping the home folders by graduation year to make it easier to remove once they graduate, so there is also a sub-folder that has all of the sophmore home folders in it.

 

Here's it's path:

e:\students\2010\(username)\library\caches

e:\students\2010\(username)\library\application support\firefox\profiles

 

I know this is a complicated script and i found one from the windows "scripting guys" but i don't know if it will do wildcards and if it does what they are. (maybe *)

 

Here's the script they gave me:

--------------------------------------------------------

Dim arrFolders()

intSize = 0

 

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

 

strFolderName = "c:\scripts\archive"

 

Set colSubfolders = objWMIService.ExecQuery _

("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _

& "Where AssocClass = Win32_Subdirectory " _

& "ResultRole = PartComponent")

 

ReDim Preserve arrFolders(intSize)

arrFolders(intSize) = strFolderName

intSize = intSize + 1

 

For Each objFolder in colSubfolders

GetSubFolders strFolderName

Next

 

Sub GetSubFolders(strFolderName)

Set colSubfolders2 = objWMIService.ExecQuery _

("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _

& "Where AssocClass = Win32_Subdirectory " _

& "ResultRole = PartComponent")

 

For Each objFolder2 in colSubfolders2

strFolderName = objFolder2.Name

ReDim Preserve arrFolders(intSize)

arrFolders(intSize) = strFolderName

intSize = intSize + 1

GetSubFolders strFolderName

Next

End Sub

 

For i = Ubound(arrFolders) to 0 Step -1

strFolder = arrFolders(i)

strFolder = Replace(strFolder, "\", "\\")

Set colFolders = objWMIService.ExecQuery _

("Select * from Win32_Directory where Name = '" & strFolder & "'")

 

For Each objFolder in colFolders

errResults = objFolder.Delete

Next

Next

-------------------------------------------------------------------------

 

I know this is complicated script but they said that the script must delete all of the sub-folders before it will delete the root folder.

If i've left out any important info let me know.

 

Thanks for the help,

 

Jon Phillips

[email protected]

System Admin

Princeton Regional Schools

Posted

Wow!

 

Might not need to be that difficult - sometimes batch files are easier ...

 

e:

cd \students

for /d %%i in (*) do rd "e:\students\%%i\library\caches" /s /q

for /d %%i in (*) do rd "e:\students\%%i\application support\firefox\profiles" /s /q

 

Not sure if you want to delete the contents of each folder but leave the folder - if so, it's probably easier to just delete the folder and its contents and then put back an empty folder:

 

for /d %%i in (*) do md "e:\students\%%i\library\caches"

for /d %%i in (*) do md "e:\students\%%i\application support\firefox\profiles"

 

I'll dig out some vbscript later but my tea's ready now :-)

  • Thanks 1
Posted
Don't mean to hijack here but I would like a slight modification to this script as I need to copy a couple of folders into every users home directory, don't know how to set up the (*) wildcard properly though, any help appreciated.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...