Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- 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 ('findstr /R /N "^" "%file%" ^| find /C ":"') do set /a lines_count=%%C
- if defined end if %end%0 LSS 0 if not defined lines_count for /F %%C in ('findstr /R /N "^" "%file%" ^| find /C ":"') do set lines_count=%%C
- if not defined begin if defined end if %end%0 GEQ 0 goto :positive_end_only
- if not defined begin if defined end if %end%0 LSS 0 goto :negative_end_only
- if not defined end if defined begin if %begin%0 GEQ 0 goto :positive_begin_only
- if not defined end if defined begin if %begin%0 LSS 0 goto :negative_begin_only
- if %begin%0 LSS 0 if %end%0 LSS 0 goto :negative_negative
- if %begin%0 LSS 0 if %end%0 GEQ 0 goto :negative_positive
- if %begin%0 GEQ 0 if %end%0 LSS 0 goto :positive_negative
- if %begin%0 GEQ 0 if %end%0 GEQ 0 goto :positive_positive
- goto :eof
- rem -------------------------
- rem ------ functions --------
- rem -------------------------
- rem ----- single cases -----
- :positive_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
- :negative_begin_only
- setlocal DisableDelayedExpansion
- set /a begin_line=%lines_count%%begin%+1
- for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
- set "line=%%L"
- for /F "delims=:" %%n in ("%%L") do (
- if %%n GEQ %begin_line% (
- setlocal EnableDelayedExpansion
- set "text=!line:*:=!"
- (echo(!text!)
- endlocal
- )
- )
- )
- endlocal
- endlocal
- goto :eof
- :positive_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_peo
- )
- )
- :break_peo
- endlocal
- endlocal
- goto :eof
- :negative_end_only
- setlocal disableDelayedExpansion
- set /a end_line=%lines_count%%end%+1
- for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
- set "line=%%L"
- for /F "delims=:" %%n in ("%%L") do (
- IF %%n LEQ %end_line% (
- setlocal EnableDelayedExpansion
- set "text=!line:*:=!"
- (echo(!text!)
- endlocal
- ) ELSE goto :break_neo
- )
- )
- :break_neo
- endlocal
- endlocal
- goto :eof
- rem --- end and begin cases -----
- :negative_negative
- setlocal disableDelayedExpansion
- set /a end_line=%lines_count%%end%+1
- set /a begin_line=%lines_count%%begin%+1
- if %begin_line% GTR %end_line% goto :break_nn
- for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
- set "line=%%L"
- for /F "delims=:" %%n in ("%%L") do (
- IF %%n GEQ %begin_line% (
- IF %%n LEQ %end_line% (
- setlocal EnableDelayedExpansion
- set "text=!line:*:=!"
- (echo(!text!)
- endlocal
- ) ELSE goto :break_nn
- )
- )
- )
- :break_nn
- endlocal
- endlocal
- goto :eof
- :negative_positive
- setlocal disableDelayedExpansion
- set /a begin_line=%lines_count%%begin%+1
- if %begin_line% GTR %end% goto :break_nn
- for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
- set "line=%%L"
- for /F "delims=:" %%n in ("%%L") do (
- IF %%n GEQ %begin_line% (
- IF %%n LEQ %end% (
- setlocal EnableDelayedExpansion
- set "text=!line:*:=!"
- (echo(!text!)
- endlocal
- ) ELSE goto :break_np
- )
- )
- )
- :break_np
- endlocal
- endlocal
- goto :eof
- :positive_negative
- echo oioioi
- setlocal disableDelayedExpansion
- set /a end_line=%lines_count%%end%+1
- if %begin% GTR %end_line% goto :break_nn
- 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_line% (
- setlocal EnableDelayedExpansion
- set "text=!line:*:=!"
- (echo(!text!)
- endlocal
- ) ELSE goto :break_pn
- )
- )
- )
- :break_pn
- endlocal
- endlocal
- goto :eof
- :positive_positive
- setlocal disableDelayedExpansion
- if %begin% GTR %end% goto :break_nn
- 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_pp
- )
- )
- )
- :break_pp
- 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_libe ^| -end=end_line }
- echo or
- echo %~n0 -file file_to_process {-begin begin_libe ^| -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(
- echo( by Vasil "npocmaka" Arnaudov
- goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement