Board logo

标题: [文件操作] 【已解决】批处理按照日期生成文件夹后放入按站点拆分的log日志,出错,求指教 [打印本页]

作者: memexiaolv    时间: 2018-10-9 22:22     标题: 【已解决】批处理按照日期生成文件夹后放入按站点拆分的log日志,出错,求指教

想处理apache生成的日志文件,每天按照一定规则提取日志放入当天的文件夹
代码:
  1. @echo off
  2. set year=%date:~0,4%
  3. set month=%date:~5,2%
  4. set day=%date:~8,2%
  5. set path=E:\log\%year%\%year%-%month%\%date:~2,2%-%month%-%day%
  6. if exist %path% (
  7. if exist %path%\ (
  8. exit
  9. ) else (
  10. ren "%path%" "%date:~2,2%-%month%-%day%-BAK"
  11. )
  12. )
  13. mkdir %path%
  14. for /f "delims=" %%a in ('type list.txt') do findstr "%%~a" test.log>>%path%\ %%a.log
  15. pause
复制代码


list.tet文件内容:
www.test1.com
www.test2.com

test.log文件内容:
202.105.58.83 - - [03/Sep/2018:03:18:05 +0800] "GET /zw/czzxzjglqd/ HTTP/1.1" 200 92679 "www.test1.com" "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
202.105.58.83 - - [03/Sep/2018:03:18:05 +0800] "GET /zw/ztzl/ HTTP/1.1" 200 101731 "www.test1.com" "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
202.105.58.83 - - [03/Sep/2018:03:18:11 +0800] "GET /zw/ztzl/jlshbdcjxs/ HTTP/1.1" 200 13349 "www.test1.com" "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
202.105.58.83 - - [03/Sep/2018:03:18:11 +0800] "GET /zw/ztzl/2018cbszx/ HTTP/1.1" 200 15388 "www.test1.com" "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
202.105.58.83 - - [03/Sep/2018:03:18:11 +0800] "GET /zw/ztzl/bsssthj/ HTTP/1.1" 200 93780 "www.test1.com" "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
202.105.58.83 - - [03/Sep/2018:03:18:23 +0800] "GET /zw/ztzl/hbdc/ HTTP/1.1" 200 100183 "www.test2.com" "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
202.105.58.83 - - [03/Sep/2018:03:18:29 +0800] "GET /zw/ztzl/bszpyc/ HTTP/1.1" 200 11582 "www.test2.com" "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
202.105.58.83 - - [03/Sep/2018:03:18:29 +0800] "GET /sy/ztzl/yyssyd/ HTTP/1.1" 200 11242 "www.test2.com" "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
202.105.58.83 - - [03/Sep/2018:03:18:29 +0800] "GET /zw/ggzyjy/ HTTP/1.1" 200 98161 "www.test2.com" "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"

----------------------------------------------------------------分割线---------------------------------------------------------------------------------------------
①运行后出现错误,提示:'findstr' 不是内部或外部命令,也不是可运行的程序或批处理文件。for /f放在第二行就不会出现这个错误,应该是set path引起,但不知道怎么解决
②test.log并不是固定的文件,应该是access_Y%M%D%_log 按照日期每天生成的一个日志文件,这个地方该如何修改?
因为对批处理刚接触,很多东西搞不明白,请大神指教!小女子先谢过了!
作者: flashercs    时间: 2018-10-10 06:25

不要用系统变量 path 命名自定义变量。
  1. @echo off
  2. set "year=%date:~0,4%"
  3. set "month=%date:~5,2%"
  4. set "day=%date:~8,2%"
  5. set "folder=E:\log\%year%\%year%-%month%\%date:~2,2%-%month%-%day%"
  6. if exist "%folder%" (
  7.         if exist "%folder%\" (
  8.                  exit /b
  9.         ) else (
  10.                 ren "%folder%" "%date:~2,2%-%month%-%day%-BAK"
  11.         )         
  12. )
  13. mkdir "%folder%"
  14. REM logfile=access_2018%10%10%_log
  15. set "logfile=access_%year%%%%month%%%%day%%%_log"
  16. for /f "delims=" %%a in ('type list.txt') do findstr /l "%%~a" "%logfile%">>"%folder%\%%a.log"
  17. pause
  18. exit /b
复制代码

作者: memexiaolv    时间: 2018-10-10 08:41

回复 2# flashercs


   
十分感谢!刚接触批处理,都不知道pacth是系统变量,问题解决,感谢!




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2