Advertisement
npocmaka

tailhead.2.0.2.bat

May 28th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2. setlocal
  3.  
  4. rem ---------------------------
  5. rem ------ arg parsing --------
  6. rem ---------------------------
  7.  
  8.     if "%~1" equ "" goto :help
  9.         for %%H in (/h -h /help -help) do (
  10.                 if /I "%~1" equ "%%H" goto :help
  11.         )
  12.         setlocal enableDelayedExpansion
  13.             set "prev="
  14.             for %%A in (%*) do (
  15.                     if /I "!prev!" equ "-file" set file=%%~fsA
  16.                     if /I "!prev!" equ "-begin" set begin=%%~A
  17.                     if /I "!prev!" equ "-end" set end=%%A
  18.                     set prev=%%~A
  19.             )
  20.         endlocal & (
  21.                 if "%file%" neq "" (set file=%file%)
  22.                 if "%begin%" neq "" (set /a begin=%begin%)
  23.                 if "%end%" neq "" (set /a end=%end%)
  24.         )
  25.  
  26. rem -----------------------------
  27. rem --- invalid cases check -----
  28. rem -----------------------------
  29.        
  30.         if "%file%" EQU "" echo file not defined && exit /b 1
  31.         if not exist "%file%"  echo file not exists && exit /b 2
  32.         if not defined begin if not defined end echo neither BEGIN line nor END line are defined && exit /b 3
  33.        
  34. rem --------------------------
  35. rem -- function selection ----
  36. rem --------------------------
  37.        
  38.         if defined begin if %begin%0 LSS 0 for /F %%C in ('find /c /v "" ^<"%file%"')  do set /a lines_count=%%C
  39.         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
  40.                
  41.                 rem -- begin only
  42.         if not defined begin if defined end if %end%0 GEQ 0 goto :end_only
  43.         if not defined begin if defined end if %end%0 LSS 0 (
  44.                         set /a end=%lines_count%%end%+1
  45.                         goto :end_only
  46.                 )
  47.                
  48.                 rem -- end only
  49.         if not defined end if defined begin if %begin%0 GEQ 0 goto :begin_only
  50.         if not defined end if defined begin if %begin%0 LSS 0 (
  51.                         set /a begin=%lines_count%%begin%+1
  52.                         goto :begin_only
  53.                 )
  54.                 rem -- begin and end
  55.         if %begin%0 LSS 0 if %end%0 LSS 0 (
  56.                         set /a begin=%lines_count%%begin%+1
  57.                         set /a end=%lines_count%%end%+1
  58.                         goto :begin_end
  59.                 )
  60.         if %begin%0 LSS 0 if %end%0 GEQ 0 (
  61.                         set /a begin=%lines_count%%begin%+1
  62.                         goto :begin_end
  63.                 )
  64.         if %begin%0 GEQ 0 if %end%0 LSS 0 (
  65.                         set /a end=%lines_count%%end%+1
  66.                         goto :begin_end
  67.                 )
  68.         if %begin%0 GEQ 0 if %end%0 GEQ 0 (
  69.                         goto :begin_end
  70.                 )      
  71. goto :eof
  72.  
  73. rem -------------------------
  74. rem ------ functions --------
  75. rem -------------------------
  76.  
  77. rem -----  single cases -----
  78.  
  79. :begin_only
  80.         setlocal DisableDelayedExpansion
  81.         for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
  82.                 set "line=%%L"
  83.                 for /F "delims=:" %%n in ("%%L") do (
  84.                         if %%n GEQ %begin% (
  85.                                 setlocal EnableDelayedExpansion
  86.                                 set "text=!line:*:=!"
  87.                                 (echo(!text!)
  88.                                 endlocal
  89.                         )
  90.                 )
  91.         )
  92.         endlocal
  93. endlocal
  94. goto :eof
  95.  
  96. :end_only
  97.         setlocal disableDelayedExpansion
  98.         for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
  99.                 set "line=%%L"
  100.                 for /F "delims=:" %%n in ("%%L") do (
  101.                         IF %%n LEQ %end% (
  102.                                 setlocal EnableDelayedExpansion
  103.                                 set "text=!line:*:=!"
  104.                                 (echo(!text!)
  105.                                 endlocal
  106.                         ) ELSE goto :break_eo
  107.                 )
  108.         )
  109.         :break_eo
  110.         endlocal
  111. endlocal
  112. goto :eof
  113.  
  114. rem ---  end and begin case  -----
  115.  
  116. :begin_end
  117.         setlocal disableDelayedExpansion
  118.         if %begin% GTR %end% goto :break_be
  119.         for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
  120.                 set "line=%%L"
  121.                 for /F "delims=:" %%n in ("%%L") do (
  122.                     IF %%n GEQ %begin% IF %%n LEQ %end% (        
  123.                         setlocal EnableDelayedExpansion
  124.                         set "text=!line:*:=!"
  125.                        (echo(!text!)
  126.                         endlocal
  127.                     ) ELSE goto :break_be                              
  128.                 )
  129.         )
  130.         :break_be
  131.         endlocal
  132. endlocal
  133. goto :eof
  134. rem ------------------
  135. rem --- HELP ---------
  136. rem ------------------
  137. :help
  138.     echo(
  139.         echo %~n0 - dipsplays a lines of a file defined by -BEGIN and -END arguments passed to it
  140.         echo(
  141.         echo( USAGE:
  142.         echo(
  143.         echo %~n0  -file=file_to_process {-begin=begin_line ^| -end=end_line }
  144.         echo or
  145.         echo %~n0  -file file_to_process {-begin begin_line ^| -end end_line }
  146.         echo(
  147.         echo( if some of arguments BEGIN or END has a negative number it will start to count from the end of file
  148.         echo(
  149.         echo( http://ss64.org/viewtopic.php^?id^=1707
  150.         echo(
  151.         echo( by Vasil "npocmaka" Arnaudov
  152. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement