如何在许多文本文件中查找变量字符串并将文本文件名和找到的字符串输出到CSV文件中?

时间:2016-06-25 17:32:12

标签: batch-file cmd

以下内容适用于所有时间值不同的文件。

[ boomer ]  elapsed:            00:00:02

我希望输出如下所示为CSV文件。我试图检查已经过的字符串,但是在Windows命令行中输出不正确。

logfile1 - Copy (2).txt,00:00:02
logfile1 - Copy.txt,00:00:34
logfile1.txt,00:00:09
New folder\logfile1 - Copy (2).txt,00:00:02

文件中的示例文本为:

logfile1 - 复制(2).txt:

[ boomer ]  elapsed:            00:00:02

logfile1 - Copy.txt:

[ boomer]  elapsed:            00:00:34

logfile1.txt:

[ boomer]  elapsed:            00:00:09

新文件夹\ logfile1 - 复制(2).txt:

[ boomer]  elapsed:            00:00:02

新文件夹\ logfile1 - Copy.txt:

[ boomer]  elapsed:            00:00:34

新文件夹\ logfile1.txt:

[ boomer]  elapsed:            00:00:09

以下是我尝试实现结果的代码:

set /P filename=Please enter csv file name:
echo file,time>%filename%.csv
findstr /s /x "elapsed" .txt >> %filename%.txt
@echo off
setlocal DisableDelayedExpansion
set INTEXTFILE=test.txt
set OUTTEXTFILE=temp.txt
set SEARCHTEXT=:
set REPTEXT=,
set OUTPUTLINE=
for /f "tokens=1, delims=¶" %%A in ('"type %INTEXTFILE%"') do (
    SET string=%%A
    setLocal EnableDelayedExpansion
    SET modified=!string:%SEARCHTEXT%=%REPTEXTT%!
    echo !modified! >> %OUTTEXTFILE%
    endlocal
)

1 个答案:

答案 0 :(得分:0)

我认为[ boomer]没有关闭方括号的空格是输入错误,所有文件都包含[ boomer ]。但是,这种差异使得(很容易)使用空格/制表符作为分隔符来处理包含elapsed:的文件中的行和感兴趣的时间数据。

因此,Windows标准控制台应用程序 FINDSTR 用于搜索包含elapsed:行的* .txt文件。使用

调用 FINDSTR 的输出
%SystemRoot%\System32\findstr.exe /L /S /C:elapsed: *.txt

用于提供的示例文件和数据

logfile1 - Copy (2).txt:[ boomer ]  elapsed:            00:00:02
logfile1 - Copy.txt:[ boomer]  elapsed:            00:00:34
logfile1.txt:[ boomer]  elapsed:            00:00:09
New folder\logfile1.txt:[ boomer]  elapsed:            00:00:09
New folder\logfile1 - Copy.txt:[ boomer]  elapsed:            00:00:34
New folder\logfile1 - Copy (2).txt:[ boomer ]  elapsed:            00:00:02

但是,如果 FINDSTR 命令末尾的文件模式使用完整路径,例如使用

%SystemRoot%\System32\findstr.exe /L /S /C:elapsed: "C:\Temp\*.txt"

文件名也以完整路径输出

C:\Temp\logfile1 - Copy (2).txt:[ boomer ]  elapsed:            00:00:02
C:\Temp\logfile1 - Copy.txt:[ boomer]  elapsed:            00:00:34
C:\Temp\logfile1.txt:[ boomer]  elapsed:            00:00:09
C:\Temp\New folder\logfile1.txt:[ boomer]  elapsed:            00:00:09
C:\Temp\New folder\logfile1 - Copy.txt:[ boomer]  elapsed:            00:00:34
C:\Temp\New folder\logfile1 - Copy (2).txt:[ boomer ]  elapsed:            00:00:02

现在还包含驱动器号后的冒号。

在解析 FINDSTR 的输出行时必须考虑这种差异,就像下面为本地或具有驱动器号的网络驱动器上的源目录设计的两个批处理文件中所做的那样。

使用相对于当前目录的路径获取文件名的批处理代码:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem Delete the output file if existing already in current directory
rem for example from a previous execution of this batch file.
if exist OutputList.csv del /F OutputList.csv
goto CreateCSV

rem Execute console application FINDSTR to search in current directory and
rem all subdirectories for files with file extension TXT containing a line
rem with "elapsed:". FINDSTR outputs the file name with relative path and
rem the line containing the searched string with a colon as separator.

rem As each found line contains also a colon after "elapsed" the colon
rem is used as delimiter to split up each line into 3 substrings (tokens)
rem assigned to the loop variables A, B and C. Loop variable A holds the
rem file name (with relative path), loop variable B holds the line from
rem file up to end of "elapsed" and loop variable C holds the remaining
rem string on the line with last 8 characters being the time data.

rem A second FOR loop is used to trim preceding and trailing spaces/tabs
rem from the substring with the time data.

rem For each line returned by FINDSTR a row is appended to the output CSV
rem file with the file name and the time data. If the file name contains
rem itself a comma, the file name is enclosed in double quotes to produce
rem a valid CSV file also for such an unusual file name.

:CreateCSV
for /F "tokens=1,2* delims=:" %%A in ('%SystemRoot%\System32\findstr.exe /L /S /C:elapsed: *.txt 2^>nul') do (
    for /F %%I in ("%%C") do set "TimeData=%%I"
    set "FileName=%%A"
    if "!FileName:,=!" == "!FileName!" (
        echo !FileName!,!TimeData!>>OutputList.csv
    ) else (
        echo "!FileName!",!TimeData!>>OutputList.csv
    )
)
endlocal

使用绝对路径获取文件名的类似批处理代码:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem Define source directory. Use %CD% instead of C:\Temp to use the
rem current directory on running the batch file or use %~dp0 to use
rem the directory of the batch file as source directory.
set "SourceDirectory=C:\Temp"

rem Make sure the source directory path does not end with a backslash
rem and check if the source directory exists at all, otherwise exit.
if "%SourceDirectory:~-1%" == "\" set "SourceDirectory=%SourceDirectory:~0,-1%"
if not exist "%SourceDirectory%\*" goto :EOF

rem Delete the output file if existing already in source directory
rem for example from a previous execution of this batch file.
set "OutputFile=%SourceDirectory%\OutputList.csv"
if exist "%OutputFile%" del /F "%OutputFile%"
goto CreateCSV

rem Execute console application FINDSTR to search in source directory and
rem all subdirectories for files with file extension TXT containing a line
rem with "elapsed:". FINDSTR outputs the file name with absolute path and
rem the line containing the searched string with a colon as separator.

rem As each found line contains also a colon after "elapsed" the colon
rem is used as delimiter to split up each line into 4 substrings (tokens)
rem assigned to the loop variables A, B, C and D. Loop variable A holds the
rem drive letter, loop variable B the path and file name, loop variable C
rem holds the line from file up to end of "elapsed" and loop variable D
rem holds the remaining string on the line with last 8 characters being
rem the time data.

rem A second FOR loop is used to trim preceding and trailing spaces/tabs
rem from the substring with the time data.

rem For each line returned by FINDSTR a row is appended to the output CSV
rem file with the file name and the time data. If the file name contains
rem itself a comma, the file name is enclosed in double quotes to produce
rem a valid CSV file also for such an unusual file name.

:CreateCSV
for /F "tokens=1-3* delims=:" %%A in ('%SystemRoot%\System32\findstr.exe /L /S /C:elapsed: "%SourceDirectory%\*.txt" 2^>nul') do (
    for /F %%I in ("%%D") do set "TimeData=%%I"
    set "FileName=%%A:%%B"
    if "!FileName:,=!" == "!FileName!" (
        echo !FileName!,!TimeData!>>"%OutputFile%"
    ) else (
        echo "!FileName!",!TimeData!>>"%OutputFile%"
    )
)
endlocal

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

另请阅读Microsoft文章Using command redirection operators,了解>>2>nul的解释。

重定向操作符>必须在 FOR 命令中进行转义,并在执行 FINDSTR 时应用^并且不会被解释为重定向命令 FOR 在错误的位置会导致语法错误。将 STDERR 2>nul重定向到设备 NUL 用于抑制 FINDSTR 输出的错误消息,如果它找不到任何* .txt文件包含elapsed: