Batch file 批处理文件命令-Findstr、拆分和文件名变量
我是批处理命令的新手,一直在尝试将手动操作自动化。我有一个每天下载的日志文件,然后我使用Findstr(输出到另一个文件)搜索其中的某些项目,然后将找到的内容拆分为较小的文件 我在生成文件名和文件名变量以及在代码中使用这些变量时遇到了麻烦。拆分器代码是给我的,所以我只是想把Findstr合并到其中。有什么建议或者我做错了什么 例如: 它将通过Batch file 批处理文件命令-Findstr、拆分和文件名变量,batch-file,cmd,findstr,Batch File,Cmd,Findstr,我是批处理命令的新手,一直在尝试将手动操作自动化。我有一个每天下载的日志文件,然后我使用Findstr(输出到另一个文件)搜索其中的某些项目,然后将找到的内容拆分为较小的文件 我在生成文件名和文件名变量以及在代码中使用这些变量时遇到了麻烦。拆分器代码是给我的,所以我只是想把Findstr合并到其中。有什么建议或者我做错了什么 例如: 它将通过10-06-16_file.log查找活动ID为1234的任何行,并将它们输出到fnd_10-06-16_file.log 如果fnd_10-06-16_f
10-06-16_file.log
查找活动ID为1234
的任何行,并将它们输出到fnd_10-06-16_file.log
如果fnd_10-06-16_file.log
文件大小大于177000kb,则将该文件拆分为名为2SplitFile1_Pepsi.log
,2SplitFile2_Pepsi.log
,2SplitFile3_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
首先,将%%a in(%fnd\u impressionfile%)的
设置fnd\u impressionfile\u sz=%%~zfnd\u impressionfile
更改为,设置fnd\u impressionfile\u sz=%%~za
然后,再次检查maxbytesize。您的描述是kb,但批处理意味着字节。fnd_impressionfile_sz包含字节值
我看到的另一件事是,如果fnd_impressionfile_sz>maxbytesize,则在
中使用重定向操作符>。您应该将其更改为GTR
或GEQ
。请参阅帮助,如果首先,将设置fnd\u印象派文件更改为对于%%a in(%fnd\u印象派文件%)确实设置fnd\u印象派文件>sz=%%~za
然后,再次检查maxbytesize。您的描述是kb,但批处理意味着字节。fnd_impressionfile_sz包含字节值
我看到的另一件事是,如果fnd_impressionfile_sz>maxbytesize
,则在中使用重定向操作符>。您应该将其更改为GTR
或GEQ
。如果要使用此代码获取impression文件的大小,请参阅帮助:设置fnd\u impressionfile\u sz=%%~zfnd\u impressionfile
。如果是这样,那就不是它的工作原理。您需要使用FOR命令来执行此操作。是否尝试使用以下代码获取impression文件的大小:SET fnd\u impressionfile\u sz=%%~zfnd\u impressionfile
。如果是这样,那就不是它的工作原理。您需要使用FOR命令来执行此操作。
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