Blog Post

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

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating