How to write simple batch files that meet your purposes ?

H

Knowing how to write batch files is definitely an added advantage for any person who is using computer , There may be number of situations that you come across day to day life where you have to do laborious work for getting things done , In some cases you can avoid your manual work by writing simple batch files.

If you want to write a batch file , it is very simple , Open Notepad and just type the commands that you know in dos in new lines and save it as .bat extension file , that is it . The commands that you have typed will be executed in order of their appearance in the batch file.

To Write Comments in Batch Files use either of the following syntaxes

REM Comment Line 1
REM Comment Line 2

(or)

:: Comment Line 1
:: Comment Line 2

Here are some example batch files for your understanding

Example 1 : You have to copy and paste the content of 50 text files into a single file then you probably may open each and individual file and copy them and paste them in the single file.

Batch Solution :

type *.txt >> final.txt

You don’t need to write a batch file for this sort of single command but one single command may some times need some situation to execute.

Example 2 : You want to shut down your PC when time is 14:30 , Then you probably may put alarm in your phone at 14:30 and shut down when your alarm beeps or you may feel like let your computer run as it is.

Batch Solution :

@echo off
echo current time is %time:~0,5%
set /p user_time="Enter Time as HH:MM in 24 hour format "
echo You have entered %user_time% to shut down your pc
:loop
set current_time=%time:~0,5%
IF %current_time%==%user_time% goto endloop
timeout 50>%temp%OFF.txt
goto loop
:endloop
shutdown /s
echo System will turn off in less than a minute

This may be a single command shutdown /s , but it is waiting for time to become that user have entered. These are just simple applications , there are many more things that you can do with batch files.

Example 3 : Suppose you have one weirdest requirement as changing the IP of your PC continuously in a particular range and check whether your PC have internet connectivity or not , You probably might go to ipv4 properties in Local Area connection settings and change manually it to your required address and check whether your pc is connected to internet.

Batch Solution :

@ ECHO OFF
timeout 2 > %temp%ipchanger.txt
ECHO Welcome to IP changer :)
timeout 2 > %temp%/ipchanger.txt
ECHO Range of IP Address is A.B.C.D to A.B.E.F
timeout 2 > %temp%ipchanger.txt
set /p var_1="Enter A value "
timeout 1 > %temp%ipchanger.txt
set /p var_2="Enter B value "
timeout 1 > %temp%ipchanger.txt
set /p var_3="Enter C value "
timeout 1 > %temp%ipchanger.txt
set /p var_4="Enter D value "
timeout 2 > %temp%ipchanger.txt
set /p var_5="Enter E value "
timeout 1 > %temp%ipchanger.txt
set /p var_6="Enter F value "
FOR /l %%A IN (%var_3%,1,%var_5%) DO FOR /l %%B IN (%var_4%,1,%var_6%) DO netsh interface ip set address name="Local Area Connection" static 172.16.%%A.%%B 255.255.0.0 172.16.20.1 && ping 172.16.20.1 −n 9 >>C:results.txt & echo %var_1%.%var_2%.%%A.%%B >>C:results.txt & echo pinging %var_1%.%var_2%.%%A.%%B

You might get a software to turn of your PC at user mentioned time but you will never get a software that meets your weirdest requirement but batch files can be helpful in solving your requirement. This post is just to tell you that you can do things simple by using batch files.

You can learn batch scripting from this website Robvanderwoude  if you wish to.

About the author

pavankumar.p1990
By pavankumar.p1990