Adding a increasing value - batch-file

I know this one is an easy one, but it's driving me nuts
Need help with setting up a counter to increase by 1
I have a txt file with a 100x timestamp and when I enter 1000 all I get is 1001
and I'm trying to get 1001,1002,1003
Here is the script I am using
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
#For %%G In ("%~dp0New Folder1") Do Set "sourcedir=%%~fG"
#For %%G In ("%~dp0New Folder2") Do Set "destdir=%%~fG"
Set /p Stamp=Enter TimeStamp:
SET /a TimeCount=%Stamp%
SET /a TimeCount+=1
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\*.txt"') DO (
(
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:timestamp=!" (ECHO %%c) ELSE (
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: %TimeCount%,
)
) ELSE ECHO(
)
)>"%destdir%\%%q"
Thanks guys

Thank you Magoo for pointing out !Timecount!
Here is the correct way to set up the script to increase by 1
This script will add to your number if you enter 100 and you have 10 repeated codes within the same file you will get 101,102,103,...110, etc...
Now the adding will carry over to all other files so..
If you have 2 files in the same folder and each have 10 repeated codes each the script will count from 101 to 120
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
#For %%G In ("%~dp0New Folder1") Do Set "sourcedir=%%~fG"
#For %%G In ("%~dp0New Folder2") Do Set "destdir=%%~fG"
Set /p Stamp=Enter TimeStamp:
SET /a TimeCount=%Stamp%
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\*.txt"') DO (
(
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:timestamp=!" (ECHO %%c) ELSE (
SET /a TimeCount+=1
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: !TimeCount!,
)
) ELSE ECHO(
)
)>"%destdir%\%%q"
)
Now by moving this SET /a TimeCount=%Stamp% under the For Loop
you will 2 results each file will get 101 to 110
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
#For %%G In ("%~dp0New Folder1") Do Set "sourcedir=%%~fG"
#For %%G In ("%~dp0New Folder2") Do Set "destdir=%%~fG"
Set /p Stamp=Enter TimeStamp:
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\*.txt"') DO (
SET /a TimeCount=%Stamp%
(
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:timestamp=!" (ECHO %%c) ELSE (
SET /a TimeCount+=1
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: !TimeCount!,
)
) ELSE ECHO(
)
)>"%destdir%\%%q"
)

Related

CMD: extract variable string from .txt file

I have myfile.txt containing this:
line 1: keyword=string
line 2
line 3
I need to echo that variable string via constant keyword.
And this string I need is always in the end of the 1st line.
So far I have:
for /f "usebackq tokens=*" %%a in ("myfile.txt") do (
for /f "usebackq tokens=*" %%b in ('findstr /i "keyword=" %%a') do (
echo %%b & goto 108
)
)
:108
But I guess that findstr syntax is completely wrong.
Please, help.
You can do rhis very simply as:
Echo off
SETLOCAL ENABLEDELAYEDEXPANSION
For /F "tokens=*" %%_ IN ('Type "c:\my\path\myfile.txt" ^| FIND /I "keyword="
) DO (
SET "Tmp_Line=%%_"
For /F "Tokens=* delims==" %%A in ('ECHO=!Tmp_Line:*keyword=!') DO (
echo %%a
GOTO :108
Pause
)
GOTO :EOF
)
Goto :EOF

batch script adaption with following code

I have a Batch file with the following code:
#echo off
set "quelle=C:\Users\User-01\quelle_001.txt"
set "ziel=C:\Users\User-01\ziel_001.csv"
>"%ziel%" (for /f "usebackq tokens=1-9 delims=;" %%a in ("%quelle%") DO for /L %%z in (1 1 %%i) do echo %%a;%%b;%%c;%%d;%%e;%%f;%%g;%%h;%%i;)
Input:
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
Output:
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
Now I would like to have the following Output:
TEST0;TEST1;WERT1;;;;;;3;
TEST0;TEST1;WERT2;;;;;;3;
TEST0;TEST1;WERT3;;;;;;3;
because in %%i has the value 3.
If in %%i the value is 4 the output should be:
TEST0;TEST1;WERT1;;;;;;4;
TEST0;TEST1;WERT2;;;;;;4;
TEST0;TEST1;WERT3;;;;;;4;
TEST0;TEST1;WERT4;;;;;;4;
etc.
#echo off
SETLOCAL EnableDelayedExpansion
set "quelle=t.csv"
set "ziel=t1.csv"
>"%ziel%" (
for /f "usebackq tokens=1,2,* delims=;" %%a in ("%quelle%") DO (
set "i=0"
for %%k in (%%c) do set "nr=%%k"
for %%m in (%%c) do (
set /a i+=1
if !i! leq !nr! echo %%a;%%b;%%m;;;;;;!nr!;
)
)
)
type "%ziel%"
Input:
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
TEST0;TEST1;WERTa;WERTb;WERTc;WERTd;WERTe;WERTf;4;
Output:
TEST0;TEST1;WERT1;;;;;;3;
TEST0;TEST1;WERT2;;;;;;3;
TEST0;TEST1;WERT3;;;;;;3;
TEST0;TEST1;WERTa;;;;;;4;
TEST0;TEST1;WERTb;;;;;;4;
TEST0;TEST1;WERTc;;;;;;4;
TEST0;TEST1;WERTd;;;;;;4;
#echo off
set "quelle=C:\Users\User-01\quelle_001.txt"
set "ziel=C:\Users\User-01\ziel_001.csv"
>"%ziel%" (for /f "usebackq tokens=1-9 delims=;" %%a in ("%quelle%") DO (
echo %%a;%%b;%%c;;;;;;%%i;
echo %%a;%%b;%%d;;;;;;%%i;
echo %%a;%%b;%%e;;;;;;%%i;
echo %%a;%%b;%%f;;;;;;%%i;
echo %%a;%%b;%%g;;;;;;%%i;
echo %%a;%%b;%%h;;;;;;%%i;
)
)
I've removed the For /L loop as it's purpose isn't clear, and the %%z value does not get used. The only thing it appears to do is cause the same line to be echoed multiple times depending on the value of the %%i token retrieved in the first loop.
This seems as if it will do what your samples show, is it what you mean?
#Set "quelle=C:\Users\User-01\quelle_001.txt"
#Set "ziel=C:\Users\User-01\ziel_001.csv"
#(For /F "UseBackQ Tokens=1-3,9 Delims=;" %%G In ("%quelle%") Do #For /F "Delims=0123456789" %%K In ("%%I") Do #For /L %%L In (1 1 %%J) Do #Echo %%G;%%H;%%K%%L;;;;;;%%J;)>"%ziel%"

batch file: Parse string after substring occurence, not delimeter

I have and batch file script shows me output of adb utility.
FOR /F "skip=1 delims=~" %%x IN ('adb devices -l') DO echo %%x
2 output lines
0123456789ABCDEF device product:java_joyplus_qb7 model:TM702B4_3G device:java_joyplus_qb7 transport_id:12
F9NPFP084096 device product:WW_P023 model:P023 device:P023_1 transport_id:11
I want parse non-space substring after "transport_id:".
For the first line i want get 12, for second 11. How can I do that?
If I understand correctly what you've asked, the following batch-file should output the information you require.
#Echo Off
For /F "Skip=1 Tokens=1*" %%G In ('adb.exe devices -l') Do (Set "DI=%%H"
SetLocal EnableDelayedExpansion
For /F %%I In ("!DI:*transport_id:=!") Do EndLocal & Echo %%I)
Pause
And, if you wanted to do that from cmd:
For /F "Skip=1Tokens=1*" %G In ('adb.exe devices -l')Do #Set "DI=%H"&For /F %I In ('Cmd /D/V/C Echo "!DI:*transport_id:=!"')Do #Echo %~I
You may easily extract all variables no matter their positions and show what you want:
#echo off
setlocal EnableDelayedExpansion
FOR /F "skip=1 delims=" %%a IN ('adb devices -l') DO (
for %%b in (%%a) do for /F "tokens=1,2 delims=:" %%x in ("%%b") do set "%%x=%%y"
echo Product=!product!
echo Transport=!transport_id!
)
For example:
Product=java_joyplus_qb7
Transport=12
Product=WW_P023
Transport=11
SETLOCAL EnableDelayedExpansion
REM …
FOR /F "skip=1 delims=~" %%x IN ('adb devices -l') DO (
set "_ID="&set "_transport_id="
call :parse "" "%%~x"
echo transport_id !_transport_id! (!_ID!^)
)
REM … remaining part of your script here …
goto :eof
:parse
if "%~2"=="" goto :eof
for /F "tokens=1,*" %%G in ("%~2") do (
if "%~1"=="" (
set "_ID=%%~G"
call :parse "%%~G" "%%~H"
) else (
for /F "tokens=1,2 delims=: " %%g in ("%~2") do (
if /I "%%~g"=="transport_id" (
set "_transport_id=%%~h"
) else (
call :parse "%%~G" "%%~H"
)
)
)
)
goto :eof
Tested using the following script with hard-coded hypothetical adb output (not having adb installed):
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
(
REM last token
set "_ID="&set "_transport_id="
call :parse "" "0123456789ABCDEF device product:java_joyplus_qb7 model:TM702B4_3G device:java_joyplus_qb7 transport_id:12"
echo transport_id !_transport_id! (!_ID!^)
REM penult token
set "_ID="&set "_transport_id="
call :parse "" "F9NPFP084096 device product:WW_P023 model:P023 transport_id:11 device:P023_1"
echo transport_id !_transport_id! (!_ID!^)
REM elsewhere token
set "_ID="&set "_transport_id="
call :parse "" "abc123def567 transport_id:10 device product:XX_P023 model:P023 device:P023_2"
echo transport_id !_transport_id! (!_ID!^)
REM nowhere token
set "_ID="&set "_transport_id="
call :parse "" "noTransportId sport_id:10 device product:XX_P023 model:P023 device:P023_2"
echo transport_id !_transport_id! (!_ID!^)
)
goto :eof
:parse
if "%~2"=="" goto :eof
for /F "tokens=1,*" %%G in ("%~2") do (
if "%~1"=="" (
set "_ID=%%~G"
call :parse "%%~G" "%%~H"
) else (
for /F "tokens=1,2 delims=: " %%g in ("%~2") do (
if /I "%%~g"=="transport_id" (
set "_transport_id=%%~h"
) else (
call :parse "%%~G" "%%~H"
)
)
)
)
goto :eof
Output: D:\bat\SO\61588773.bat
transport_id 12 (0123456789ABCDEF)
transport_id 11 (F9NPFP084096)
transport_id 10 (abc123def567)
transport_id (noTransportId)

How to use parameter 2 in a for loop nested in Batch file?

I could not use the second parameter(%%b) in the first for loop to compare (if !count! GTR %%b) in the for second loop, hoping the experts to help, sorry about my English is not good.
File text1.txt
# #
#Employee*.PDF,3
School*.PDF,4
Family*.PDF,5
Folder c:\user\text
xxxxxxxxx.pdf
Employee1.pdf
Employee3.pdf
Employee2.pdf
Employee4.pdf
Employee5.pdf
Employee6.pdf
Employee7.pdf
School1.pdf
School3.pdf
School2.pdf
School4.pdf
School5.pdf
School6.pdf
School7.pdf
Total Code:
#Echo off
#SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=, tokens=1,2 eol=#" %%a in ('Type text1.txt') do (
set /a count=0
for /f %%x in ('dir C:\user\text\%%a /b') do (
set /a count+=1
if !count! GTR %%b (
del "C:\user\text\%%x"
)
)
)
I cannot use %%b in the second for loop.
output after run All files are deleted.
but i just want to delete :
Employee4.pdf
Employee5.pdf
Employee6.pdf
Employee7.pdf
School5.pdf
School6.pdf
School7.pdf
Help me, please!
I tried... add set /a flag=%%b and change Comparative conditions = if !count! GTR !flag!
for /f "delims=, tokens=1,2 eol=#" %%a in ('Type text1.txt') do (
set /a count=0
set /a flag=%%b
for /f %%x in ('dir C:\user\text\%%a /b') do (
set /a count+=1
if !count! GTR !flag! (
del "C:\user\text\%%x"
)
)
)
But output !flag! = 5,
equal to its final value Family*.PDF,5.
Your first code seems to work fine (I added some code to add demo files and a minor change in the dir command to match environment - adapt to your needs):
#echo off
setlocal enabledelayedexpansion
REM create test environment:
for /l %%a in (1,1,7) do break>School%%a.pdf
for /l %%a in (1,1,7) do break>Family%%a.pdf
dir /b *.pdf
echo ---------
type text1.txt
echo ---------
pause
for /f "delims=, tokens=1,2 eol=#" %%a in ('Type text1.txt') do (
set /a count=0
for /f %%x in ('dir %~dp0%%a /b') do (
set /a count+=1
if !count! GEQ %%b echo "%%~fx"
)
)
Output:
Family1.pdf
Family2.pdf
Family3.pdf
Family4.pdf
Family5.pdf
Family6.pdf
Family7.pdf
School1.pdf
School2.pdf
School3.pdf
School4.pdf
School5.pdf
School6.pdf
School7.pdf
---------
# #
#Employee*.PDF,3
School*.PDF,4
Family*.PDF,5
---------
Drücken Sie eine beliebige Taste . . .
"D:\tmp\test\School4.pdf"
"D:\tmp\test\School5.pdf"
"D:\tmp\test\School6.pdf"
"D:\tmp\test\School7.pdf"
"D:\tmp\test\Family5.pdf"
"D:\tmp\test\Family6.pdf"
"D:\tmp\test\Family7.pdf"
Oh yes - and I changed GTR to GEQ to match your example.
The dir command needs a wildcard following %%a
for /f %%x in ('dir /b "C:\user\text\%%a*"') do (
Also %%b read from text1.txt isn't just the number, but has the extension .pdf attached.
Either use
if !count! GTR %%~nb to strip off the extension, or
modify test1.txt to have another comma between the number and the extension.

Batch Script To Extract Lines Between Specified Words

I have a log file like below.
[Tue Aug 19 10:45:28 2014]Local/PLPLAN/PL/giuraja#MSAD/2172/Info(1019025)
Reading Rules From Rule Object For Database [PL]
[Tue Aug 19 10:45:28 2014]Local/PLPLAN/PL/giuraja#MSAD/2172/Info(1013157)
Received Command [Import] from user [giuraja#MSAD] using [AIF0142.rul] with data file [SQL]
.
.
.
.
.
Clear Active on User [giuraja#MSAD] Instance [1]
.
.
I want to extract the line starting with "[Tue Aug 19 10:" until the line that starts with "Clear Active on User" and output to a file using windows batch script. I tried the below code. It only outputs the last line.
#echo off & setlocal enabledelayedexpansion
set Month_Num=%date:~4,2%
if %Month_Num%==08 set Month_Name=Aug
set Day=%date:~0,3%
set Today_Date=%date:~7,2%
set Search_String=[%Day% %Month_Name% %Today_Date% 10:
for /f "tokens=1 delims=[]" %%a in ('find /n "%Search_String%"^
#(
more +%%a D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN.LOG)>D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN_Temp.txt
(for /f "tokens=*" %%a in (D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN_Temp.txt) do (
set test=%%a
if "!test:~0,20!" equ "Clear Active on User" goto :eof
echo %%a
))>D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN_Formatted.txt
Regards,
Ragav.
The Batch file below was designed to process as fast as possible a big file; however, it deletes empty lines from result:
#echo off
setlocal EnableDelayedExpansion
set "start=[Tue Aug 19 10:"
set "end=Clear Active on User"
for /F %%a in ("%start%") do set startWord=%%a
for /F %%a in ("%end%") do set endWord=%%a
set "startLine="
set "endLine="
for /F "tokens=1,2 delims=: " %%a in ('findstr /I /N /B /C:"%start%" /C:"%end%" logFile.txt') do (
if not defined startLine if "%%b" equ "%startWord%" set startLine=%%a
if not defined endLine if "%%b" equ "%endWord%" set "endLine=%%a" & goto continue0
)
:continue0
set /A skipLines=startLine-1, numLines=endLine-startLine+1
set "skip="
if %skipLines% gtr 0 set skip=skip=%skipLines%
(for /F "%skip% delims=" %%a in (logFile.txt) do (
echo %%a
set /A numLines-=1
if !numLines! equ 0 goto continue1
)) > outFile1.txt
:continue1
rem Previous outFile1.txt contain as many extra lines as empty lines removed, so we need to eliminate they
for /F "delims=:" %%a in ('findstr /I /N /B /C:"%end%" outFile1.txt') do set numLines=%%a
(for /F "delims=" %%a in (outFile1.txt) do (
echo %%a
set /A numLines-=1
if !numLines! equ 0 goto continue2
)) > outFile2.txt
:continue2
del outFile1.txt
TYPE outFile2.txt
If you want to preserve empty lines, the process would be much slower.
This should work (tested)
#echo off
set "st_line=Tue Aug 19"
set "end_line=Clear Active on User"
for /f "delims=:" %%i in ('findstr /inc:"%st_line%" logfile.txt') do (set st_line_ln=%%i)
for /f "delims=:" %%j in ('findstr /inc:"%end_line%" logfile.txt') do (set end_line_ln=%%j)
findstr /in /c:[a-z] /c:[0-9] /rc:"^$" logfile.txt >logfile_ln.txt
set /a "st_line_ln_temp=%st_line_ln-1"
for /f "skip=%st_line_ln_temp% tokens=1* delims=:" %%a in ('type logfile_ln.txt') do (
if %%a leq %end_line_ln% (echo.%%b)
)
del logfile_ln.txt
Sample output -
C:\test>type logfile.txt
Tue Aug 19 10:45:28 2014]Local/PLPLAN/PL/giuraja#MSAD/2172/Info(1019025)
Reading Rules From Rule Object For Database [PL]
[Tue Aug 19 10:45:28 2014]Local/PLPLAN/PL/giuraja#MSAD/2172/Info(1013157)
Received Command [Import] from user [giuraja#MSAD] using [AIF0142.rul] with data file [SQL]
test1
test2
test4
test546
Clear Active on User [giuraja#MSAD] Instance [1
test1212
test232
test67
dj
C:\test>draft.bat
[Tue Aug 19 10:45:28 2014]Local/PLPLAN/PL/giuraja#MSAD/2172/Info(1013157)
Received Command [Import] from user [giuraja#MSAD] using [AIF0142.rul] with data file [SQL]
test1
test2
test4
test546
Clear Active on User [giuraja#MSAD] Instance [1
C:\test>
Cheers, G
#ECHO OFF
SETLOCAL
:: If you don't want to preserve empty lines
SET "select="
(
FOR /f "delims=" %%a IN (q25390541.txt) DO (
ECHO %%a|FINDSTR /b /L /c:"[Tue Aug 19 10:" >NUL
IF NOT ERRORLEVEL 1 SET select=y
IF DEFINED select ECHO(%%a
ECHO %%a|FINDSTR /b /L /c:"Clear Active on User" >NUL
IF NOT ERRORLEVEL 1 GOTO done1
)
)>newfile.txt
:done1
:: If you want to preserve empty lines
SET "select="
(
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r ".*" q25390541.txt') DO (
IF "%%b"=="" (
IF DEFINED select ECHO(
) ELSE (
ECHO %%b|FINDSTR /b /L /c:"[Tue Aug 19 10:" >NUL
IF NOT ERRORLEVEL 1 SET select=Y
IF DEFINED select ECHO(%%b
ECHO %%b|FINDSTR /b /L /c:"Clear Active on User" >NUL
IF NOT ERRORLEVEL 1 GOTO done2
)
)
)>newfile2.txt
:done2
GOTO :EOF
I used a file named q25390541.txt containing your data for my testing.
Produces newfile.txt and newfile2.txt depending on which is preferred.
This is an inclusive (start and end line are shown) script that should do the job. You can modify it to exclude the lines:
#echo off
setlocal
set filename=text_file.txt
for /f "tokens=1 delims=:" %%a in ('findstr /n /b /c:"[Tue Aug " %filename%') do (
set /a f_line=%%a-1
)
for /f "tokens=1 delims=:" %%a in ('findstr /n /b /c:"Clear Active on User" %filename%') do (
set /a l_line=%%a
)
echo %l_line% -- %f_line%
call :tail_head2 -file=%filename% -begin=%f_line% -end=%l_line%
exit /b 0
#echo off
:tail_head2
setlocal
rem ---------------------------
rem ------ arg parsing --------
rem ---------------------------
if "%~1" equ "" goto :help
for %%H in (/h -h /help -help) do (
if /I "%~1" equ "%%H" goto :help
)
setlocal enableDelayedExpansion
set "prev="
for %%A in (%*) do (
if /I "!prev!" equ "-file" set file=%%~fsA
if /I "!prev!" equ "-begin" set begin=%%~A
if /I "!prev!" equ "-end" set end=%%A
set prev=%%~A
)
endlocal & (
if "%file%" neq "" (set file=%file%)
if "%begin%" neq "" (set /a begin=%begin%)
if "%end%" neq "" (set /a end=%end%)
)
rem -----------------------------
rem --- invalid cases check -----
rem -----------------------------
if "%file%" EQU "" echo file not defined && exit /b 1
if not exist "%file%" echo file not exists && exit /b 2
if not defined begin if not defined end echo neither BEGIN line nor END line are defined && exit /b 3
rem --------------------------
rem -- function selection ----
rem --------------------------
if defined begin if %begin%0 LSS 0 for /F %%C in ('find /c /v "" ^<"%file%"') do set /a lines_count=%%C
if defined end if %end%0 LSS 0 if not defined lines_count for /F %%C in ('find /c /v "" ^<"%file%"') do set lines_count=%%C
rem -- begin only
if not defined begin if defined end if %end%0 GEQ 0 goto :end_only
if not defined begin if defined end if %end%0 LSS 0 (
set /a end=%lines_count%%end%+1
goto :end_only
)
rem -- end only
if not defined end if defined begin if %begin%0 GEQ 0 goto :begin_only
if not defined end if defined begin if %begin%0 LSS 0 (
set /a begin=%lines_count%%begin%+1
goto :begin_only
)
rem -- begin and end
if %begin%0 LSS 0 if %end%0 LSS 0 (
set /a begin=%lines_count%%begin%+1
set /a end=%lines_count%%end%+1
goto :begin_end
)
if %begin%0 LSS 0 if %end%0 GEQ 0 (
set /a begin=%lines_count%%begin%+1
goto :begin_end
)
if %begin%0 GEQ 0 if %end%0 LSS 0 (
set /a end=%lines_count%%end%+1
goto :begin_end
)
if %begin%0 GEQ 0 if %end%0 GEQ 0 (
goto :begin_end
)
goto :eof
rem -------------------------
rem ------ functions --------
rem -------------------------
rem ----- single cases -----
:begin_only
setlocal DisableDelayedExpansion
for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
set "line=%%L"
for /F "delims=:" %%n in ("%%L") do (
if %%n GEQ %begin% (
setlocal EnableDelayedExpansion
set "text=!line:*:=!"
(echo(!text!)
endlocal
)
)
)
endlocal
endlocal
goto :eof
:end_only
setlocal disableDelayedExpansion
for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
set "line=%%L"
for /F "delims=:" %%n in ("%%L") do (
IF %%n LEQ %end% (
setlocal EnableDelayedExpansion
set "text=!line:*:=!"
(echo(!text!)
endlocal
) ELSE goto :break_eo
)
)
:break_eo
endlocal
endlocal
goto :eof
rem --- end and begin case -----
:begin_end
setlocal disableDelayedExpansion
if %begin% GTR %end% goto :break_be
for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
set "line=%%L"
for /F "delims=:" %%n in ("%%L") do (
IF %%n GEQ %begin% IF %%n LEQ %end% (
setlocal EnableDelayedExpansion
set "text=!line:*:=!"
(echo(!text!)
endlocal
) ELSE goto :break_be
)
)
:break_be
endlocal
endlocal
goto :eof
rem ------------------
rem --- HELP ---------
rem ------------------
:help
echo(
echo %~n0 - dipsplays a lines of a file defined by -BEGIN and -END arguments passed to it
echo(
echo( USAGE:
echo(
echo %~n0 -file=file_to_process {-begin=begin_line ^| -end=end_line }
echo or
echo %~n0 -file file_to_process {-begin begin_line ^| -end end_line }
echo(
echo( if some of arguments BEGIN or END has a negative number it will start to count from the end of file
echo(
echo( http://ss64.org/viewtopic.php^?id^=1707
echo(
goto :eof
EDIT - last 100 lines:
#echo off
setlocal
set filename=text_file.txt
for /F %%C in ('find /c /v "" ^<"%filename%"') do set /a lines_count=%%C
set /a last_100_lines=lines_count-100
type %filename% | more /e +%last_100_lines%

Resources