SQLCMD –Quick Copy of Files – Using Robocopy

The requirement is to move the files to different server programatically and hence I’ve decided to use Robocopy in SQLCMD.

Robocopy is a command line utility. It has been available as part of the windows resource kit and It’s one of the fastest way to copy files across network.

–Robocopy syntax is different from standard copy commands, as it accepts only folder names as its source and destination arguments.

— We can use the wild-card characters (such as “*.*”) as its third parameters(File lists)

–For example, to copy all files which starts UAT from directory g:\MSSQL to G:\MSSQL, one could use the following syntax:

Robocopy G:\MSSQL G:\MSQL1\ UAT*.*

SQLCMD can be enabled in Query Editor. Go to SSMS Menu -> Query ->Select SQLCMD.

Image

copy and paste the below code SSMS

:SETVAR DATABASE UAT

:SETVAR SOURCE HQSPDBSU01

:SETVAR DESTINATION HQSPDBSU02

:SETVAR SOURCEPATH G:\MSSQL

:SETVAR COPYPATH G$\MSSQL

SET NOCOUNT ON

GO

:CONNECT $(SOURCE)

print ‘Files in sourcePath’

!!dir $(SourcePath)\*.*

Go

print ‘Files in Destination Path’

:CONNECT $(DESTINATION)

!!dir $(SOURCEPATH)\*.*

Go

:CONNECT $(SOURCE)

print ‘*** Copy database $(DATABASE) from Source server $(Source) to Destination server $(destination) ***’

!!ROBOCOPY $(SOURCEPATH)\ \\$(DESTINATION)\$(COPYPATH) $(DATABASE)*.*

GO

print ‘Files in Destination Path’

:CONNECT $(DESTINATION)

!!dir $(SOURCEPATH)\*.*

Go

print ‘Files in source Path’

:CONNECT $(SOURCE)

!!dir $(SOURCEPATH)\*.*

Download the code here  Robocopy

Output:

Image

Thanks for reading my space.

Happy Learning

Advertisements

About Prashanth Jayaram

I’m a Database technologist having 11+ years of rich, hands-on experience on Database technologies. I am Microsoft Certified Professional and backed with a Degree in Master of Computer Application. My expertise lies in T-SQL programming, Replication and PowerShell You can connect me via https://social.technet.microsoft.com/profile/prashanth jayaram/ http://www.sqlshack.com/author/prashanth/ https://www.tumblr.com/blog/prashantjayaram http://www.sqlservercentral.com/blogs/powersql-by-prashanth-jayaram/ http://www.toadworld.com/members/prashanthjayaram/ My Articles are published in following sites http://www.ssas-info.com/analysis-services-articles/ http://db-pub.com/ http://www.sswug.org/sswugresearch/community/
This entry was posted in Robocopy, SETVAR, SQLCMD, T-SQL and tagged , , . Bookmark the permalink.

3 Responses to SQLCMD –Quick Copy of Files – Using Robocopy

  1. Pingback: SQLCMD – Database Backup and Restore – Automation – Quickest and Simplest method | Prashanth Jayaram

  2. Pingback: SQLCMD – Database Backup and Restore – Automation – Quickest and Simplest method - SQL Server - SQL Server - Toad World

  3. Pingback: What is xCopy and Robocopy? - Techxpress.in

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s