Delete OS Files using sql server 2005

  • Hi,

    I am creating log file in c: in my procedure.

    I want check weather that file is exist and if yes i want to delete that file. Ho wI can do this?

    pls tell me

    Bhushan

  • use xp_cmdshell to fire OS level commands.

    xp_cmdshell 'del c:\testfile.txt'

    make sure xp_cmdshell is enabled at Sql Server 2005 surface area configuration.



    Pradeep Singh

  • You may want to look at this URL. contains code to delete files in a folder. you may want to do little twicking to delete certain files only.

    http://www.sqlservercentral.com/articles/Administering/usingxp_cmdshell/1250/



    Pradeep Singh

  • [font="Verdana"]Do read, the link that ps has posted. But, this code should help you out:

    SET NOCOUNT ON

    DECLARE @iFileExists INT

    create table #table (File_exists int, File_directory int,parent_dir int)

    --If the path has spaces in it, use double quotes eg. "D:\Program Files\Test.txt"

    insert into #table exec master..xp_fileexist "O:\Program Files\ApplicationName\App_log.txt"

    select @iFileExists=File_exists from #table

    If @iFileExists = 1

    Begin

    exec master..xp_cmdshell "Del O:\Program Files\ApplicationName\App_log.txt"

    End

    Else

    Print 'File Does not Exist!'

    drop table #table

    Regards,[/font]

    -Hope is a heuristic search :smooooth: ~Hemanth
  • THNX.....IT WORKS FINE

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

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