Problem solve Get help with specific problems with your technologies, process and projects.

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


declare @cmd varchar(1000)

set @cmd = 'move c:dataiwdataall.txt "c:dataoldiwdataall_' +
convert(varchar, datepart(year, getdate())) +
when convert(varchar, datepart(month, getdate())) < 10
then '0' + convert(varchar, datepart(month, getdate()))
else convert(varchar, datepart(month, getdate())) end +
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


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

select @daterange = 
convert(varchar, datepart(year, getdate() - 30)) + 
when convert(varchar, datepart(month, getdate() - 30)) < 10
then '0' + convert(varchar, datepart(month, getdate() - 30))
else convert(varchar, datepart(month, getdate() - 365)) end + 
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)) + 
when convert(varchar, datepart(month, getdate() - 14)) < 10
then '0' + convert(varchar, datepart(month, getdate() - 14))
else convert(varchar, datepart(month, getdate() - 14)) end +
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 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.

Dig Deeper on SQL Server Database Modeling and Design

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.