2016-10-06 121 views
1

我是一種新的批處理命令,並試圖自動化我們手動做的事情。我有一個每天下載的日誌文件,然後使用Findstr(輸出到另一個文件)搜索其中的某些項目,然後將發現的內容拆分爲較小的文件。批處理文件命令 - Findstr,分裂和文件名變量

我在製作文件名和文件名變量時遇到了問題,並在我的代碼中使用它。 splitter代碼給了我,所以我只是試圖將Findstr合併到它。任何提示或看看我做錯了什麼?

實施例:

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

它看起來通過10-06-16_file.log用於與廣告活動ID 1234,並將它們輸出到fnd_10-06-16_file.log任何行。

如果fnd_10-06-16_file.log文件大小比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 
+2

您是否試圖使用此代碼獲取展示文件的大小:SET fnd_impressionfile_sz = %%〜zfnd_impressionfile'。如果是這樣,那不是它的工作原理。你需要使用FOR命令來做到這一點。 – Squashman

回答

0

首先,更改SET fnd_impressionfile_sz=%%~zfnd_impressionfileFOR %%a in (%fnd_impressionfile%) DO SET fnd_impressionfile_sz=%%~za

然後,仔細檢查你maxbytesize。你的描述說kb,但批處理意味着字節。 fnd_impressionfile_sz包含字節值。

我看到的另一件事是在那裏你使用重定向操作符>在if fnd_impressionfile_sz > maxbytesize。您應該將其更改爲GTRGEQ。請參閱HELP IF