I am Computer Engineer by profession, passionate about technological advances in software technology, enthusiast learner. Also loves to read literature of historical & social importance.
::This program is used to demonstrate the for loop structure
@echo off
::For loop to iterate over list of values
echo print the list by for loop
for %%v in (11 22 33 44 55) do echo "v :: %%v"
::For loop to iterate through content of directory
echo print list of files in current directory
FOR %%v IN (.\*) DO ECHO %%v
echo To iterate over range from 10 to 100 with increment of 20 each iteration
::For loop to iterate over range
:: /L signifies to iterate over range
for /L %%v IN (10, 20, 100) Do echo "v :: %%v"
::This program is used to demonstrate the if else decision making structures
@echo off
set /A var_1=10
set /A var_2=20
::apply if else to check if two variables are same or different
if %var_1%==%var_2% (echo var_1 and var_2 are equal) else (echo var1 and var2 are not equal )
::apply if to check if variable is already defined
if DEFINED var_1 (echo var_1 is defined)
if NOT DEFINED var_2 (echo var_2 is undefined, hence defining again && set /A var2=21) else (echo var_2 is defined)
::apply if to check if file specified exists or not
if exist 05Data.txt (echo 05Data.txt file exists) else (echo 05Data.txt file does not exist)
pause
::this program will demonstrate assignment operators, following operators are supported.
::+= add & assign
::-= substract and assign
::*= multiply and assign
::/= divide and assign
::%= modulo divide and assign
@echo off
set /A var_1=3
set /A var_1+=2
echo "add and assign var_1 :: %var_1%"
set /A var_1-=3
echo "substract and assign var_1 :: %var_1%"
set /A var_1*=5
echo "multiply and assign var_1 :: %var_1%"
set /A var_1/=6
echo "divide and assign var_1 ::%var_1%"
set /A var_1%=2
echo "module divide and assign var_1 ::%var_1%"
Batch script supports logical operators for bitwise operations only. Unlike other rich programming languages, batch program does not support conditional logical operators.
06Operators_Bitwise.bat
::this program will demonstrate bitwise operators, following operators are supported
::& Bitwise AND
::| Bitwise OR
::^ Bitwise Exclusive OR
@echo off
set /A var_1=3
set /A var_2=5
set /A bitwiseAND="%var_1%&%var_2%"
set /A bitwiseOR="%var_1%|%var_2%"
set /A bitwiseEXOR="%var_1%^%var_2%"
echo "bitwiseAND :: %bitwiseAND%"
echo "bitwiseOR :: %bitwiseOR%"
echo "bitwiseEXOR :: %bitwiseEXOR%"
::this program will demonstrate redirectional operators, following are provided operators
::> To redirect command output to another file or stream. If file is already present it is overwritten
::> To redirect command output to another file in append mode
@echo off
::To print list of system users to file named 05Data.txt.
net users > 05Data.txt
::To append the ping output to same file 05Data.txt
ping yahoo.co.in >> 05Data.txt
::To Print the content of file
echo Content of file 05Data.txt
type 05Data.txt
::To provide input to sort command. Input is list of numbers which is passed from 05Num.txt
echo Content of file 05Num.txt
type 05Num.txt
echo Applying sort command on File data 05Num.txt
sort <05Num.txt
Output
05Data.txt
User accounts for \\LAPTOP-A4OSG6CL
-------------------------------------------------------------------------------
Administrator DefaultAccount defaultuser100001
Guest Owner WDAGUtilityAccount
The command completed successfully.
Pinging yahoo.co.in [74.6.136.150] with 32 bytes of data:
Reply from 74.6.136.150: bytes=32 time=292ms TTL=51
Reply from 74.6.136.150: bytes=32 time=212ms TTL=51
Reply from 74.6.136.150: bytes=32 time=681ms TTL=51
Reply from 74.6.136.150: bytes=32 time=400ms TTL=51
Ping statistics for 74.6.136.150:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 212ms, Maximum = 681ms, Average = 396ms
::this program will demonstrate relational operators. Following list of relational operators are supported
::EQU is for EQUAL TO
::NEQ is for Not Equal TO
::LSS is for Less Than
::LEQ is for Less Than Or Equal To
::GTR is for Greater Than
::GEQ is for Greater Or Than Equal To
@echo off
set /A var_1=10
set /A var_2=20
if %var_1% EQU %var_2% echo "var_1 and var_2 are equal"
if %var_1% NEQ %var_2% echo "var_1 and var_2 are not equal"
if %var_1% LSS %var_2% echo "var_1 is less than var_2"
if %var_1% LEQ 10 echo "var_1 is less than or equal to 10"
if %var_2% GTR %var_1% echo "var_2 is greater than var_1"
if %var_2% GEQ 20 echo "var_2 is greater than or equal to 20"
pause
::this program will demonstrate variables in batch file
@echo off
::set is used to declare and initialize variables
::by default variables are declared in global scope
set var1=I am global variable
::to print the value of variable surrount by %%
echo "var1 value:: %var1%"
::to declare variable in local scope
SETLOCAL
set var_2=I am the local variable, my life limited to SETLOCAL block
echo %var_2 value:: var_2%
ENDLOCAL
::ENDLOCAL is used to terminate the local block and scope, var_2 variable will be destroyed & dead after this.
echo "var_2 value after ENDLOCAL:: %var_2%"
::/A param used to assign numeric value
set /A data_num=100
echo "data_num value:: %data_num%"
import javax.xml.bind.annotation.XmlElement;
/**
*
*/
/**
* @author Owner
*
*/
public class Employee {
/**
*
*/
public Employee() {
// TODO Auto-generated constructor stub
}
String name, designation;
int employeeCode;
float salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public int getEmployeeCode() {
return employeeCode;
}
public void setEmployeeCode(int employeeCode) {
this.employeeCode = employeeCode;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public Employee(String name, String designation, int employeeCode, float salary){
this.name=name;
this.designation=designation;
this.employeeCode=employeeCode;
this.salary=salary;
}
@Override
public String toString() {
return "Employee [name=" + name + ", designation=" + designation + ", employeeCode=" + employeeCode
+ ", salary=" + salary + "]"+"\n";
}
}
Department.java
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Department {
public Department() {
// TODO Auto-generated constructor stub
super();
employeeList = new ArrayList();
}
String departmentName;
String departmentCode;
List employeeList;
@XmlAttribute
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
@XmlAttribute
public String getDepartmentCode() {
return departmentCode;
}
public void setDepartmentCode(String departmentCode) {
this.departmentCode = departmentCode;
}
@XmlElement(name="Employee")
public List getEmployeeList() {
return employeeList;
}
public void setEmployeeList(List employeeList) {
this.employeeList = employeeList;
}
public Department(String departmentCode, String departmentName, List employeeList){
this.departmentCode=departmentCode;
this.departmentName=departmentName;
this.employeeList=employeeList;
}
public void addEmployee(Employee employee){
this.employeeList.add(employee);
}
@Override
public String toString() {
return "Department [departmentName=" + departmentName + ", departmentCode=" + departmentCode + ", employeeList="
+ employeeList + "]";
}
}
TransformObjectToXML.java
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class TransformObjectToXML {
public TransformObjectToXML() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws JAXBException, FileNotFoundException {
// TODO Auto-generated method stub
//Creating object of Department
Department aerospace = new Department("222", "AEROSPACE", new ArrayList());
//add employeeObjects to departments
Employee david = new Employee("David Sherrif", "DBA", 11, 50000);
Employee Kevin = new Employee("Kevin Taylor","QA",22,30000);
Employee rudolf = new Employee("Rudolf Kane","Dev",33,70000);
Employee thomas = new Employee("Thomas Drake","MANAGER",55,90000);
//Adding employees into department
aerospace.addEmployee(david);
aerospace.addEmployee(Kevin);
aerospace.addEmployee(rudolf);
aerospace.addEmployee(thomas);
JAXBContext jaxbContext = JAXBContext.newInstance(Department.class);
Marshaller marshellObject = jaxbContext.createMarshaller();
marshellObject.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshellObject.marshal(aerospace, new FileOutputStream("DepartmentFile.xml"));
}
}