Many people think batch files are mostly things of the past.But it is not only past, Some days ago I worked on release for WSO2 Branch 3.2.3. I found scripting and batch files are every important on that task.
Batch file is just the thing to automate the job you want to do? not only that it is much more.
What is batch file?
Batch file is script file, a text file containing a series of commands to be executed by the command interpreter in DOS, OS/2, and Microsoft Windows.
HelloWorld
@ECHO off
ECHO Hello World!
PAUSE
save file as ‘HelloWorld.bat’
Double click the file to run the batch file
In batch file we can get environment variable values
eg:
ECHO java Home is %JAVA_HOME%
ECHO user profile is at %userprofile%
we can have windows command inside batch file such open notepad.exe
or ever web url
START http://google.com
START /max notepad.exe
NOTE
In “START /max notepad.exe” /max make open notepad in maximize view mode.
We can have some condition in batch file, and user can give choice for to process
Here is simple example code for it.
@echo off
color 0A
title Conditional Batch File
::comments for batch files
:start
echo What would you like to do?
echo.
echo 1. Ice Cream
echo 2. Apple
echo.
echo 0. Quit
echo.
set /p choice="Enter your choice: "
if %choice%==1 goto eatIceCream
if %choice%==2 goto eatApple
if %choice%==0 exit
echo Invalid choice: %choice%
echo.
pause
cls
goto start
:eatIceCream
cls
set /p type="What type of ice Cream You need?"
echo Ice Cream %type% given.
echo.
goto finalAsk
:eatApple
cls
set /p count="How many Apple you need?"
echo %count% apples are given.
echo.
goto finalAsk
:finalAsk
set /p cancel="Type cancel to stop Process, start to Start again "
if %cancel%==cancel cls
if %cancel%==start cls
if %cancel%==start goto start
if %cancel%==cancel echo Process is cancelled.
if %cancel%==cancel echo.
if %cancel%==cancel pause
if %cancel%==cancel exit
Add a comment