Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a simple script I can use to copy files in a particular folder to a network share? This is for use with SQL Server Agent so I can run it as a step after backups.


share|improve this question
Not exactly what you're asking, but I blogged something very similar just today: – spaghettidba Feb 8 '13 at 15:44
up vote 1 down vote accepted

This is (part of) a script that I use to "archive" files that were processed by SQL Server. I actually mapped the network drive instead of moving to a UNC because it was just more stable.

A few things to note:

  • The service account under which the SQL Agent runs must have permissions on both the source and the target (I'm assuming the target would be the one you'd really need to check, unless the Agent account can't delete from the local store).
  • This script MOVEs the files. If you want to copy them, then I'd recommend looking into replacing the command in the last step with XCOPY

    EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', 
       @type=N'LOCAL',@name=N'[Uncategorized (Local)]'

    IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback


    DECLARE @jobId BINARY(16)
    EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'File Mover', 
            @description=N'No description available.', 
            @category_name=N'[Uncategorized (Local)]', 
            @notify_email_operator_name=N'alertteam', @job_id = @jobId OUTPUT

    EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Archive Files', 
            @os_run_priority=0, @subsystem=N'CmdExec', 
            @command=N'move \source\path\*.csv \dest\path
share|improve this answer
swasheck's answer will create you a new job with a copy step inside. If you just want to add a new step to an existing backup job then take the last part (with sp_add_jobstep), but be sure to add it in the context of the already existing backup job. – Marian Feb 8 '13 at 21:37
good point. i missed the "backup" part – swasheck Feb 8 '13 at 21:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.