Advertisement
npocmaka

tailhead1.01

May 23rd, 2013
68
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 ('findstr /R /N "^" "%file%" ^| find /C ":"')  do set /a lines_count=%%C
  39.         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
  40.        
  41.         if not defined begin if defined end if %end%0 GEQ 0 goto :positive_end_only
  42.         if not defined begin if defined end if %end%0 LSS 0 goto :negative_end_only
  43.        
  44.         if not defined end if defined begin if %begin%0 GEQ 0 goto :positive_begin_only
  45.         if not defined end if defined begin if %begin%0 LSS 0 goto :negative_begin_only
  46.        
  47.         if %begin%0 LSS 0 if %end%0 LSS 0 goto :negative_negative
  48.         if %begin%0 LSS 0 if %end%0 GEQ 0 goto :negative_positive
  49.         if %begin%0 GEQ 0 if %end%0 LSS 0 goto :positive_negative
  50.         if %begin%0 GEQ 0 if %end%0 GEQ 0 goto :positive_positive
  51.        
  52. goto :eof
  53.  
  54. rem -------------------------
  55. rem ------ functions --------
  56. rem -------------------------
  57.  
  58.  
  59. rem -----  single cases -----
  60.  
  61. :positive_begin_only
  62.         setlocal disableDelayedExpansion
  63.         for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
  64.                 set "line=%%L"
  65.                 for /F "delims=:" %%n in ("%%L") do (
  66.                         IF %%n GEQ %begin% (
  67.                                 setlocal EnableDelayedExpansion
  68.                                 set "text=!line:*:=!"
  69.                                 (echo(!text!)
  70.                                 endlocal
  71.                         )
  72.                 )
  73.         )
  74.         endlocal
  75. endlocal
  76. goto :eof
  77.  
  78. :negative_begin_only
  79.         setlocal DisableDelayedExpansion
  80.         set /a begin_line=%lines_count%%begin%+1
  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_line% (
  85.                                 setlocal EnableDelayedExpansion
  86.                                 set "text=!line:*:=!"
  87.                                 (echo(!text!)
  88.                                 endlocal
  89.                         )
  90.                 )
  91.         )
  92.         endlocal
  93. endlocal
  94. goto :eof
  95.  
  96. :positive_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_peo
  107.                 )
  108.         )
  109.         :break_peo
  110.         endlocal
  111. endlocal
  112. goto :eof
  113.  
  114. :negative_end_only
  115.         setlocal disableDelayedExpansion
  116.         set /a end_line=%lines_count%%end%+1
  117.         for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
  118.                 set "line=%%L"
  119.                 for /F "delims=:" %%n in ("%%L") do (
  120.                         IF %%n LEQ %end_line% (
  121.                                 setlocal EnableDelayedExpansion
  122.                                 set "text=!line:*:=!"
  123.                                 (echo(!text!)
  124.                                 endlocal
  125.                         ) ELSE goto :break_neo
  126.                 )
  127.         )
  128.         :break_neo
  129.         endlocal
  130. endlocal
  131. goto :eof
  132.  
  133. rem ---  end and begin cases -----
  134.  
  135. :negative_negative
  136.         setlocal disableDelayedExpansion
  137.         set /a end_line=%lines_count%%end%+1
  138.         set /a begin_line=%lines_count%%begin%+1
  139.         if %begin_line% GTR %end_line% goto :break_nn
  140.         for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
  141.                 set "line=%%L"
  142.                 for /F "delims=:" %%n in ("%%L") do (
  143.                     IF %%n GEQ %begin_line% (
  144.                                 IF %%n LEQ %end_line% (
  145.                                         setlocal EnableDelayedExpansion
  146.                                         set "text=!line:*:=!"
  147.                                         (echo(!text!)
  148.                                         endlocal
  149.                                 ) ELSE goto :break_nn
  150.                         )      
  151.                 )
  152.         )
  153.         :break_nn
  154.         endlocal
  155. endlocal
  156. goto :eof
  157.  
  158.  
  159. :negative_positive
  160.         setlocal disableDelayedExpansion
  161.         set /a begin_line=%lines_count%%begin%+1
  162.         if %begin_line% GTR %end% goto :break_nn
  163.         for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
  164.                 set "line=%%L"
  165.                 for /F "delims=:" %%n in ("%%L") do (
  166.                     IF %%n GEQ %begin_line% (
  167.                                 IF %%n LEQ %end% (
  168.                                         setlocal EnableDelayedExpansion
  169.                                         set "text=!line:*:=!"
  170.                                         (echo(!text!)
  171.                                         endlocal
  172.                                 ) ELSE goto :break_np
  173.                         )      
  174.                 )
  175.         )
  176.         :break_np
  177.         endlocal
  178. endlocal
  179. goto :eof
  180.  
  181. :positive_negative
  182.         echo oioioi
  183.         setlocal disableDelayedExpansion
  184.         set /a end_line=%lines_count%%end%+1
  185.         if %begin% GTR %end_line% goto :break_nn
  186.         for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
  187.                 set "line=%%L"
  188.                 for /F "delims=:" %%n in ("%%L") do (
  189.                     IF %%n GEQ %begin% (
  190.                                 IF %%n LEQ %end_line% (
  191.                                         setlocal EnableDelayedExpansion
  192.                                         set "text=!line:*:=!"
  193.                                         (echo(!text!)
  194.                                         endlocal
  195.                                 ) ELSE goto :break_pn
  196.                         )      
  197.                 )
  198.         )
  199.         :break_pn
  200.         endlocal
  201. endlocal
  202. goto :eof
  203.  
  204. :positive_positive
  205.         setlocal disableDelayedExpansion
  206.         if %begin% GTR %end% goto :break_nn
  207.         for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
  208.                 set "line=%%L"
  209.                 for /F "delims=:" %%n in ("%%L") do (
  210.                     IF %%n GEQ %begin% (
  211.                                 IF %%n LEQ %end% (
  212.                                         setlocal EnableDelayedExpansion
  213.                                         set "text=!line:*:=!"
  214.                                         (echo(!text!)
  215.                                         endlocal
  216.                                 ) ELSE goto :break_pp
  217.                         )      
  218.                 )
  219.         )
  220.         :break_pp
  221.         endlocal
  222. endlocal
  223. goto :eof
  224.  
  225.  
  226.  
  227.  
  228. rem ------------------
  229. rem --- HELP ---------
  230. rem ------------------
  231.  
  232. :help
  233.     echo(
  234.         echo %~n0 - dipsplays a lines of a file defined by -BEGIN and -END arguments passed to it
  235.         echo(
  236.         echo( USAGE:
  237.         echo(
  238.         echo %~n0  -file=file_to_process {-begin=begin_libe ^| -end=end_line }
  239.         echo or
  240.         echo %~n0  -file file_to_process {-begin begin_libe ^| -end end_line }
  241.         echo(
  242.         echo( if some of arguments BEGIN or END has a negative number it will start to count from the end of file
  243.         echo(
  244.         echo( http://ss64.org/viewtopic.php^?id^=1707
  245.         echo(
  246.         echo( by Vasil "npocmaka" Arnaudov
  247. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement