Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

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)
0
vijay11
Asked:
vijay11
  • 6
  • 3
  • 2
1 Solution
 
UltrabrowserCommented:
Add the below 2 lines to a deleteBAK.bat file and your all set.

Echo Off
del c:\softwares\ *.bak /S /Q
0
 
UltrabrowserCommented:
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
 
RiteshShahCommented:
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
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

 
RiteshShahCommented:
Ultrabrowser, author want to do this with xp_cmdshell and from sql job not from .bat file and run that .bat manually.
0
 
vijay11Author Commented:
Hey ritesh i want to delete all the .bak files irrespective of age of .bak files.is there any simple script
0
 
RiteshShahCommented:
what are the name of your files? is it dynamic or you create it manually?
0
 
vijay11Author Commented:
dynamically created by maintainance plan
0
 
RiteshShahCommented:
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
 
RiteshShahCommented:
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
 
vijay11Author Commented:
thanks Ritesh ,

                            It works fine.can you please tell me what do /Q /S mean in ''ERASE /Q /S '+@Path+'\*.bak''
0
 
RiteshShahCommented:
/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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now