批处理将几个.txt文件的最后一行复制到一个新的.text文件中,并在新行上附加文件名

时间:2012-10-25 16:33:57

标签: windows batch-file cmd

我发现上一篇文章提供了这个答案的一部分,用目录中每个文件的最后一行创建新文件,如下所示:

@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文件的文件名附加到新文件中的行,因此它将是文件名的最后一行,目录中的文件。

这是否有人可以提供帮助?谢谢。

3 个答案:

答案 0 :(得分:0)

你可以

@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!
)

答案 1 :(得分:0)

哦,天哪 - 不要使用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

答案 2 :(得分:0)

我的两个示例不会将文本文件的最后几行复制到另一个文件,但可以使用它来执行此操作。

我的示例改为像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会显示最后一行。


为了确定两种方法中的哪一种更快,FORMORE方法,我创建了以下内容(有大量备注)。我把它包括在内,以便你自己测试一下。

请注意,此示例的最大部分是经过时间计算:

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