Introduction
Often when we
update our software, installation setup program will handle everything for
us. We don’t need to concern about where the software is located, what are the
latest program to deploy, what settings to transfer over. The installer
will settle everything.
But when we are without
the installer, we can use the Windows Batch Script to do the job for us
automatically.
Compile source code using
Visual Studio or other compilers, often the output are the execution files
without the installer. Therefore, in some situations we are directly
deploying the execution files after Visual Studio compilation. To automate
the deployment process, we can use Windows Batch Script to automate the new
program deployment over the old program.
Batch Script
Work Flow
1.
First we will copy the batch
script file (‘.bat’) and the new folder files we intend to deploy in the
same location where existing program folder located. Do take note that
batch script will not able to run in system folder, including ‘System32’,
‘My Documents’ folder.
2.
Write the current program folder
name into batch script file: SET
CURRENT_PROGRAM=CurentProgram
3.
Current program folder location:
SET CURRENT_PROGRAM_DIR=%cd%/%CURRENT_PROGRAM%
4.
New program folder that going to
deploy: SET
NEW_PROGRAM_DIR=%cd%\NewProgram
5.
Check if new program folder
exists? If new program folder not found, print error message ‘New program
folder not found’ and exit the BAT script.
6.
Check if existing program folder
found in current directory? If existing program folder not found, print
error message ‘Existing program folder not found’ and exit the BAT script.
7.
Check if configuration files
that need to be transferred to new folder occurred in the existing program
folder? If configuration files not found, print error message
‘Configuration files are not found in the existing program folder’ and exit
the BAT script.
8.
If CURRENT_PROGRAM folder in
current directory/drive already being replaced by today, exit BAT script
now.
9.
If the old CurentProgram.exe
program is still running, any folder/files moving/renaming will not be
allowed by Windows OS. Exit BAT script.
10.
Backup the existing program
folder by rename the existing program folder by appending today date
a.
Retrieve today date.
:: Today datestamp
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%"
& set "YYYY=%dt:~0,4%" & set
"MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
& set "Min=%dt:~10,2%" & set
"Sec=%dt:~12,2%"
set "datestamp=%YY%%MM%%DD%"
|
b.
Rename the existing program
folder by appending “_replaced_” and today date stamp.
SET OLD_PROGRAM=%cd%\%CURRENT_PROGRAM%_replaced_%datestamp%
|
11.
Rename the existing program
folder.
12.
Rename the new deployment
folder.
13.
Copy the necessary configuration
files from old folder to newly deployed folder.
a.
If configuration files with the
same name already exist in the newly deployed folder. Backup it by rename
the file by appending “_replaced_” and today date stamp.
14.
After deployment completed, auto
launch the new Program.
15.
End
Source code
Batch Script: Auto_Deploy.bat.txt
|