自动化测试 日志_自动化删除旧日志文件的过程

自动化测试 日志

自动化测试 日志

Many services and programs out there produce log files as an audit trail for everything they are doing, however few have a function which removes these files as they outlive their usefulness. As a result, these log files sit on your system eating up space (sometimes more than you know) and cluttering directories for those times you need to access them.

那里的许多服务和程序都会将日志文件作为其所做的所有事情的审核记录,但是很少有功能可以在这些文件和文件不再有效时将其删除。 结果,这些日志文件位于您的系统上,占用了空间(有时比您知道的更多),并且在您需要访问它们的那些时间里目录混乱。

So if you don’t need these files, why keep them? We are going to show you how to easily remove these old log files to keep you system nice and tidy.

因此,如果您不需要这些文件,为什么要保留它们? 我们将向您展示如何轻松删除这些旧日志文件,以使您的系统保持整洁。

Of course, while the we are covering below are immediately useful for managing log files, you can also apply the same techniques to any other type of “expiring” file (such as backups).

当然,尽管下面要介绍的内容对于管理日志文件非常有用,但您也可以将相同的技术应用于任何其他类型的“到期”文件(例如备份)。

根据上次修改日期删除文件 (Remove Files Based on Last Modified Date)

If you want to clear your existing log files based solely on the last modified date of the file, all you have to do is use the FORFILES command. For example:

如果要仅基于文件的最后修改日期来清除现有日志文件,则只需使用FORFILES命令。 例如:

FORFILES /P “C:LogFiles” /S /D -7 /C “CMD /C DEL /F /Q @PATH”

文件/ P“ C:LogFiles” / S / D -7 / C“ CMD / C DEL / F / Q @PATH”

The above command would delete all files from the “C:LogFiles” folder, and all sub-folders which have not been modified in the last week.

上面的命令将从“ C:LogFiles”文件夹中删除所有文件,以及上周未修改的所有子文件夹。

The FORFILES command is pretty flexible with the search pattern and date functions. For example, in place of a number, you can enter a date such as ‘-1/13/2010’ to delete files last modified prior to the specified date.

FORFILES命令与搜索模式和日期函数非常灵活。 例如,代替数字,您可以输入一个日期,例如'-1/13/2010',以删除在指定日期之前最后修改的文件。

To get all the details on what FORFILES can do, view the online help using the following command from the command prompt:

要获取有关FORFILES可以执行的所有详细信息,请在命令提示符下使用以下命令查看联机帮助:

FORFILES /?

档案/?

根据文件名中的日期模式删除文件 (Remove Files Based on a Date Pattern in the File Name)

Many applications and services produce log files based on a date pattern as to have one log file per day (i.e. Log100113.txt, Backup-2010-01-13.zip, etc.). For these types of files, it is preferable to delete based on the date of the file incorporated into the file name rather than the last modified date. This is useful for scenarios such as keeping all log files for the past 3 months. Unfortunately, Windows does not have a native command with this type of logic but with a batch script we can easily handle this task.

许多应用程序和服务都基于日期模式生成日志文件,以便每天有一个日志文件(即Log100113.txt,Backup-2010-01-13.zip等)。 对于这些类型的文件,最好根据合并到文件名中的文件的日期而不是上次修改的日期来删除。 这对于过去三个月保留所有日志文件的情况很有用。 不幸的是,Windows没有具有这种逻辑类型的本机命令,但是通过批处理脚本,我们可以轻松地完成此任务。

There are examples included in the usage comments on the script, so it should be pretty easy to figure out.

脚本的用法注释中包含一些示例,因此应该很容易弄清楚。

剧本 (The Script)

@ECHO OFF
ECHO Delete By Date Pattern
ECHO Written by: Jason Faulkner
ECHO SysadminGeek.com
ECHO.
ECHO.

REM Delete/Select files based on a date which utilizes MM and/or DD for file naming patterns.
REM
REM Usage:
REM DeleteByDatePattern {/M | /D} NumberToKeep Path PatternPrefix PatternPostfix [/L | /DEL]
REM    /M     Specifies the pattern being used is based on months.
REM    /D     Specifies the pattern being used is based on days.
REM    NumberToKeep
REM           The number of months (/M) or days (/D) to keep, including the current.
REM           For example, entering 1 keeps only the current month/day and 6 would keep the current minus 5.
REM    Path   The root location to search. Subdirectories will be searched.
REM    PatternPrefix
REM           The file search pattern placed before of the month/day when building the search string.
REM    PatternPostfix
REM           The file search pattern placed after of the month/day when building the search string.
REM    /L     (optional) Lists all files matching the pattern, but does not delete them.
REM    /DEL   (optional) Deletes all files matching the pattern.
REM
REM Examples:
REM    DeleteByDatePattern /M 3 "%WinDir%system32LogFiles" ex?? ??.log /DEL
REM       Deletes all IIS log files (Windows Server 2003) except for the current and previous two months.
REM    DeleteByDatePattern /D 7 "D:Backup" *-????-??- .zip /DEL
REM       Deletes all zip files from the D:Backup folder except for the current week.
REM       The file name pattern assumed above is "*-YYYY-MM-DD.zip"
REM    DeleteByDatePattern /M 0 "C:" *( )* /L
REM       Prints a list of all files on the C drive matching the pattern: "*-MM-*" (where MM is replaced with 01-12)
REM    DeleteByDatePattern /D 14 "C:Logs" Log-???? .txt
REM       Prints a list of all patterns which would be processed by the script.

SETLOCAL EnableExtensions EnableDelayedExpansion

REM Assumes your Windows Date/Time settings are set to 'DayOfWeek M/D/YYYY' format.
REM If your format is different, you will need to alter the variables below so they align.
FOR /F "tokens=1,2,3,4 delims=/ " %%A IN ('DATE /T') DO (
   SET Month=%%B
   SET Day=%%C
   SET Year=%%D
)

IF /I {%1}=={/M} (
   SET Keep=%Month%
   SET Max=12
)
IF /I {%1}=={/D} (
   SET Keep=%Day%
   SET Max=31
   REM Working off of the previous month's max days.
   SET /A PrevMonth=%Month%-1
   IF !PrevMonth! EQU 2 (
      SET Max=28
      REM Leap years... add more as needed.
      IF /I %Year% EQU 2012 SET Max=29
      IF /I %Year% EQU 2016 SET Max=29
   )
   IF /I !PrevMonth! EQU 4 SET Max=30
   IF /I !PrevMonth! EQU 6 SET Max=30
   IF /I !PrevMonth! EQU 9 SET Max=30
   IF /I !PrevMonth! EQU 11 SET Max=30
)
SET Current=%Keep%
SET /A Keep=%Keep%-%2+1

REM Determine the range to be removed.
SET /A RemoveHighStart=%Current%+1
IF /I %Keep% LSS 1 (
   SET RemoveLow=0
   SET /A RemoveHighEnd=%Keep%+%Max%-1
) ELSE (
   SET /A RemoveLow=%Keep%-1
   SET RemoveHighEnd=%Max%
)

REM Process all less than the low range.
FOR /L %%Z IN (1,1,%RemoveLow%) DO CALL :Process %%Z %3 %4 %5 %6
REM Process all greater than the high range.
FOR /L %%Z IN (%RemoveHighStart%,1,%RemoveHighEnd%) DO CALL :Process %%Z %3 %4 %5 %6

ENDLOCAL
GOTO End

:Process
SET Key=0%1
SET Key=%Key:~-2%
SET Target="%~2%~3%Key%%~4"
ECHO Target Pattern: %Target%

IF /I {%5}=={/L} DIR %Target% /B /S
IF /I {%5}=={/DEL} DEL /F /S /Q %Target%
GOTO End

:End

流程自动化 (Automating the Process)

The FORFILES command is native to Windows, however the DeleteByDatePattern script should be placed in a folder defined in your Path variable (such as your Windows folder) so it can be called as though it were a native command. Once this is done, you can create a scheduled task which is either a single command (if you only need to delete from a single location) or a batch file (if you need to delete from multiple locations) which runs daily, weekly, monthly or whenever.

FORFILES命令是Windows的本机,但是DeleteByDatePattern脚本应放在Path变量中定义的文件夹中(例如Windows文件夹),以便可以像调用本机命令一样调用它。 完成此操作后,您可以创建一个计划的任务,该任务可以是每天,每周,每月运行的单个命令(如果只需要从一个位置删除)或批处理文件(如果需要从多个位置删除)。或任何时候。

One more thing you can set and forget.

您可以设置并忘记的另一件事。

链接 (Links)

Download Delete By Date Pattern script from Sysadmin Geek

从Sysadmin Geek下载按日期删除模式脚本

翻译自: https://www.howtogeek.com/50528/automating-the-process-of-deleting-old-log-files/

自动化测试 日志

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值