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
625 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 500 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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
Via a live example, show how to shrink a transaction log file down to a reasonable size.

777 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