我发现了一个以前的帖子,让这个答案的一部分,创建与目录中的每个文件的最后一行新的文件,如下图所示:
@echo off
for %%f in (*.log) do (
set /a line_count = -1
for /f %%l in (%%f) do set /a line_count += 1
more +%line_count% %%f
)
如果你的文件不以换行符结束,你就必须在LINE_COUNT变量初始化为0,而不是-1。
您可以重定向更多的输出追加结果到一个文件中:
more +%line_count% %%f >> your_results_file
我需要什么帮助,现在用是找出一种方法来追加的最后一行正在从该行采取的新文件.txt文件的文件名,所以这将是文件名的最后一行,对每个文件在目录中。
这是不是任何人都可以帮助? 谢谢。
你可以
@echo off
setlocal enabledelayedexpansion
for %%f in (*.log) do (
set /a line_count = -1
for /f "tokens=*" %%l in (%%f) do (
set /a line_count += 1
set value=%%l
set file=%%f
)
echo line_count=%line_count% value=!value! file=!file!
)
我的天哪 - 不要使用FOR循环在文件计数线 - 这是缓慢和不可靠的。 它会跳过空行,也可以跳过与EOL字符开头,除非您禁用EOL线。
使用FIND /C
计算行数。 与空搜索字符串/V
选项会匹配所有行。
使用SET / P来打印文件的名称,而不会发出一个换行符。
@echo off
setlocal enableDelayedExpansion
>output.txt (
for %%F in (*.log) do (
<nul set /p "=%%F: "
for /f %%N in ('type "%%F"^|find /c /v ""') do set /a skip=%%N
if !skip! gtr 0 set /a skip-=1
more +!skip! "%%F"
)
)
如果有任何文件名包含!
然后延迟扩展必须打开和关闭
@echo off
setlocal disableDelayedExpansion
>output.txt (
for %%F in (*.bat) do (
set "file=%%F"
setlocal enableDelayedExpansion
<nul set /p "=!file!: "
for /f %%N in ('type "!file!"^|find /c /v ""') do set /a skip=%%N
if !skip! gtr 0 set /a skip-=1
more +!skip! "!file!"
endlocal
)
)
type output.txt
我的两个例子并不文本文件的最后几行复制到另一个文件,但它可以用来做。
我的例子,而不是工作像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
方法大大快于MORE
。
文章来源: batch to copy last line of several .txt files into 1 new .txt file and appends file name on new line