Tip

Dynamically manipulating files using xp_cmdshell

Every MS SQL Server expert knows you can run OS commands within a SQL script using xp_cmdshell. But not everyone realizes how powerful it is to able to dynamically build and execute such commands within

    Requires Free Membership to View

a SQL script.

Below are two stored procedures I created at my job, which run as part of a scheduled nightly import job. The first one (move_old_iwdataall) moves the import source text files into an archive directory, appending the date/time of job execution onto their filenames (thus creating an ongoing archive of dated imports). The second (delete_old_iwdataall) goes into the archive folder and deletes files that are older than 14 days.

/*stored proc 1: move and date-label the import file*/

create procedure move_old_iwdataall

as

declare @cmd varchar(1000)

set @cmd = 'move c:dataiwdataall.txt "c:dataoldiwdataall_' +
convert(varchar, datepart(year, getdate())) +
case
when convert(varchar, datepart(month, getdate())) < 10
then '0' + convert(varchar, datepart(month, getdate()))
else convert(varchar, datepart(month, getdate())) end +
case
when convert(varchar, datepart(day, getdate())) < 10
then '0' + convert(varchar, datepart(day, getdate()))
else convert(varchar, datepart(day, getdate())) end
+ ' ' + convert(varchar, datepart(hour, getdate()))
+ '-' + convert(varchar, datepart(minute, getdate()))
+ '-' + convert(varchar, datepart(second, getdate())) + '.txt"'

exec master..xp_cmdshell @cmd

/*stored proc 2: delete import archives older than 
  14 days (within past 30 days)*/

create procedure delete_old_iwdataall

as

declare 
@daterange varchar(2000),
@cmd varchar(1000)

select @daterange = 
convert(varchar, datepart(year, getdate() - 30)) + 
case
when convert(varchar, datepart(month, getdate() - 30)) < 10
then '0' + convert(varchar, datepart(month, getdate() - 30))
else convert(varchar, datepart(month, getdate() - 365)) end + 
case
when convert(varchar, datepart(day, getdate() - 30)) < 10
then '0' + convert(varchar, datepart(day, getdate() - 30))
else convert(varchar, datepart(day, getdate() - 30)) end + ',1,' + 
convert(varchar, datepart(year, getdate() - 14)) + 
case
when convert(varchar, datepart(month, getdate() - 14)) < 10
then '0' + convert(varchar, datepart(month, getdate() - 14))
else convert(varchar, datepart(month, getdate() - 14)) end +
case
when convert(varchar, datepart(day, getdate() - 14)) < 10
then '0' + convert(varchar, datepart(day, getdate() - 14))
else convert(varchar, datepart(day, getdate() - 14)) end

set @cmd = 'for /L %i in (' + @daterange + ') 
            do del "c:dataoldiwdataall_*%i*.txt"'

exec master..xp_cmdshell @cmd

For More Information

  • What do you think about this tip? E-mail us at editor@searchDatabase.com with your feedback.
  • The Best SQL Server Web Links: tips, tutorials, and much more.
  • Have another SQL Server tip to offer your fellow DBA's and developers? The best tips submitted will receive a cool prize--submit your tip today!
  • Do you have any technical questions about SQL Server administration or development? Post them--or help out your peers by answering them--in our live discussion forums.
  • Check out our new Ask the Experts feature! Our MS SQL Server gurus are waiting to answer your toughest SQL Server questions.

This was first published in February 2005

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.