如何使用批量读取文本文件导出到输出文件

时间:2013-09-16 09:10:28

标签: batch-file

我有文本文件格式

2013-08-13 17:12:39 3868 0 status: Starting upload of U:\AOT\KPT_BU\

2013-08-13 17:12:39 3868 0 status: Starting upload of U:\AOT\KPT_BU\

2013-09-13 17:12:39 3868 0 status: Starting upload of U:\AOT\KPT_BU\

2013-09-13 17:12:39 3868 0 status: Starting upload of U:\AOT\KPT_BU\

2013-08-13 17:12:39 3868 0 status: Starting upload of U:\AOT\KPS\

2013-08-13 17:12:39 3868 0 status: Starting upload of U:\AOT\KPS\

2013-08-13 17:12:39 3868 0 status: Starting upload of U:\AOT\KPS\

2013-09-13 17:12:39 3868 0 status: Starting upload of U:\AOT\KPS\

2013-09-13 17:12:39 3868 0 status: Starting upload of U:\AOT\KPS\

2013-09-13 17:12:39 3868 0 status: Starting upload of U:\AOT\KPS\

2013-09-13 17:12:39 3868 0 status: Starting upload of U:\AOT\KPS\

我想要输出文件格式

date 2013-08-13 KPT_BU total = 2

date 2013-08-13 KPS Total = 3

date 2013-09-13 KPT_BU total = 2

date 2013-09-13 KPS total = 4

2 个答案:

答案 0 :(得分:0)

我假设你想要实现以下目标: 您希望将上传计入每个日期的指定文件夹。

我的解决方案为每个日期和路径名组合创建一个环境变量,并将计数器放入该变量中。它非常依赖于ENABLEDELAYEDEXPANSION,如果没有它,我无法想到一个解决方案,因为你不能在'for'结构中使用替代调用/ %%。 如果有人知道解决方案,我会很高兴知道这一点。 因此,此解决方案不适用于包含“!”的路径。由于脚本依赖于创建环境变量,因此当您用完环境内存时它将停止。

@echo off
setlocal ENABLEDELAYEDEXPANSION
:: remove all variabel starting with $ they will be restored after 'endlocal'
for /f "tokens=1 delims==" %%# in ('set $') do set "%%#="
:: create an environment variable from the date and path and increase its
:: value for every match
for /f "delims=" %%# in (testfile.txt) do (
    :: get first and last token ($1=date and $2=path) from line
    set "$1="
    for %%A in (%%#) do set $1>nul 2>nul||set "$1=%%A" & set "$2=%%A"
    :: get last part of path from $2 (mask '\' with '" "' to use in for
    set "$2="!$2:\=" "!
    for %%A in (!$2!) do set $2=%%~A
    :: generate envvar '-' must be removed, because set/a gets confused by '-'
    set "_=$!$1:-=!!$2!"
    :: counting in created vars
    if defined !_! ( set /a "!_!+=1" ) else ( set "!_!=1" ) 
)
set "$1=" & set "$2="
::output results
for /f "tokens=1,2 delims==" %%A in ('set $ 2^>nul') do (
    set _=%%A
    echo date !_:~1,4!-!_:~5,2!-!_:~7,2! !_:~8! Total = %%B
)
endlocal
goto:eof

输出略有不同,因为'set $'按字母顺序对变量进行排序。

答案 1 :(得分:0)

运行bat文件时的输出文件

++++++++++++++++++++++++++++++++++++++++ date 2013-09-11 1总计= 736 ---->不使用

date 2013-09-11 111092013_66file_B Total = 64

日期2013-09-11 1KPDC总计= 1

日期2013-09-11 1KPT总计= 1

日期2013-09-11 1KPT_BU总计= 7

日期2-01-30 0911总计= 1

++++++++++++++++++++++++++++++++++++++++++

我有完整的日志文件

http://www.4shared.com/file/eax2ykoS/filezilla.html

谢谢..