Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-04-14
11
Medium Priority
?
655 Views
Last Modified: 2012-06-27
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)
0
Comment
Question by:vijay11
  • 6
  • 3
  • 2
11 Comments
 

Expert Comment

by:Ultrabrowser
ID: 24138047
Add the below 2 lines to a deleteBAK.bat file and your all set.

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

Expert Comment

by:Ultrabrowser
ID: 24138061
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
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24138075
just few hours back I created one script and give to one poster at

http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_24319038.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.

0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 31

Expert Comment

by:RiteshShah
ID: 24138088
Ultrabrowser, author want to do this with xp_cmdshell and from sql job not from .bat file and run that .bat manually.
0
 

Author Comment

by:vijay11
ID: 24138191
Hey ritesh i want to delete all the .bak files irrespective of age of .bak files.is there any simple script
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24138257
what are the name of your files? is it dynamic or you create it manually?
0
 

Author Comment

by:vijay11
ID: 24138281
dynamically created by maintainance plan
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24138342
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

0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 1500 total points
ID: 24138357
sorry forget to declare @Path variable, use this one.

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

Author Comment

by:vijay11
ID: 24138631
thanks Ritesh ,

                            It works fine.can you please tell me what do /Q /S mean in ''ERASE /Q /S '+@Path+'\*.bak''
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24139035
/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.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question