• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

bat脚本、vbs脚本和perl脚本删除日志文件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

脚本这个东东还是非常实用的,可以简化手动操作。下面这个脚本是删除前七天的日志文件。

bat脚本:

@echo off
echo ------------Delete log file,develop by terry-------------
echo Today date::%date%
::Year
set yy=%date:~0,4%
::Month
set mms=%date:~5,2%
if %mms% GTR 10 (set mm=%mms%) else (set mm=%mms:~1,2%)
::Day
set dds=%date:~8,2%
if %dds% GTR 10 (set dd=%dds%) else (set dd=%dds:~1,2%)
::Caluation
set /a dt=%yy%*365+%mm%*31+%dd%

echo Begin to delete the log file before seven days...
echo ----------------------------------------------------------
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%i in ('dir /s/b "D:\Exception\*txt"') do (        
    set ff=%%~ti    
    ::Year    
    set yf=!ff:~0,4!
    ::Month
    set mfs=!ff:~5,2!    
    if !mfs! GTR 10 (set mf=!mfs!) else (set mf=!mfs:~1,2!)
    ::Day
    set dfs=!ff:~8,2!
    if !dfs! GTR 10 (set df=!dfs!) else (set df=!dfs:~1,2!)
    ::Caluation
    set /a ft=!yf!*365+!mf!*31+!df!
    set /a res=%dt%-!ft!
    ::Delete log file
    if !res! GTR 7 (echo Delete log file %%i)&&(del %%i /f /q)
)
Endlocal
echo ----------------------------------------------------------
echo Delete Completed.
pause

 

vbs脚本:

Dim fso, startFolder, OlderThanDate
Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = "D:\Exception\" 'File Path
OlderThanDate = DateAdd("d",-7, Now)  'Delete files before sevens day,you can change the span day what you want


Sub DeleteOldFiles(folderName, BeforeDate)
   Dim folder, file, fileCollection, folderCollection, subFolder
   Set folder = fso.GetFolder(folderName)
   Set fileCollection = folder.Files
   For Each file In fileCollection
      If file.DateLastModified < BeforeDate Then   
         fso.DeleteFile(file.Path)
      End If
   Next
    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
       DeleteOldFiles subFolder.Path, BeforeDate 
    Next
End Sub

'Use function
DeleteOldFiles startFolder, OlderThanDate

 

perl脚本:

#!/usr/bin/perl
use strict;
use warnings;
 
my $path = "D:/Exception";    #path
my $filecount = 0;            #delete file count
my $timeday=7*24*60*60;     #delete files before senven days
my $timenow=time();         #timenow

sub parse_env {
     my $path = $_[0];
     my $subpath;
     my $handle;

     if (-d $path) {
         if (opendir($handle, $path)) {
             while ($subpath = readdir($handle)) {
                 if (!($subpath =~ m/^\.$/) and !($subpath =~ m/^(\.\.)$/)) {
                     my $p = $path."/$subpath";
                     
                     my $filetime = (stat($p))[9];      #file modify time
                     my $timespan=$timenow-$filetime;    #span time
    
                     if (-d $p) {
                         parse_env($p);
             rmdir $p; }
elsif ($timespan > $timeday) { ++$filecount; print "Delete the file:".$p."\n"; unlink($p) or warn "failed on $subpath:$!"; } } } closedir($handle); } } return $filecount; } #use function parse_env my $count = parse_env $path; my $str = "Delete files count:".$count; print $str;

 

 

关注
 
 
 
 
 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
对数据访问层的重构(及重构中Perl的应用)发布时间:2022-07-22
下一篇:
[转] perl的LWP模块介绍发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap