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
622 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now