.bat and Windows command line

BAT

Introduction

The content of batch files (.bat extention files) is in reality the stack of command lines which have been typed and executed in a Windows console.

Windows commands are note case sensitive.

 

Most common commands

In order to disable the display of all commands which are executed in our .bat file, we must put at the beginning of that .bat file :

@echo off

Note: if we don’t type an at sign before “echo off”, the following lines will not be displayed but “echo off” will.

 

How to comment a line with REM

Command which will not be commented
REM Command which will be commented

 

Specific commands

Put the result of a command in a variable (if more than one line are returned, the variable will be filled only with the content of the last line):

for /f "delims=" %%i in ('commande') do Set VAR=%%i

Example with a test.bat file which contains:

@echo off

ping 14octobre.org

REM Retrieve the line which has the stats of a ping
REM #######################################################
echo RESUME STATS :
for /f "delims=" %%i in ('ping 14octobre.org ^| findstr /i /c:"Packets"') do echo %%i

REM Retrieve the average round trip time in milli-seconds
REM #######################################################
echo AVERAGE OF ROUNDTRIPS:
for /f "tokens=9 delims= " %%d in ('ping 14octobre.org ^| findstr /i /c:"Minimum ="') do echo %%d

bat-ping

Note: Be careful when you do a search on a commandline result, it depends of the language of the Operating System, all result is located. Accentuation is also a problem.

 

Resources

Website with a lot of information about the use and the possibilities offered by commandline when used in batch: http://windows.developpez.com/cours/ligne-commande (FR)

 

Leave a Reply

Your email address will not be published. Required fields are marked *