Advertisement
npocmaka

tailhead.2.5.1.bat

May 29th, 2013
72
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. :begin_only
  79. setlocal enableDelayedExpansion
  80.     set counter=0
  81.     for /f "usebackq delims=" %%L in ("%file%") do (
  82.         set /a counter=!counter!+1
  83.         if !counter! GEQ %begin% (
  84.             setlocal disableDelayedExpansion
  85.             (echo(%%L)
  86.              endlocal
  87.         )
  88.     )  
  89. endlocal
  90. goto :eof
  91.  
  92. :end_only
  93. setlocal enableDelayedExpansion
  94.     set counter=0
  95.     for /f "usebackq delims=" %%L in ("%file%") do (
  96.         set /a counter=!counter!+1
  97.         if !counter! LEQ %end% (
  98.             setlocal disableDelayedExpansion
  99.             (echo(%%L)
  100.              endlocal
  101.         ) else goto :break_eo
  102.     )
  103.     :break_eo
  104. endlocal
  105. goto :eof
  106.  
  107. rem ---- begin and end ---
  108.  
  109. :begin_end
  110. setlocal enableDelayedExpansion
  111.     if %begin% GTR %end% goto :break_be
  112.     set counter=0
  113.     for /f "usebackq delims=" %%L in ("%file%") do (
  114.         set /a counter=!counter!+1
  115.         IF !counter! GEQ %begin% IF !counter! LEQ %end% (
  116.             setlocal disableDelayedExpansion
  117.             (echo(%%L)
  118.              endlocal
  119.         ) else goto :break_be
  120.     )
  121.     :break_be
  122.  
  123. endlocal
  124. goto :eof
  125.  
  126. rem ------------------
  127. rem --- HELP ---------
  128. rem ------------------
  129. :help
  130.     echo(
  131.         echo %~n0 - dipsplays a lines of a file defined by -BEGIN and -END arguments passed to it
  132.         echo(
  133.         echo( USAGE:
  134.         echo(
  135.         echo %~n0  -file=file_to_process {-begin=begin_line ^| -end=end_line }
  136.         echo or
  137.         echo %~n0  -file file_to_process {-begin begin_line ^| -end end_line }
  138.         echo(
  139.         echo( if some of arguments BEGIN or END has a negative number it will start to count from the end of file
  140.         echo(
  141.         echo( http://ss64.org/viewtopic.php^?id^=1707
  142.         echo(
  143.         echo( by Vasil "npocmaka" Arnaudov
  144. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement