Link to home
Start Free TrialLog in
Avatar of vijay11
vijay11

asked on

how can we delete all .bak files in a folder using xp_cmdshell i.i have to keep this in a job.

how can we delete all .bak files in a folder(ex:c:\softwares\) using xp_cmdshell
.i have to write  this script  in a job so that all .bak files are deleted.(sql server 2000)
Avatar of Ultrabrowser
Ultrabrowser

Add the below 2 lines to a deleteBAK.bat file and your all set.

Echo Off
del c:\softwares\ *.bak /S /Q
Make that

Add the below 2 lines to a deleteBAK.bat file and your all set.

Echo Off
del c:\softwares\ *.bak /Q

With out the /S switch
just few hours back I created one script and give to one poster at

https://www.experts-exchange.com/questions/24319038/how-to-delete-a-bak-file-using-a-job-in-sqlserver.html

you can refer the same and can modify as per your need. You can set that script in job. I used to delete only 24 hours old backup but your remove that restrictions.

Ultrabrowser, author want to do this with xp_cmdshell and from sql job not from .bat file and run that .bat manually.
Avatar of vijay11

ASKER

Hey ritesh i want to delete all the .bak files irrespective of age of .bak files.is there any simple script
what are the name of your files? is it dynamic or you create it manually?
Avatar of vijay11

ASKER

dynamically created by maintainance plan
use this.

SET @Path = 'C:\Software\';
DECLARE @cmdStr AS varchar(250);
SET @cmdStr = 'ERASE /Q /S '+@Path+'\*.bak'
EXEC master..xp_cmdshell @cmdStr, NO_OUTPUT

ASKER CERTIFIED SOLUTION
Avatar of RiteshShah
RiteshShah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vijay11

ASKER

thanks Ritesh ,

                            It works fine.can you please tell me what do /Q /S mean in ''ERASE /Q /S '+@Path+'\*.bak''
/Q means it will do its work quietly ;) generally when you manually delete it, than it ask for prompt want to delete it? yes or not, it won't ask anything.

/S will delete from sub directory also.