Skip to content

Instantly share code, notes, and snippets.

@ejhari
Created September 16, 2016 01:02
Show Gist options
  • Save ejhari/5d3e678b0d5a6c8ace31c2e0dc39beab to your computer and use it in GitHub Desktop.
Save ejhari/5d3e678b0d5a6c8ace31c2e0dc39beab to your computer and use it in GitHub Desktop.
Batch Script
:: Name: batch_script.cmd
:: Purpose: A helper tool.
::
:: References:
:: http://steve-jansen.github.io/guides/windows-batch-scripting/
:: http://www.vectorsite.net/tsbatch.html
:: http://ss64.com/nt/
:: To turn off printing (ECHO’ing) of each batch file line.
:: The @ is a special operator to suppress printing of the command line.
@ECHO OFF
:: By default, variables are global to your entire command prompt session.
:: Call the SETLOCAL command to make variables local to the scope of your script.
:: After calling SETLOCAL, any variable assignments revert upon calling ENDLOCAL,
:: calling EXIT, or when execution reaches the end of file (EOF) in your script.
:: The ENABLEEXTENSIONS argument turns on a very helpful feature called
:: command processor extensions.
:: The ENABLEDELAYEDEXPANSION instructs cmd to recognise the syntax !var! which
:: allows to access the current value of var.
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
:: ~ removes quotes from the argument, useful esecially for paths.
:: Store batch script name.
SET script=%~n0
:: Store batch script parent directory.
SET scriptdir=%~dp0
ECHO.
ECHO All rights reserved.
ECHO.
IF [%1] == [] CALL :usage
ECHO Setting up build env.
IF EXIST "build" (
RMDIR /S /Q build
CALL :check_exit_code %ERRORLEVEL% "Failed to remove build directory."
)
MKDIR build
CALL :check_exit_code %ERRORLEVEL% "Failed to create build directory."
IF EXIST "dist" (
RMDIR /S /Q dist
CALL :check_exit_code %ERRORLEVEL% "Failed to remove dist directory."
)
MKDIR dist
CALL :check_exit_code %ERRORLEVEL% "Failed to create dist directory."
ECHO Creating dist.
XCOPY %scriptdir%build\* %scriptdir%dist\
ECHO.
ECHO Dist created in '%scriptdir%dist\'.
ECHO.
ECHO Done.
EXIT
:: Functions.
:: Function to check exit code.
:check_exit_code
IF %1 NEQ 0 (
ECHO %~2 Exiting with code: %1
EXIT
)
:: This acts as the "return" as in other languages. WTF? Aargh!
GOTO :EOF
:: Funciton to print usage.
:usage
ECHO Usage:
ECHO %~n0%~x0 arg
ECHO.
ECHO *arg - Help text for the arg.
ECHO.
EXIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment