Dynamically manipulating files using xp_cmdshell

Dynamically build and execute OS commands within an SQL script.

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 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
This Content Component encountered an error

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

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:

-ADS BY GOOGLE

SearchBusinessAnalytics

SearchDataCenter

SearchDataManagement

SearchAWS

SearchOracle

SearchContentManagement

SearchWindowsServer

Close