Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

PowerSQL By Prashanth Jayaram

Technology enthusiast with 7+ years of experience in Database Technology. I am Microsoft Certified Professional with certificates of OCA, MCP, MCTS, MCITP developer, MCITP administration and backed with a degree in Master of Computer Application. My expertise lies in T-SQL programming, Replication, PowerShell and Performance Tuning. Hobbies are Drawing, playing soccer and listening to Melodies songs.

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


Comments

Leave a comment on the original post [sqlpowershell.wordpress.com, opens in a new window]

Loading comments...