Deleting files in a directory

  • I need to delete files in a directory on the SQL server depending on their filedates. Does anyone know of a way toi do this?

    Thanks!

  • See BOL for xp_cmdshell.

    _____________
    Code for TallyGenerator

  • You may need to store the files in a table first, eg.

    create table #files (file_properties varchar(1000))

     

    INSERT INTO #files (file_properties)

    EXEC master..xp_cmdshell 'DIR <switches and parameters>'

    The DOS command 'DIR' provides a switch that allows you to suppress header information and to list only files on that folder.

    When done, simply loop thru each record to retrieve the filename (using appropriate string functions), build the xp_cmdshell statement using 'DEL' to delete the file.

  • suppose you want to delete file test.txt in directory c:\tes.txt, so just copy this code :

    exec master..xp_cmdshell 'del  C:\tes.txt'

    xp_cmdshell  --> this command is used to do any operation data like in a command prompt 

Viewing 4 posts - 1 through 3 (of 3 total)

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