Loading ...
Sorry, an error occurred while loading the content.

Simple deletion script not working correctly under Server 2003

Expand Messages
  • Bushy
    Hi all batch gurus, The below script is pretty much taken straight from Timo s FAQ. It doesn t output the correct directory structure. It will be blatantly
    Message 1 of 3 , Aug 24, 2011
    • 0 Attachment
      Hi all batch gurus,

      The below script is pretty much taken straight from Timo's FAQ.
      It doesn't output the correct directory structure. It will be blatantly
      obvious to everyone, but not to me.

      Very quickly, the following is a location where backups are dumped
      nightly, into directory names that are date generated.
      Under each of these directories are around 4 gig of nightly SQL backup
      files.
      Each of the dated directory names are created at 10pm on that particular
      date, so they each have a creation date based on their filename.
      At the moment there is around two months worth, and the need to remember
      to manually go in and delete chunks.
      My goal is to automate the process and just to keep the last ten days worth.


      Direction location example:
      \\tamarang\e$\lpscadmin\e\sql\22082011\model.bak
      \\tamarang\e$\lpscadmin\e\sql\22082011\tracerassets.bak
      \\tamarang\e$\lpscadmin\e\sql\22082011\PowerBudgetNew.bak
      \\tamarang\e$\lpscadmin\e\sql\22082011\SoftwareSupport.bak
      \\tamarang\e$\lpscadmin\e\sql\23082011\model.bak
      \\tamarang\e$\lpscadmin\e\sql\23082011\tracerassets.bak
      \\tamarang\e$\lpscadmin\e\sql\23082011\PowerBudgetNew.bak
      \\tamarang\e$\lpscadmin\e\sql\23082011\SoftwareSupport.bak
      \\tamarang\e$\lpscadmin\e\sql\24082011\model.bak
      \\tamarang\e$\lpscadmin\e\sql\24082011\tracerassets.bak
      \\tamarang\e$\lpscadmin\e\sql\24082011\PowerBudgetNew.bak
      \\tamarang\e$\lpscadmin\e\sql\24082011\SoftwareSupport.bak
      \\tamarang\e$\lpscadmin\e\sql\25082011\model.bak
      \\tamarang\e$\lpscadmin\e\sql\25082011\tracerassets.bak
      \\tamarang\e$\lpscadmin\e\sql\25082011\PowerBudgetNew.bak
      \\tamarang\e$\lpscadmin\e\sql\25082011\SoftwareSupport.bak

      Using Timo's original example taken straight from his FAQ, it doesn't
      dump anything since it was only for files within a single directory.
      A further '/s' parameter is required before it dumps anything into the
      created 'CLEANUP_SQL_DIR.CMD' output command batch file.
      However even then it is missing the 'dated' directories, and I don't
      know how to add it within the %targetdir%.
      It is these 'dated' directories that I am missing, and do not know how
      to add it.


      The following is the output of the created batch file 'CLEANUP_SQL_DIR.CMD'
      [ MORE \SCRIPTS\CLEANUP_SQL_DIR.CMD ]

      del "e:\lpscadmin\e\sql\tracerassets.bak"
      del "e:\lpscadmin\e\sql\SoftwareSupport.bak"
      del "e:\lpscadmin\e\sql\model.bak"
      del "e:\lpscadmin\e\sql\PowerBudgetNew.bak"
      del "e:\lpscadmin\e\sql\tracerassets.bak"
      del "e:\lpscadmin\e\sql\SoftwareSupport.bak"
      del "e:\lpscadmin\e\sql\model.bak"
      del "e:\lpscadmin\e\sql\PowerBudgetNew.bak"
      del "e:\lpscadmin\e\sql\tracerassets.bak"
      del "e:\lpscadmin\e\sql\SoftwareSupport.bak"
      del "e:\lpscadmin\e\sql\model.bak"
      del "e:\lpscadmin\e\sql\PowerBudgetNew.bak"
      del "e:\lpscadmin\e\sql\tracerassets.bak"
      del "e:\lpscadmin\e\sql\SoftwareSupport.bak"
      del "e:\lpscadmin\e\sql\model.bak"
      del "e:\lpscadmin\e\sql\PowerBudgetNew.bak"

      It is completely missing the 'dated' sub directories after \lpscadmin\e\sql\

      I wish to keep it pure batch, rather than using any visual basic
      scripting if it is at all possible.
      Anyone have any clues?

      cheers
      dave

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

      :: Should create batch file "c:\scripts\cleanup_sql_dir.cmd"
      :: which will remove everything but last 10 files created (creation date)
      ::
      :: \\tamarang\e$\lpscadmin\e\sql
      ::
      @echo off & setlocal enableextensions

      set targetdir_=e:\lpscadmin\e\sql
      if not exist "%targetdir_%\*.*" (
      echo Folder "%targetdir_%" not found
      goto :EOF)

      set script_="c:\scripts\cleanup_sql_dir.cmd"
      if exist %script_% del %script_%

      for /f "skip=10 delims=" %%f in (
      'dir /a:-d /b /s /o:-d "%targetdir_%\*.*"') do (
      echo del "%targetdir_%\%%~nf%%~xf">>%script_%)

      endlocal & goto :EOF
    • foxidrive
      ... This has a couple of changes but should keep 10 of the latest folders. The syntax should be to remove the folders using RD rather than deleting files.
      Message 2 of 3 , Aug 24, 2011
      • 0 Attachment
        On 25/08/2011 10:02, Bushy wrote:

        >
        > Very quickly, the following is a location where backups are dumped
        > nightly, into directory names that are date generated.
        > Under each of these directories are around 4 gig of nightly SQL backup
        > files.
        > Each of the dated directory names are created at 10pm on that particular
        > date, so they each have a creation date based on their filename.
        > At the moment there is around two months worth, and the need to remember
        > to manually go in and delete chunks.
        > My goal is to automate the process and just to keep the last ten days worth.
        >
        >
        > Direction location example:
        > \\tamarang\e$\lpscadmin\e\sql\22082011\model.bak
        > \\tamarang\e$\lpscadmin\e\sql\22082011\tracerassets.bak
        > \\tamarang\e$\lpscadmin\e\sql\22082011\PowerBudgetNew.bak
        > \\tamarang\e$\lpscadmin\e\sql\22082011\SoftwareSupport.bak
        > \\tamarang\e$\lpscadmin\e\sql\23082011\model.bak
        > \\tamarang\e$\lpscadmin\e\sql\23082011\tracerassets.bak
        > \\tamarang\e$\lpscadmin\e\sql\23082011\PowerBudgetNew.bak
        > \\tamarang\e$\lpscadmin\e\sql\23082011\SoftwareSupport.bak
        > \\tamarang\e$\lpscadmin\e\sql\24082011\model.bak
        > \\tamarang\e$\lpscadmin\e\sql\24082011\tracerassets.bak
        > \\tamarang\e$\lpscadmin\e\sql\24082011\PowerBudgetNew.bak
        > \\tamarang\e$\lpscadmin\e\sql\24082011\SoftwareSupport.bak
        > \\tamarang\e$\lpscadmin\e\sql\25082011\model.bak
        > \\tamarang\e$\lpscadmin\e\sql\25082011\tracerassets.bak
        > \\tamarang\e$\lpscadmin\e\sql\25082011\PowerBudgetNew.bak
        > \\tamarang\e$\lpscadmin\e\sql\25082011\SoftwareSupport.bak
        >

        This has a couple of changes but should keep 10 of the latest folders.
        The syntax should be to remove the folders using RD rather than deleting files.

        (untested)

        @echo off & setlocal enableextensions

        set targetdir_=e:\lpscadmin\e\sql

        if not exist "%targetdir_%\" (
        echo Folder "%targetdir_%" not found
        goto :EOF)

        set script_="c:\scripts\cleanup_sql_dir.cmd"
        if exist %script_% del %script_%

        for /f "skip=10 delims=" %%f in (
        'dir /a:d /b /o:-d "%targetdir_%"') do (
        echo rd /s /q "%targetdir_%\%%~nxf">>%script_%
        )

        endlocal & goto :EOF
      • Bushy
        ... That worked a treat. Your a legend. Thankyou! (Flippin stupid me trying to do it with DEL. I didn t even put any thought into that at all; didnt even think
        Message 3 of 3 , Aug 25, 2011
        • 0 Attachment
          >Posted by: "foxidrive"

          > This has a couple of changes but should keep 10
          > of the latest folders. The syntax should be to
          > remove the folders using RD rather than deleting files.

          That worked a treat. Your a legend. Thankyou!

          (Flippin stupid me trying to do it with DEL. I didn't even put any
          thought into that at all; didnt even think about RD /S.)
        Your message has been successfully submitted and would be delivered to recipients shortly.