Friday, January 31, 2014

How to create a fully fledged file download script (.bat)



This time I am going to write about how to create a build (or file) download script (.bat) which you can use for day to day work since I could not found a good post with complete solution. 

Once upon a time, in our project, we had a serious issue regarding the build downloads. Since our teams are geographically dislocated, build is compiled in their location and we need to download the new build daily. 

Altogether, we have 3.5 GB data and it will take around 3.5 hours to download the builds which could not accepted. Due to the pain we had I thought of creating a build download script to download those automatically. Now we just need to install the downloaded builds in the morning.

So following are the exact steps which we need to create the fully fledged script with mail notification.
NB: Main difficulty was that we need to copy a folder that created each day

Step 1:
Create two variables. One for the source and second for the destination.

Ex:
:Variables
SET folderPath=\\182.10.8.56\Builds\BuildComplete <------------------------------------- Source
SET localfolderPath=\\152.30.0.10\Builds\AutoSOBuilds\BuildComplete <--------- Destination

Step 2:
Need to add the following syntax:

FOR /F "delims=|" %%I IN ('DIR "%folderPath%\*.*" /B /O:D') DO SET Newestfolder=%%I

FOR /F - Processing of a command consists of reading the output
'DIR "%folderPath%\*.*" /B /O:D' – This is to take the static folder path and take any folder/file beyond that.
DO SET Newestfolder=%%I – Locate the newest folder/file

Step 3:
Now we need to verify and notify our members that the build/file which we are going to download is already here (I mean we already have the latest build/file). So in this case we need a tool to send emails. So I have used the free version of ‘Smtp Mailer’ (http://www.smtpinfo.com/). This is a very nice tool. So see the syntax:

if exist %localfolderPath%\%Newestfolder%\Web_build (
start /d "D:\" SmtpMail.exe server=smtp.gmail.com from=[gmail address] to=[Send address] user=[gmail address] password=xxxxx ssl license=2013112908f4fc450745794d129cb94c359f0eba subject=You_already_have_the_latest_build body="Your build is already downloaded. Check %localfolderPath%."
exit
)

Step 4:
Let say we do not have the latest build/file. Now we need to add the following syntax:

if exist %folderPath%\%Newestfolder%\Web_build (
start /d "D:\" SmtpMail.exe server=smtp.gmail.com from=[gmail address] to=[Send address] user=[gmail addres] password=xxxxx ssl license=2013112908f4fc450745794d129cb94c359f0eba subject= build_download_is_started body="build download is started - (%Newestfolder%)."

And now we need to copy the build (s) from the newly created folder to our location (\\152.30.0.10\Builds\AutoSOBuilds\BuildComplete).

So we use ‘xcopy’ command:

xcopy "%folderPath%\%Newestfolder%\Web_build" "%localfolderPath%\%Newestfolder%\web_build" /D /E /C /R /I /K /Y
If you want, you can copy many files/folders. See following:
xcopy "%folderPath%\%Newestfolder%\Win_build" "%localfolderPath%\%Newestfolder%\win_build" /D /E /C /R /I /K /Y
exit )

Step 5:
Let say build is failed and no folders are created. Then we need to check that and send an email. See following:

if not exist %folderPath%\%Newestfolder%\Web_build (
start /d "D:\" SmtpMail.exe server=smtp.gmail.com from=[gmail address] to=[Send address] user=[gmail addres] password=xxxxx ssl license=2013112908f4fc450745794d129cb94c359f0eba subject=build_download_is_failed body="build download is failed."
exit
)
else(
exit
)

So all the things are finished. See the full script:

:Variables
SET folderPath=\\182.10.8.56\Builds\BuildComplete
SET localfolderPath=\\152.30.0.10\Builds\AutoSOBuilds\BuildComplete

FOR /F "delims=|" %%I IN ('DIR "%folderPath%\*.*" /B /O:D') DO SET Newestfolder=%%I
if exist %localfolderPath%\%Newestfolder%\[build name] (
start /d "D:\" SmtpMail.exe server=smtp.gmail.com from=[gmail address] to=[Send address] user=[gmail addres] password=xxxxx ssl license=2013112908f4fc450745794d129cb94c359f0eba subject=You_already_have_the_latest_build body="Your build is already downloaded. Check %localfolderPath%."
exit
)
if exist %folderPath%\%Newestfolder%\Web_build (
start /d "D:\" SmtpMail.exe server=smtp.gmail.com from=[gmail address] to=[Send address] user=[gmail addres] password=xxxxx ssl license=2013112908f4fc450745794d129cb94c359f0eba subject= build_download_is_started body="build download is started - (%Newestfolder%)."
xcopy "%folderPath%\%Newestfolder%\Web_build" "%localfolderPath%\%Newestfolder%\web_build" /D /E /C /R /I /K /Y
xcopy "%folderPath%\%Newestfolder%\Win_build" "%localfolderPath%\%Newestfolder%\win_build" /D /E /C /R /I /K /Y
exit
)
if not exist %folderPath%\%Newestfolder%\Web_build (
start /d "D:\" SmtpMail.exe server=smtp.gmail.com from=[gmail address] to=[Send address] user=[gmail addres] password=xxxxx ssl license=2013112908f4fc450745794d129cb94c359f0eba subject=
build_download_is_failed body="build download is failed."
exit
)
else(
exit
)
Hope this will help you to create a working script to download things from different location.