批处理文件命令 - Findstr,拆分和文件名变量

时间:2016-10-06 16:43:30

标签: batch-file cmd findstr

我是批处理命令的新手,并且一直在尝试自动执行我们手动执行的操作。我有一个每天下载的日志文件,然后我使用Findstr(输出到另一个文件)搜索其中的某些项目,然后将找到的内容拆分为较小的文件。

我在制作文件名和文件名变量时遇到了麻烦,并在整个代码中使用它。分配器代码是给我的,所以我只是试图将Findstr合并到其中。任何提示或看看我做错了什么?

示例:

Campaign ID: 1234
Campaign Name: Pepsi
Impression Filename: 10-06-16_file.log
Day of week: 2

对于广告系列标识为10-06-16_file.log的所有行,它会查看1234,并将其输出到fnd_10-06-16_file.log

如果fnd_10-06-16_file.log filesize大于177000kb,则将文件拆分为名为2SplitFile1_Pepsi.log2SplitFile2_Pepsi.log2SplitFile3_Pepsi.log等的较小文件。

setlocal ENABLEDELAYEDEXPANSION
@echo off

REM Ask for Campaign information to find
SET /P campaignid="Campaign ID(s): "
SET /P campaignname="Campaign Name: "
SET /P impressionfile="Impression Filename: "
SET /P dayofweek="Day of week: "

SET fnd_impressionfile=%campaignname%_%impressionfile%
SET maxbytesize=177000
SET fnd_impressionfile_sz=%%~zfnd_impressionfile


REM Find campaigns inside log file
findstr "%campaignname%" %impressionfile% > %fnd_impressionfile%


REM Split log file if greater than 177000 kb
if fnd_impressionfile_sz > maxbytesize (

    REM Edit this value to change the name of the file that needs splitting. Include the extension.
    SET BFN=%fnd_impressionfile%
    REM Edit this value to change the number of lines per file.
    SET LPF=1000000
    REM Edit this value to change the name of each short file. It will be followed by a number indicating where it is in the list.
    SET SFN=SplitFile

    REM Do not change beyond this line.

    SET SFX=%BFN:~-3%

    SET /A LineNum=0
    SET /A FileNum=1

    For /F "delims=" %%l in (%BFN%) Do (
        SET /A LineNum+=1

        echo %%l >> %dayofweek%%SFN%!FileNum!_%campaignname%.%SFX%

        if !LineNum! EQU !LPF! (
            SET /A LineNum=0
            SET /A FileNum+=1
            )

        )
)

endlocal
pause

1 个答案:

答案 0 :(得分:0)

首先,将SET fnd_impressionfile_sz=%%~zfnd_impressionfile更改为FOR %%a in (%fnd_impressionfile%) DO SET fnd_impressionfile_sz=%%~za

然后,仔细检查你的maxbytesize。你的描述是kb,但批量意味着字节。 fnd_impressionfile_sz包含字节值。

我看到的另一件事是您使用重定向运算符>在if fnd_impressionfile_sz > maxbytesize。您应该将其更改为GTRGEQ。请参阅HELP IF