Deleting folder on a drive

  • I would approach this differently - I would create a powershell script that removes the empty directories and schedule that to run using the powershell subsystem in SQL Server agent, or as a task on the server.

    Get-ChildItem -Directory $backupDirectory | Where-Object {$_.GetFileSystemInfos().Count -eq 0} | Remove-Item

    In the script - you would have to define the $backupDirectory and any other variables, but that shouldn't be too hard.  If you need to look into SQL Server you can use Invoke-SqlCmd to execute a query and return the results - or use a standard .NET connection strings to setup a call to a stored procedure with parameters.

    If you add the -WhatIf parameter to the end...the code runs but doesn't actually remove anything.  It just shows you what would happen if you ran the code.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • How are you checking that the folder still exists?  I've been "caught" more than once by Windows Explorer still displaying the folder until I did a refresh.

     

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • @Jeffrey - I did think about using PowerShell but wanted everything to be in one place.

     

    @jeff - That's very strange.  Yesterday, I refreshed the parent folder quite a few times and the folder in question was still there.

    This morning, it's gone.  Looks like Windows was doing something strange and not refreshing correctly (unless it was just taking some time to update the list of folders).  If I'm using the code correctly, I'll put it back into play (it's commented out for now while I sorted this out).

  • I figured.  You can tell that's "never" happened to me before, right? 😀  Thanks for the feedback.  Long live the command line! 😀

     

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Oh, yeah, I can tell 😀

    Did it cause you to lose hair as well?

Viewing 5 posts - 31 through 34 (of 34 total)

You must be logged in to reply to this topic. Login to reply