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
628 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is there a better way to combine two queries 7 26
SQL Activity Monitor detail 2 25
TSQL Challenge... 7 35
Where is the Help Section? 8 26
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

860 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