bat指令碼、vbs指令碼和perl指令碼刪除記錄檔

來源:互聯網
上載者:User

指令碼這個東東還是非常實用的,可以簡化手動操作。下面這個指令碼是刪除前七天的記錄檔。

bat指令碼:

@echo offecho ------------Delete log file,develop by terry-------------echo Today date::%date%::Yearset yy=%date:~0,4%::Monthset mms=%date:~5,2%if %mms% GTR 10 (set mm=%mms%) else (set mm=%mms:~1,2%)::Dayset dds=%date:~8,2%if %dds% GTR 10 (set dd=%dds%) else (set dd=%dds:~1,2%)::Caluationset /a dt=%yy%*365+%mm%*31+%dd%echo Begin to delete the log file before seven days...echo ----------------------------------------------------------setlocal enabledelayedexpansionfor /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))Endlocalecho ----------------------------------------------------------echo Delete Completed.pause

 

vbs指令碼:

Dim fso, startFolder, OlderThanDateSet fso = CreateObject("Scripting.FileSystemObject")startFolder = "D:\Exception\" 'File PathOlderThanDate = DateAdd("d",-7, Now)  'Delete files before sevens day,you can change the span day what you wantSub 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     NextEnd Sub'Use functionDeleteOldFiles startFolder, OlderThanDate

 

perl指令碼:

#!/usr/bin/perluse strict;use warnings; my $path = "D:/Exception";    #pathmy $filecount = 0;            #delete file countmy $timeday=7*24*60*60;     #delete files before senven daysmy $timenow=time();         #timenowsub 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_envmy $count = parse_env $path;my $str = "Delete files count:".$count;print $str;

 

 

作者: ForEvErNoME
出處: http://www.cnblogs.com/ForEvErNoME/
歡迎轉載或分享,但請務必聲明文章出處。如果文章對您有協助,希望你能 推薦 或 關注

     
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.