Code Scraps: Script zipping, transfer and unzipping in a Windows Batch Script

This code scrap illustrates how you can have a Batch File create a deployment zip, transfer the zip to a target server, and then unzip at the other end. Zipping before transferring any files is generally quicker than copying individual files across the network.

@echo off
REM “deploy” directory contains the deployment files
cd deploy
REM compress all the files in the directory into a “deploy.zip” in the parent directory
REM This assumes 7Zip is on the system PATH variable
for /f “usebackq tokens=*” %%a in (dir /b) do (
7z a -r “..\deploy.zip” “%%~nxa”
)
REM return to the parent directory
cd ..
REM Connect to the target server
net use n: \\my-other-server\ApplicationDeployments
REM Copy over the deployment zip to the target server
copy deploy.zip n:\TestDeploymentZip.zip
REM Extract the files on the Target server to ‘TestDeploymentZipOut’
7z x n:\TestDeploymentZip.zip -on:\TestDeploymentZipOut
REM Remove the Drive mapping to the target server
net use n: /delete

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.