About Batch Files
According to Wikipedia, A Batch file is script file in DOS, OS/2 and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file
Up to me, following description is more explanatory. A .bat file is a DOS/Windows shell script executed by the DOS/Windows command interpreter. When a batch script is saved to a .bat file, it is just called a batch file.
a Batch file is stored as a file with a .bat file extension in Windows. For instance : I created a simply batch file as naming greetings.bat in Windows OS
Additionally batch file language is batch script. Batch Scripting consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file.
There are two data types in Batch. Integers and Strings by the way batch does not support floating-point values.
Well I think we should look up to batch commands now. Basically there are essential eight command in batch
HELP : This command tells us all about the commands available in the cmd. In the following image shows example help command output.
DIR : The ‘dir’ command is used to get all the directories, sub-directories, and files present in the current working directory.
CD: It is used for changing working directory
VER: This command will give our windows version
CLS: Clear the screen of the terminal
ECHO: Actually I love this command. It remembers my old days to me. Anyway this batch command displays messages, or turns command echoing on or off.
@: The ‘@’ if used before any command hides which command is running.
@ECHO OFF: This commands serves as the start point to any basic batch script as it hides the prompt with ‘echo off’ and hides ‘echo off’ command with ‘@’.
Now Let’s do some practice. I had wroten a greetings.bat in essay’s start. Now I am changing to this file’s name as OurProblem.bat and write to following problems solves sequentially.
My Problem: I want to find perimeter of the a basketball court. I have edge values which I wanna enter to this program and I want to show program’s perimeter of the a basketball court on the screen.
My Solve:
@ECHO OFF
SET /p firstEdge=Please enter the first edge:
SET /p secondEdge=Please enter the second edge:
SET /p thirdEdge=Please enter the third edge:SET /A perimeterBasketballCourt= firstEdge + secondEdge + thirdEdgeECHO Perimeter of the a Basketball Court:PAUSE
Now I used new expression for this solving. Firstly in my opinion, you must look up to these commands and understand
SET /p = This command is used for take input from user
ECHO: = for new line
PAUSE = I used this command for program doesn't ending immediately
SET /A = SET uses for variable initialize. for /A parameter, this switch is used if the value needs to be numeric in nature.
Okey now we should increare problem’s difficulty.
My Problem 2: I wanna find the sum of all cards on a playing cards. By the way playing cards contain 4 group (clubs (♣), diamonds (♦), hearts (♥), and spades (♠)) and each group contains from 2 to 10 and additionally it contains joker, queen, king, and as. I guess most logical approach is joker=11, queen=12, king=13 and as=14
My Solve 2:
@ECHO OFF
setlocal enabledelayedexpansion
SET sum=0for /l %%y in (1, 1, 4) do (
for /l %%x in (2, 1, 14) do (
SET /a sum=sum+%%x
ECHO + %%x = !sum!
)
)ECHO:
ECHO Result=%sum%PAUSE
So we learnt for loop structure, variable assign, printing to screen and basic operations in batch script. Now we should solve more difficult problem. It will be related computer
My Problem 3: How do I close a running program with program’s PID? I wanna enter program’s PID and wanna program’s close it.
My Solve 3:
@ECHO OFF:startPoint
SET /P pid=Please enter the pid of the running program you want to close:TASKKILL /F /PID %pid%SET /P userChoose=If do you want to close another program, you ought to enter the "yes":
IF "%userChoose%"=="yes" (
goto :startPoint
)PAUSE
Now we should try to understand to this code pieces. I already explained many pieces so I passed what i explained.
Now firstly we gonna focus to :start and goto :startPoint. Well this code pieces the same with C goto structure.If you don’t know to C programming language, I could say this code pieces use for jump.
Secondly TASKKILL /F /PID structure is used kill process. We give process id and it kill to process.
Thirdly IF statement, I guess you know that but if we need to explain a little, this code structure is used for condition al structure.
My Problem 4: I generally develop my hobby projects without IDE. This is hard stuff but though IDE is very usefull for development, I believe that It blunts the developer because It does many complex operation and we only responsible with application development.
Anyway I talked a lot. I wanna a application which it creates folder and java class. Application ought to take application name from the console screen then it ought to create a folder and a java file inside this folder as user gave name. Lastly our batch file gotta run our java for test.
My Solve 5:
@ECHO OFF
SET /P applicationName=Please Enter that Your Java Application Name:
MKDIR %applicationName% ECHO public class %applicationName%{ public static void main(String... args) { System.out.println("Hello World"); } } >> "%applicationName%/%applicationName%.java" JAVAC "%applicationName%/%applicationName%.java"
CD "./%applicationName%"
JAVA "%applicationName%"PAUSE
We go to step by step. We talked about following commands so i am passing to other commands.
@ECHO OFF
SET
ECHO
PAUSE
MKDIR command is used for create new folder.
Javac Command is used for compile to your java code.
CD is used for changing working directory.
Lastly Java Command is used for running your compiled java codes.
In conclusion,we talked about batch files, batch script, data types on the batch script, a lot of commands and solving varios problem. I hope that this essay will be useful for you. Ah don’t forget you gotta solve various problem for remember. I hope that’ll be fun. See you next essay…
I used various resource for prepare to this essay. I put the some. You could check this links.
Link 1 : https://www.tutorialspoint.com/batch_script/batch_script_files.htm