script to check file

  • Does anyone have any script that deletes say .bak files from a directory after checking if its been archived or not. I am trying to create a job that creates database backups after deleting any existing backups thats been archived. any help on this will be greatly appreciated.

    TIA

  • You can use the extended sproc xp_getfiledetails to capture the information you need.

    Create a temp table:

    CREATE TABLE #ATTRIBS (

     alternate_name  VARCHAR(128),

        INT,

     creation_date  INT,

     creation_time  INT,

     last_written_date INT,

     last_written_time INT,

     last_accessed_date INT,

     last_accessed_time INT,

     attributes  INT)

    -- Check the archive bit to determine if the file has been archived.

        INSERT INTO #ATTRIBS EXEC master.dbo.xp_getfiledetails @BACKUPFILE

        IF ((SELECT attributes&32 FROM #ATTRIBS) != 32)

        -- if the value of the bitwise operation isn't 32 then the archive flag has been flipped and you can delete the @backupfile using xp_cmdshell

        BEGIN 

           --put your code in here

        END

    Hope this points you in the direction

    MG

    "There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies."
    Tony Hoare

    "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair.

Viewing 2 posts - 1 through 1 (of 1 total)

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