Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Dec 2009
    Location
    St. Joseph, MI, USA
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Automate file copy/delete on Win 2000 server

    I would like to automate a file task I perform every day. It involves deleting the oldest file (of three) in a folder on an Windows 2003 server, and moving a file from a Windows 2000 server to replace the one deleted. These are SQL Server backup files and include a date key in the file name that is incremented each day. I don't really have a problem with moving the new file in; that's not too hard; but deleting the oldest file is harder.

    1. The old file must be deleted first so that there will be room for the new backup file.

    2. Delete oldest file only if three files already exist in the folder.

    3. Files are named like:
    backup_201107132200.BAK
    backup_201107142200.BAK
    backup_201107152200.BAK

    Thank you for your suggestions.
    Bill B

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,162
    Thanks
    47
    Thanked 976 Times in 906 Posts
    You can do it with a batch file, or VBS etc. Here is a batch version.
    Code:
    for /f "skip=2 tokens=*" %%X in ('dir *.bak /b /o:d') do del %%X
    This performs a "dir" in date order on the backup files, skips the first 2 items and deletes the rest - in this case the 3rd, but it would also do the 4th etc if they existed.
    If you want to test this on a command line - not in a batch file - you need to drop one of the % signs.
    Code:
    for /f "skip=2 tokens=*" %X in ('dir *.bak /b /o:d') do echo del %X
    cheers, Paul
    Last edited by Paul T; 2011-07-20 at 13:30.

  3. #3
    New Lounger
    Join Date
    Dec 2009
    Location
    St. Joseph, MI, USA
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much, Paul. I'll play with this until I feel comfortable using it in a production environment.

    Regards,
    Bill

  4. #4
    New Lounger
    Join Date
    Dec 2009
    Location
    St. Joseph, MI, USA
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, Paul, It was working great in test. I deleted a couple of test files with my batch script, but when it came to the real thing, it chickened out.

    Here is my script, named "DeleteOldest.bat". I ran this from a command prompt in the folder directly above, hierarchically.

    ECHO OFF
    FOR /F "skip=2 tokens=*" %%X IN ('dir M2MDATA01\M2MDATA01_db_20*.bak /b /o:-d') DO DEL %%X
    DIR M2MDATA01 >> BatchBkupLog.txt

    And here is the result:
    D:\SDLSS\Utilities\Backup>ECHO OFF
    Could Not Find D:\SDLSS\Utilities\Backup\M2MDATA01_db_20110715220 0.BAK
    D:\SDLSS\Utilities\Backup>

    It obviously found the file in the FOR command, because it is named in the error text. I added the "-" filter to the :d switch to sort newest to oldest. And the file named in the error text is the oldest of the three files that existed in the directory. Why does it not find the same file in the Delete command?

    Bill

  5. #5
    New Lounger
    Join Date
    Dec 2009
    Location
    St. Joseph, MI, USA
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm going to try using the full path in each statement.

  6. #6
    Lounger
    Join Date
    Dec 2009
    Location
    Leiden, Netherlands
    Posts
    30
    Thanks
    2
    Thanked 4 Times in 2 Posts
    Hi Bill,


    if you copied the output correctly, you'll notice that there is a space in the filename to delete (M2MDATA01_db_20110715220 0.BAK).
    To solve that, enclose the variable in the DEL command in double quotes.
    Also, a FOR command (when executed against a list of files instead of the output of a command, like in this case) can return the values between quotes if they contain spaces.
    Therefore, it is good habit to prefix the varable with the tilde (~), which strips existing quotes, and then add them to make sure they are always available.
    More information on this can be found by issuing FOR /? in a command window.

    So, the line in your script should look like this:

    Code:
    FOR /F "skip=2 tokens=*" %%X IN ('dir M2MDATA01\M2MDATA01_db_20*.bak /b /o:-d') DO DEL "%%~X"
    Another improvement can be made, if you want the script to run from any location, or if you want to schedule it and do not want to rely on the "Current directory" setting in the scheduled task.
    As the first line, insert the following statement:

    Code:
    cd /d "%~dp0"
    This makes sure that the current directory is changed to the script's location (paramter %0 always exists, and contains the batch file's name, ~dp modifies that to the drive and path *only*, so D:\SDLSS\Utilities\Backup\DeleteOldest.bat is converted to D:\SDLSS\Utilities\Backup\ in the statement).

    As final note:
    If you want to make the script usable for any amount of files, but only want to delete the oldest (=last one in the list), this is a method based on your script:
    Code:
    @ECHO OFF
    cd /d "~%~dp0"
    FOR /F "tokens=*" %%X IN ('dir M2MDATA01\M2MDATA01_db_20*.bak /b /o:-d') DO set FILENAME=%%~X
    rem FILENAME now contains the last element
    if "%FILENAME%" neq "" DEL "%FILENAME%"
    DIR M2MDATA01 >> BatchBkupLog.txt
    Kind regards,
    Last edited by enjoy; 2011-07-22 at 05:36. Reason: Spelling correction
    - Eelco

    *** Puzzle me! ***

Tags for this Thread

Posting Permissions

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