+ Post New Thread
Results 1 to 3 of 3
Scripts Thread, Script to delete folder and all sub-folders in a shared folder in Coding and Web Development; Hey guys, This one should get the juices flowing. Here's what i need. I need a script that will go ...
  1. #1
    JPhillips173's Avatar
    Join Date
    Apr 2008
    Location
    NJ, USA
    Posts
    1
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

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

    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
    JPhillips@monet.prs.k12.nj.us
    System Admin
    Princeton Regional Schools

  2. #2

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,159
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    125
    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 :-)

  3. Thanks to srochford from:

    psydii (21st March 2013)

  4. #3
    originofsymmetry's Avatar
    Join Date
    Jun 2007
    Posts
    188
    Thank Post
    9
    Thanked 15 Times in 12 Posts
    Rep Power
    18
    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.



SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 5
    Last Post: 1st July 2014, 09:34 AM
  2. Replies: 3
    Last Post: 26th October 2012, 03:15 PM
  3. Replies: 8
    Last Post: 17th November 2007, 10:04 PM
  4. Replies: 15
    Last Post: 12th October 2006, 10:37 PM
  5. Replies: 3
    Last Post: 25th September 2006, 01:27 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •