我的兩個例子切換不文本文件的最後幾行復制到另一個文件,但它可以被用來這樣做。
我的示例反而像Linux命令TAIL
一樣工作,並顯示最後幾行。
經過一番實驗,我發現以下是顯示文件最後幾行的最快方法。當然,缺點是FOR
會跳過空行。
TAIL.BAT
@echo off
setlocal enabledelayedexpansion
set "fn=%~1"
set "fn=%fn:(=^(%"
set "fn=%fn:)=^)%"
if "%~2"=="" set count=1
if "%~2" neq "" set count=%~2
(
for /f "tokens=3" %%x in ('find /c /v "" %fn%') do set count=%%x
set /a count=!count!-%count%
)
echo.
for /f "skip=%count% tokens=*" %%x in (%fn%) do echo %%x
%1
是文件名
%2
被線顯示的選項號。如果留空,則TAIL.BAT
顯示最後一行。
要確定其中兩種方法都是快,FOR
,或MORE
方法,我創建了下面的(與很多的話)。我將它包含在內,以便您可以自己測試它。
注意,這個例子中的最大部分是經過時間的計算:
TESTTAIL.BAT
@echo off
::
:: :::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: TAIL.BAT filename [linesToShow [/v]]
::
:: LINESTOSHOW = Number of lines at the end of the file
:: to display. If this is left blank TAIL
:: assumes that you only want the last line
::
:: /v = Show the math, must be used with LINESTOSHOW
:: Without this switch, the time to complete
:: will be displayed, but not the math.
::
setlocal enabledelayedexpansion
set "fn=%~1" & :: Get File Name
set "fn=%fn:(=^(%" & :: Escape Left Brackets
set "fn=%fn:)=^)%" & :: Escape Right Brackets
if "%~2"=="" set count=1 & :: Assume Last Line
if "%~2" neq "" set count=%~2 & :: Specify # of Lines to show
(:: Count Lines in File
for /f "tokens=3" %%x in ('find /c /v "" %fn%') do set count=%%x
:: Subtract # to skip from lines in file
set /a count=!count!-%count%
)
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
:: :: Get last %Count% lines of file using MORE+ :: ::
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
echo.
set start=%time% & :: Get Start Time
more +%count% %fn% & :: Show the last %count% lines
set end=%time% & :: Get End Time
call :Elapsed %end% %start% %3 & :: Calculate Time Elapsed
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
:: :: Get last %Count% lines of file using FOR :: ::
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
set start=%time% & :: Get Start Time
for /f "skip=%count% tokens=*" %%x in (%fn%) do echo %%x
set end=%time% & :: Get End Time
call :Elapsed %end% %start% %3 & :: Calculate Time Elapsed
endlocal
goto :eof
:Elapsed
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
:: Calculate Elapsed Time ::
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
echo.
if "%3"=="/v" ECHO Start : %1
if "%3"=="/v" ECHO End : -%2
if "%3"=="/v" ECHO =============
for /f "tokens=1-8 delims=:.%tab% " %%a in ("%1 %2") do (
set "hh= %%a - %%e "
set "mm= %%b - %%f "
set "ss= %%c - %%g "
set "ms= %%d - %%h "
)
set "hh=%hh: 0=%"
set /a hh=%hh%
set "mm=%mm: 0=%"
set /a mm=%mm%
set "ss=%ss: 0=%"
set /a ss=%ss%
set "ms=%ms: 0=%"
set /a ms=%ms%
if %ms% lss 0 (
set /a ms=100+%ms%
set /a ss-=1
)
if %ss% lss 0 (
set /a ss=60+%ss%
set /a mm-=1
)
if %mm% lss 0 (
set /a mm=60+%mm%
set /a hh-=1
)
if %hh% lss 0 set /a hh=24+%hh%
if %hh% lss 10 set hh=0%hh%
if %mm% lss 10 set mm=0%mm%
if %ss% lss 10 set ss=0%ss%
if %ms% lss 10 set ms=0%ms%
echo Completed in : %hh%:%mm%:%ss%.%ms%
echo.
goto :eof
這裏是我的機器上運行上面的示例代碼,請注意我指定的最後5行:
D:\Misc>testtail 2012-10-25(9).MIME 5 /v
MDAwMDAgbg0KMDAwMDA5NTQwOCAwMDAwMCBuDQp0cmFpbGVyDQo8PA0KL1NpemUgMzUNCi9Sb290
IDMzIDAgUg0KL0luZm8gMzAgMCBSDQovSUQgWzwxM0NBMjNBRjhFMTk2RkEyOTRGMThFM0I3QTJC
QkNFNj48N0I5OUE4NTkxOEE1MzgzQjY3NTY5OUMxNkFEQTA3RUQ+XQ0KPj4NCnN0YXJ0eHJlZg0K
OTcyNDMNCiUlRU9GDQo=
---915665055-1601124504-1351078287=:50774--
Start : 15:30:57.11
End : -15:30:56.96
=============
Completed in : 00:00:00.15
MDAwMDAgbg0KMDAwMDA5NTQwOCAwMDAwMCBuDQp0cmFpbGVyDQo8PA0KL1NpemUgMzUNCi9Sb290
IDMzIDAgUg0KL0luZm8gMzAgMCBSDQovSUQgWzwxM0NBMjNBRjhFMTk2RkEyOTRGMThFM0I3QTJC
QkNFNj48N0I5OUE4NTkxOEE1MzgzQjY3NTY5OUMxNkFEQTA3RUQ+XQ0KPj4NCnN0YXJ0eHJlZg0K
OTcyNDMNCiUlRU9GDQo=
---915665055-1601124504-1351078287=:50774--
Start : 15:30:57.16
End : -15:30:57.15
=============
Completed in : 00:00:00.01
D:\Misc>
正如你所看到的,在這兩個之間有很大的速度差,與FOR
方法大大faste r比MORE
。
你有沒有考慮過使用PowerShell?這在PowerShell中很簡單。 – David