Surendra Sharma

Surendra Sharma

Search This Blog

Showing posts with label Robocopy. Show all posts
Showing posts with label Robocopy. Show all posts

Tuesday, October 25, 2016

ROBOCOPY : Auto Push static files to hosting environments



Developers are working on their development machine during website development phase. Once they are done with their file changes, they are going to publish those changes. 
They can do publishing at any time without any efforts. But if solutions have lots of projects then this process take times. 

Can we avoid and minimize this time and automate this process at least for static files like 
images, HTML, views, aspx, css, js etc.?

Answer is YES. We can automate this process to sync development static files folders with Published folders.

Even we can push development change static files to QA and staging environment folders which are across the network.

Sync
Sync


How to push the changes?

I am using a batch file with ROBOCOPY command in my development machine for this.

ROBOCOPY stand for robust file copy and this command allows you to copy files, directories, and even drives from one location to another.

Robocopy monitor the source folder and if any change happened in that folder like new file created, file deleted, updated etc. then Robocopy takes all the files and replace those files on destination folder.

Syntax : ROBOCOPY source destination [options]

Create a batch file in solution root folder and put below script 

@ECHO OFF

REM this batch file uses robocopy to monitor certain folders in the web projects for changes
REM and copies the changes to the web directory to save time when editing non compiled
REM files such as javascript CSS and cshtml

SET SITEROOT=C:\inetpub\wwwroot\SitecoreLessons\Website
SET SLNROOT=%~dp0
SET OPTS=/MIR /MON:1 /MOT:1 /NJS /NDL /NP /NS /NC

START ROBOCOPY "%SLNROOT%\SitecoreLessons\Assets" "%SITEROOT%\Assets" %OPTS%

REM You can put multiple Robocopy command here like

REM START ROBOCOPY "%SLNROOT%\SitecoreLessons\Views" "%SITEROOT%\Views" %OPTS%


@EXIT

Here is the description of this batch file for those who don’t have any knowledge of batch files

ECHO -> Used to print on output device
REM -> Used to write comment in batch file
SET -> Used for declaring the variable like SITEROOT variable is used to store my destination folder path.
%~dp0 -> Current directory path from where this batch file is running

I have defined “OPTS” variable which stores ROBOCOPY command options


Options
Description
MIR
MIRror a directory tree
MON:1
MONitor source. Run again when more than n changes seen.
MOT:1
MOnitor source. Run again in m minutes Time, if changed.
NJS
No Job Summary.
NDL
No Directory List - don't log directory names.
NP
No Progress - don't display percentage copied.
NS
No Size - don't log file sizes.
NC
No Class - don't log file classes.



You can get more option from http://www.computerhope.com/robocopy.htm

Double click on this batch file to start monitor our source “Assets” folder as

Batch File
Batch File


I have added “Assets\Images” folder in my VS projects and paste some image files in this folder. 

Note: - I have not build or publish my VS solution till now. 

But if I wait for 1 min. I got new all image files in my “C:\inetpub\wwwroot\SitecoreLessons\Website\Assets\Images” folder automatically.

Sync Images
Sync Images


Wowww. This is cool. Now I can write more Robocopy command in same batch file for copying Views, ASPX, HTML files etc.

I hope this pointer helps you to improve your productivity in your development environment.

Please leave your comments and share this trick with other developers.