December 21, 2008 at 11:19 pm
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
December 21, 2008 at 11:30 pm
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.
December 21, 2008 at 11:55 pm
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/
December 22, 2008 at 12:01 am
[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]
December 22, 2008 at 12:11 am
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