Jumat, 06 Maret 2020

Echo certain line in a text file using batch programming language

Hello Batch Programmer, Welcome.

This blog is how to echo certain line of a text file and you can also edit the code as you like.

You know, you can print all lines of a text file using FOR /F variant loop.

You can also print certain line of a text file using FOR /F loop variant but with few workaround; We will use IF to block or to not display echo and use setlocal EnableDelayedExpansion to get the number of iteration.

So in this tutorial we will echo or print out certain specific line out of any text file.


PRE-REQUISITE:
You need to know how to use setlocal EnableDelayedExpansion and variable: !somevariable!

You can read the setlocal EnableDelayedExpansion tutorial here: Link to EnableDelayedExpansion

Lets get into the code:

@echo off 
REM This a program to read a specific line of a text file 
REM Created by Totzfreelance 
REM %1 is the filename that you want to read 
REM %2 is for the line number that you want to read from the file a.k.a from the program argument number 1. 
setlocal EnableDelayedExpansion 
SET /a a = 0 
FOR /F "tokens=* delims=" %%i IN ('type %1') DO set /a a = a + 1&IF !a!==%2 echo %%i

Tidak ada komentar:

Posting Komentar