[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 批处理如何让输出的信息始终保持在文件第一行(首行)?

一直困扰的问题,如何在使用重定向功能生成的Log日志,最新的保持在第一行。请教有何方法,并保持日志文件的属性如创建时间。
  1. ::
  2. :: =================================================
  3. ::  Check the physical harddisk on HP DL388 and X64
  4. ::  in case any error or fail
  5. ::  Need to run with HP Array Configuration Utility CLI
  6. :: =================================================
  7. ::
  8. @echo off
  9. "C:\Program Files (x86)\Compaq\Hpacucli\bin\hpacucli.exe" ctrl slot=0 pd all show > zPdfailinfo.log
  10. find "Fail" zPdfailinfo.log
  11. if %ERRORLEVEL% == 0 (goto zNotify)
  12. find "Error" zPdfailinfo.log
  13. if %ERRORLEVEL% == 0 (goto zNotify) else (goto zExit)
  14. :zExit
  15. @ECHO.Running at %date% %time:~0,5% with no error >> zHDcheck.log
  16. exit /B
  17. :zNotify
  18. @ECHO.Running at %date% %time:~0,5% with error, pls check >> zHDcheck.log
  19. zSendmail.vbs
复制代码
zHDcheck.log
Running at 2015-10-25 Sun  9:35 with no error
Running at 2015-10-26 Mon  9:35 with no error
Running at 2015-10-27 Tue  9:35 with no error
Running at 2015-10-28 Wed  9:35 with no error

期望:
zHDcheck.log
Running at 2015-10-28 Wed  9:35 with no error
Running at 2015-10-27 Tue  9:35 with no error
Running at 2015-10-26 Mon  9:35 with no error
Running at 2015-10-25 Sun  9:35 with no error

  1. ::
  2. :: =================================================
  3. ::  Check the physical harddisk on HP DL388 and X64
  4. ::  in case any error or fail
  5. ::  Need to run with HP Array Configuration Utility CLI
  6. :: =================================================
  7. ::
  8. @echo off
  9. "C:\Program Files (x86)\Compaq\Hpacucli\bin\hpacucli.exe" ctrl slot=0 pd all show > zPdfailinfo.log
  10. type zHDcheck.log > zHDcheck_temp.log
  11. findstr "Fail Error" zPdfailinfo.log && goto zNotify || goto zExit
  12. :zExit
  13. echo Running at %date% %time:~0,5% with no error > zHDcheck.log
  14. type zHDcheck_temp.log >> zHDcheck.log
  15. exit /b
  16. :zNotify
  17. echo Running at %date% %time:~0,5% with error, pls check > zHDcheck.log
  18. type zHDcheck_temp.log >> zHDcheck.log
  19. zSendmail.vbs
复制代码

TOP

回复 2# DAIC

谢谢楼上回复,提供思路。 type 命令改成 copy,提升了一点效率。
  1. "C:\Program Files (x86)\Compaq\Hpacucli\bin\hpacucli.exe" ctrl slot=0 pd all show > zPdfailinfo.log
  2. copy /y zHDcheck.log zHDcheck_temp.log
  3. findstr "Fail Error" zPdfailinfo.log && goto zNotify || goto zExit
  4. :zExit
  5. echo Running at %date% %time:~0,5% with no error > zHDcheck.log
  6. copy /y zHDcheck.log + zHDcheck_temp.log
  7. del /q "zHDcheck_temp.log"
  8. exit /b
  9. :zNotify
  10. echo Running at %date% %time:~0,5% with error, pls check > zHDcheck.log
  11. copy /y zHDcheck.log + zHDcheck_temp.log
  12. del /q "zHDcheck_temp.log"
  13. zSendmail.vbs
复制代码

TOP

回复 3# merlyn


在你的实际环境里面copy比type快多少?日志文件的体积大概是多少?
请分享一下你的试验数据吧,谢谢。

TOP

回复 4# DAIC

简单测试了几组数据并验证
LOG文件 35KB 运行时间0.50s左右,COPY比TYPE少0.01
LOG文件 1500KB TYPE 运行时间1.1s左右,COPY 0.8-0.9s

总体测试感觉,LOG文件越大,COPY命令比TYPE效率越高。

TOP

返回列表