1iJaCk

tail.bat

Aug 18th, 2016
309
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.14 KB | None | 0 0
  1. :: tail.bat File Num [/N|/E|/C|/P|/S|/Tn]...
  2. ::
  3. ::   Prints the last Num lines of text file File.
  4. ::
  5. ::   The output will pause after filling the screen unless the /N option
  6. ::   is specified
  7. ::
  8. ::   The standard MORE options /E /C /P /S /Tn can be specified.
  9. ::   See MORE /? for more information
  10. ::
  11. :: Source: dbenham | 02/12/2012
  12. :: http://stackoverflow.com/a/9251975
  13. ::    
  14. ::
  15. :: Mods: Alex V | 08/17/2016
  16. ::
  17. :: Usage: tail.bat C:\foo.bar 10 /N
  18. :::::::::::::::::::::::::::::::::::::::
  19. @echo OFF
  20. setlocal
  21.  
  22. :: Variables ::::::::::::::::::::::::::
  23. set "default_lines_to_display="      rem :: Will only set if none passed in
  24. :::::::::::::::::::::::::::::::::::::::
  25.  
  26. :: Defaults :::::::::::::::::::::::::::
  27. set "file=%~1"
  28. set "cnt=%~2"
  29. shift /1
  30. shift /1
  31. set "options="
  32. set "noPause="
  33.  
  34. :: Parse Parameters :::::::::::::::::::
  35. :parseOptions
  36. if "%~1" neq "" (
  37.   if /i "%~1" equ "/N" (set noPause=^| findstr "^") else set options=%options% %~1
  38.   shift /1
  39.   goto :parseOptions
  40. )
  41.  
  42. :: Get total lines ::::::::::::::::::::
  43. if defined file (
  44.     if exist "%file%" (
  45.         for /f %%N in ('find /c /v "" ^<"%file%"') do set "skip=%%N"
  46.     ) else (
  47.         echo ERROR: Unable to locate %file%
  48.     )
  49. ) else (
  50.     echo ERROR: Missing file parameter
  51.     echo.
  52.     goto :helpMe
  53. )
  54.  
  55. :: Setting Default Line Count if NA :::
  56. if not defined cnt (
  57.   if not defined default_lines_to_display (
  58.     set "cnt=0"
  59.   ) else (
  60.     set /a "cnt+=%default_lines_to_display%"
  61.   )
  62. )
  63.  
  64. :: Defining number of lines to display
  65. if not defined skip set "skip=0"
  66. set /a "skip-=%cnt%"
  67. if %skip% lss 0 set skip=0
  68.  
  69.  
  70. :: FINALLY - "more" "Tail" "MORE" :::::
  71. more +%skip% %options% "%file%" %noPause%
  72. goto :eof
  73.  
  74. :helpMe
  75. echo USAGE: tail.bat filename [Lines] [/N^|/E^|/C^|/P^|/S^|/Tn]
  76. echo           Prints the last Num lines of text file File.
  77. echo/
  78. echo           The output will pause after filling the screen unless the /N option
  79. echo           is specified
  80. echo/
  81. echo           The standard MORE options /E /C /P /S /Tn can be specified.
  82. echo           See MORE /? for more information
  83. echo/
  84. echo           Example:
  85. echo              tail.bat "dir to file\foo.bar" 50 /N
  86. echo/
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment