+ Reply to Thread
Results 1 to 8 of 8
  1. Senior Member
    Join Date
    Apr 2013
    Location
    Yorkshire, England
    Posts
    118

    Certifications
    MCSA Windows Server 2012 R2, MCSE Messaging
    #1

    Cool Advice on deleting entire directory

    Hi All,

    I'm wondering if anyone could offer some advice on a script that would help me do the following:

    I have a directory on a windows server 2012 r2 server named staging. Staging contains folders 1-1000 and each of these folders contain subfolders 1-1000 (1,000,000 folders in total). A lot of these subfolders contain around 100 0-2kb files.
    We copied this directory a bit ago to a new SSD volume as it wasn't doing what it was supposed to very quickly.

    We now want to clear up the original staging folder, but each of the 1000 top level folders take about half an hour each and the system starts performing quite slow while this is happening.

    My question is, is there a script or batch file I can run which will delete the first top level folder, then say an hour later when the scheduled task runs again delete the next folder and so on?
    So I want something that will look in staging and take folder 1, delete it and it's contents. Then when it next runs it will take folder 2 and do the same. Folder 3, folder 4 etc.

    I thought something like:

    cd c:\staging
    set folder=%variable that looks for the first folder%
    rmdir /s /q %folder%

    Hope someone can help
    Reply With Quote Quote  

  2. SS -->
  3. The ceiling is glass. PJ_Sneakers's Avatar
    Join Date
    Nov 2014
    Location
    169.254.0.1
    Posts
    759

    Certifications
    AccessData, Cellebrite, CompTIA, EC-Council, IACRB, (ISC)˛, Microsoft, MSAB
    #2
    So you just want to get rid of C:\staging?

    Does "rd /s c:\staging" not work in your situation?
    Reply With Quote Quote  

  4. Senior Member
    Join Date
    Apr 2013
    Location
    Yorkshire, England
    Posts
    118

    Certifications
    MCSA Windows Server 2012 R2, MCSE Messaging
    #3
    Quote Originally Posted by PJ_Sneakers View Post
    So you just want to get rid of C:\staging?

    Does "rd /s c:\staging" not work in your situation?
    Yes, kind of. But I want it to delete the subfolders gradually so it does not slow the system down too much for too long.

    I think I may just set up a scheduled task to delete staging, but make sure it only runs for half an hour or so at a time so it will gradually get through the folders but will keep stopping.

    Thanks
    Reply With Quote Quote  

  5. The ceiling is glass. PJ_Sneakers's Avatar
    Join Date
    Nov 2014
    Location
    169.254.0.1
    Posts
    759

    Certifications
    AccessData, Cellebrite, CompTIA, EC-Council, IACRB, (ISC)˛, Microsoft, MSAB
    #4
    When you say deleting it takes a half hour, are you doing it in explorer? I'm pretty sure rd /s is just going to delete the pointer to C:\staging, which shouldn't take long at all. It's like the old deltree in DOS 5.
    Reply With Quote Quote  

  6. Senior Member wd40's Avatar
    Join Date
    May 2007
    Location
    Bahrain
    Posts
    904

    Certifications
    CISA, eJPT, CompTIA x 6, MCP, MCTS
    #5
    I think it have to delete files within the folders first, with thousands of small files it will take time.

    a bug in arcserv killed our file server by creating millions of small files, I used BartPE and opened several cmd prompts (I think more than 12) and started deleting different set of sub-folders in each cmd prompt. not really sure if this is faster than a single delete operation, it took us several hours to clean the server up.

    May be you can try that after work hours (without shutting down the server of course).
    Reply With Quote Quote  

  7. user.Status = "Learning";
    Join Date
    Sep 2005
    Location
    Server Room
    Posts
    317

    Certifications
    MCSE Server 2012, MCSE CP&I
    #6
    You're on 2012 R2...PowerShell it. This is a super simple example that would probably need some polishing for your environment, but something along the lines of:

    $folder = C:\TopLevelBadFolder
    $folders = GCI($folder) #Gets a list of the subfolders

    if ($folders.Count -gt 0) #checks if there are any subfolders
    {
    try { Remove-Item -Path $folders[0].FullName -Force -Recurse } # deletes the first subfolder in the list and anything inside of it
    catch { } #be notified of a failure, etc.
    }
    elseif ($folders.Count -eq 0) #no more subfolders
    {
    try { Remove-Item -Path $folder -Force -Recurse } # deletes the top level folder and any files at its level
    catch { } #be notified of a failure, etc.
    }


    Manually run as desired it or task it (be advised, PowerShell and Task Scheduler don't always play nice straight out of the gate).

    -EDIT-

    My bad...coffee was still kicking in this morning. First two lines should be...

    $folder = "C:\xxxxxxxx" #whatever folder is the problem
    $folders = GCI ($folder) | Where { $_.Attributes -eq "Directory" }


    That will select only the subfolders and not every pesky little file that may be at the root of the top level folder.
    Last edited by GSXR750K2; 08-12-2016 at 03:37 PM.
    Reply With Quote Quote  

  8. Questionably Benevolent Moderator Slowhand's Avatar
    Join Date
    Oct 2005
    Location
    Bay Area, CA
    Posts
    5,072
    Blog Entries
    1

    Certifications
    A+, Linux+, Server+, Security+, MCSA 2003, MCSA 2008, MCSA 2012, CCNA(expired), ITIL Foundation v3 (2011), VCP5-DCV, VCA-Cloud, VCA-DCV, VCA-WM
    #7
    If you're looking to delete all those subdirectories and files without putting too much of a strain on the CPU and memory, you might want to think about using robocopy's mirror mode. When robocopy runs through files to copy, skip, or delete, it bypasses Indexing, which is usually the process that eats up your processor and memory resources.

    The basic idea is that you'd create an empty directory and then mirror it into C:\Staging. For example, if you created an empty directory called C:\Empty, like this:

    Code:
    robocopy C:\Empty C:\Staging /MIR /W:1 /R:3 /XJ
    The /MIR tag mirrors the empty directory, marking all files and folders inside C:\Staging for immediate deletion, /W:1 tells robocopy to wait one second before retrying if it hits a file in use, /R:3 tells it to retry three times if a file is locked or in use before moving on to the next one, and /XJ excludes junction points, preventing nasty loops if there are pointers to other directories. (This is also useful if you end up needing to delete a directory structure that's too many levels deep for Windows to handle.)

    It's worth a shot, if you're finding that other methods aren't working.

    -------------------------------------------------------
    ITHumidor.net - "Futuaris nisi irrisus ridebis"
    -------------------------------------------------------

    Free Microsoft Training: Microsoft Virtual Academy
    Free PowerShell Resources: Top 50 PowerShell Blogs
    Free DevOps/Azure Resources: Visual Studio Dev Essentials

    Let it never be said that I didn't do the very least I could do.
    Reply With Quote Quote  

  9. Senior Member
    Join Date
    Apr 2013
    Location
    Yorkshire, England
    Posts
    118

    Certifications
    MCSA Windows Server 2012 R2, MCSE Messaging
    #8
    I think we might just ignore it for now, we have plenty of space free on the volume so its not really an issue.

    We are building a DR environment which may end up becoming live, and then we would be rebuilding our current live anyway.
    Reply With Quote Quote  

+ Reply to Thread

Social Networking & Bookmarks