主頁 > 知識庫 > 使用vbs腳本定時刪除N天前的文件

使用vbs腳本定時刪除N天前的文件

熱門標(biāo)簽:服務(wù)器配置 檢查注冊表項(xiàng) 鐵路電話系統(tǒng) 網(wǎng)站文章發(fā)布 智能手機(jī) 呼叫中心市場需求 銀行業(yè)務(wù) 美圖手機(jī)

腳本放在定時任務(wù)里代替服務(wù)定時執(zhí)行一些操作比較方便,下面是實(shí)現(xiàn)刪除文件夾下N天前創(chuàng)建的文件的vbs腳本,在配置文件 DelFolderList.txt 中配置要刪除的文件路徑,ONLY-DEL-FILES 下的路徑 是只刪除其下的文件,不刪除其內(nèi)的子目錄的文件。DEL-FOLDER-FILES下的路徑 是刪除其內(nèi)文件及其子目錄內(nèi)的文件,子目錄為空時刪除目錄,刪除的文件list 放在log文件夾內(nèi)。例配置文件DelFolder.txt 內(nèi)容如下:

ONLY-DEL-FILES
E:\Code\test
DEL-FOLDER-FILES
E:\Code\subfolder

腳本內(nèi)容:

復(fù)制代碼 代碼如下:

Option Explicit
dim mFSO    '定義文件系統(tǒng)對象
dim wFSO    '定義文件系統(tǒng)對象
DIM filecount '文件計(jì)數(shù)
dim foldercount '文件夾計(jì)數(shù)

dim fstream    '定義讀文件流對象
dim wstream    '定義寫文件流對象

dim folder    '文件夾對象
dim rootfolder    '文件夾對象
dim file    '文件對象
dim Subdirs     '文件夾集合對象
dim subdir        '文件夾對象
dim LoopFolder    '文件夾對象
dim dopath    '路徑字符串

Dim delFlag
delFlag =0  ' 0: 不刪除子目錄   ,1:刪除子目錄

DIM WSH
SET WSH=WSCRIPT.CreateObject("WSCRIPT.SHELL")'擊活WScript.Shell對象
main()

sub main()
dim filename
filecount = 0
foldercount = 0
Set mFSO = CreateObject("Scripting.FileSystemObject")
Set wFSO = CreateObject("Scripting.FileSystemObject")
Set fstream = mFSO.OpenTextFile("DelFolderList.txt", 1)
filename=Cstr(Date)
filename=filename + "_" + Replace(Cstr(Time),":","_")
Set wstream = wFSO.CreateTextFile("log\" filename ".log", TRUE)
'循環(huán)讀取一行filelist.txt中的內(nèi)容
Do While True > fstream.AtEndOfStream
   dopath = fstream.ReadLine
   If dopath = "ONLY-DEL-FILES" Then
      delFlag =0
   If True > fstream.AtEndOfStream Then
   dopath = fstream.ReadLine
   End If
   End If

   If dopath = "DEL-FOLDER-FILES" Then
      delFlag =1
   If True > fstream.AtEndOfStream Then
        dopath = fstream.ReadLine
   End if
   End If

   if mFSO.FolderExists(dopath) Then
       If 1 = delFlag Then ' 刪除子目錄

       set rootfolder=mFSO.GetFolder(dopath)
       wstream.WriteLine(rootfolder.path " 下刪除了以下文件:")
       del_file_subFolder rootfolder
       wstream.WriteLine(rootfolder.path " 下刪除了以下文件夾:")
       del_folderFunc rootfolder
    'del_folderFunc rootfolder
        wstream.WriteBlankLines(1) 
   Else '只刪除文件
     set rootfolder=mFSO.GetFolder(dopath)
        wstream.WriteLine(rootfolder.path " 下刪除了以下文件:")
        del_file rootfolder
        wstream.WriteBlankLines(1)
   End If
   else
        WSH.POPUP("文件夾“" dopath "”不存在或路徑錯誤")
        wstream.WriteLine("文件夾“" dopath "”不存在或路徑錯誤")
    end if
Loop
fstream.Close
    

'WSH.POPUP("共刪除文件" filecount "個、文件夾" foldercount "個")
end sub

sub del_file(folder)
    dim df        '標(biāo)記
    For Each file In folder.files
        df=DateDiff("d",file.DateCreated,Now)
        If (df>1) Then '1天前的文件
            wstream.WriteLine(folder.path "\" file.Name vbTab file.DateCreated)
            file.Delete()
            filecount=filecount+1
        End If
    Next
end Sub

''刪除文件 同時刪除subfolder
sub del_file_subFolder(folder)
    dim df        '標(biāo)記
    For Each file In folder.files
        df=DateDiff("d",file.DateCreated,Now)
        If (df>1) Then
            wstream.WriteLine(folder.path "\" file.Name vbTab file.DateCreated)
            file.Delete()
            filecount=filecount+1
        End If
    Next
    '遞歸調(diào)用del_file函數(shù),實(shí)現(xiàn)子目錄文件的遍歷刪除
    If (0 Folder.SubFolders.Count ) then
        For Each loopfolder in folder.SubFolders
        del_file_subFolder loopfolder
        Next
    End If
end sub


sub del_folderFunc(folder)
    dim loopsub
    dim tmp
    if 0=folder.subfolders.Count Then    '判斷其下是否還有子文件夾,若無
        if 0=folder.files.Count then    '如果其下還有文件則退出(符合條件的文件上一步已刪掉了)
            if Ucase(folder.path)>Ucase(dopath) then    '判斷是否為根目錄,是則退出,不是則刪除
                wstream.WriteLine(folder.path vbTab folder.DateCreated)
                folder.delete
                foldercount=foldercount+1
                 '' del_folderFunc(mFSO.GetFolder(delFolderPath))        '刪除該文件夾后從根目錄重新檢查,以檢查其父目錄是否該刪除
            end if
        End if
    else
        For Each subdir In folder.subfolders    '還有子文件夾則輪循每一個子文件夾
            del_folderFunc(subdir)
        Next
    End if

end sub

您可能感興趣的文章:
  • php定時刪除文件夾下文件(清理緩存文件)
  • 使用mysql事件調(diào)度器定時刪除binlog
  • vbs 定時刪除功能實(shí)現(xiàn)代碼
  • Windows和Linux下定時刪除某天前的文件的腳本
  • Shell定時刪除指定時間之前的文件
  • 定時刪除一個文件夾內(nèi)的所有子文件夾和文件的方法

標(biāo)簽:紅河 新疆 河南 樂山 上海 滄州 沈陽 長治

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用vbs腳本定時刪除N天前的文件》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266