计划任务 vbs 不运行 vbs删除目录下文件创建日期大于7天的文件

时间:2024-03-06 15:27:42

 windows Server2012计划任务添加运行vbs一直不执行,以下几点需要注意:

1.一定要勾选:使用最高权限运行 否则运行不了

 

 2.设置脚本路径时,一定要设置起始于 为vbs文件所在路径,(本人开始没有设置一直行不执行vbs,设置以后就可以了)

 

 

 

如果计划任务不能运行vbs,在win10下设置任务后右键属性,在常规标签中勾选“使用最高权限运行” 否责支行不了vbs

\'vbs 删除目录下文件创建日期大于7天的文件
dim AllPathFileName \'所有输出的文件路径
dim fs  \'操作文件对象
dim foldername  \'要删除的目录路径数组
dim nowTime
nowTime=Now() \'当前时间
foldername =Array("D:\MysqlDBBak\db1\","D:\MysqlDBBak\db2\") \'多个目录在这里添加

Set fs = CreateObject("scripting.filesystemobject")
For i=0 to UBound(foldername)
    delete(foldername(i))\'调用函数进行查找
Next
\'msgbox AllPathFileName \'结果显示
WriteLineToFile(AllPathFileName) \'日志记录结果
 
\'删除目录下 文件创建日期大于7天 and 扩展名为psc的文件
Function delete(path)
    Set folder = fs.getfolder(path)
    For Each file In folder.Files
        AllPathFileName=AllPathFileName & file.path  \'找到则追加到变量FileName中
        \'AllPathFileName=AllPathFileName& file.name & file.DateCreated&"__"& file.DateLastModified&"__"& file.DateLastAccessed&"__"& DayCount & vbNewLine
        dim DayCount,fileExt
        DayCount=DateDiff("d",file.DateCreated,nowTime)
        fileExt=lcase(Right(file.name,4)) 
        If DayCount>7 and (fileExt=".psc" or fileExt=".psb")  Then
            fs.deleteFile(file.path) \'删除文件            
            AllPathFileName=AllPathFileName&  "__DELETE" 
        else            
            AllPathFileName=AllPathFileName& "__KEEP"             
        End If
        AllPathFileName=AllPathFileName & vbNewLine      
    Next
End Function

\'vbs写日志
Function WriteLineToFile(message)
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim fileSystemObj, fileSpec
    Dim currentTime
    currentTime = Now() 
    Set fileSystemObj =CreateObject("Scripting.FileSystemObject")
    fileSpec = "D:\vbsLog.txt" \'日志文件名 c:\log.txt 也可根据日期生成
    If Not (fileSystemObj.FileExists(filespec)) Then 
        Set logFile = fileSystemObj.CreateTextFile(fileSpec, ForWriting, True) 
        logFile.WriteLine ("#######################################################################") 
        logFile.WriteLine (currentTime & " : Begin Log. " ) 
        logFile.WriteLine ("#######################################################################") 
        logFile.Close 
        Set logFile = Nothing
    End If

    Set logFile = fileSystemObj.OpenTextFile(fileSpec, ForAppending, False, True)    
    logFile.WriteLine ("==============="&currentTime&"==========================================")
    \'logFile.WriteLine (currentTime & " : ")
    logFile.WriteLine (message)
    logFile.Close
    Set logFile = Nothing
    
    Set fileSystemObj = Nothing

End Function

 

以下是改进了一下,判断目录是否存在

\'vbs 删除目录下文件创建日期大于7天的文件
dim AllPathFileName \'所有输出的文件路径
dim fs  \'操作文件对象
dim foldername  \'要删除的目录路径(绝对路径)
dim foldername2  \'要删除的当前路径下的 文件夹名
dim currentpath \'当前路径(不用)
dim nowTime
nowTime=Now() \'当前时间
\'获取当前路径
currentpath  = createobject("Scripting.FileSystemObject").GetFolder(".").Path
\'指定拒绝路径
foldername =Array("E:\bak\bak1\","E:\bak\bak2\") \'多个绝对路径在这里添加
\'当前目录下的
foldername2 =Array("\path1\","\path2\") \'多个目录名称

Set fs = CreateObject("scripting.filesystemobject")
For i=0 to UBound(foldername)
    delete(foldername(i))\'调用函数进行查找
Next
For i=0 to UBound(foldername2)
    delete(currentpath&foldername2(i))\'调用函数进行查找
Next
\'msgbox AllPathFileName \'结果显示
WriteLineToFile(AllPathFileName) \'日志记录结果
 
\'删除目录下的文件
Function delete(path)
    If(fs.FolderExists(path)) Then
        Set folder = fs.getfolder(path)
        For Each file In folder.Files
            AllPathFileName=AllPathFileName & file.path  \'找到则追加到变量FileName中
            \'AllPathFileName=AllPathFileName& file.name & file.DateCreated&"__"& file.DateLastModified&"__"& file.DateLastAccessed&"__"& DayCount & vbNewLine
            dim DayCount,fileExt
            DayCount=DateDiff("d",file.DateCreated,nowTime)
            fileExt=lcase(Right(file.name,4))
            \'删除文件创建日期大于7天 and 扩展名为psc的文件
            If DayCount>7 and (fileExt=".psc" or fileExt=".psb")Then 
                fs.deleteFile(file.path) \'删除文件            
                AllPathFileName=AllPathFileName&  "__DELETE" 
            else            
                AllPathFileName=AllPathFileName& "__KEEP"             
            End If
            AllPathFileName=AllPathFileName & vbNewLine      
        Next
    Else
    AllPathFileName=AllPathFileName &"Path:"&path&" is not Exist"& vbNewLine 
    End if
End Function

\'vbs写日志
Function WriteLineToFile(message)
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim fileSystemObj, fileSpec
    Dim currentTime
    currentTime = Now() 
    Set fileSystemObj =CreateObject("Scripting.FileSystemObject")
    fileSpec = currentpath&"\deleteOldBakLog.txt" \'c:\log.txt 也可根据日期生成
    If Not (fileSystemObj.FileExists(filespec)) Then 
        Set logFile = fileSystemObj.CreateTextFile(fileSpec, ForWriting, True) 
        logFile.WriteLine ("#######################################################################") 
        logFile.WriteLine (currentTime & " : Begin Log. " ) 
        logFile.WriteLine ("#######################################################################") 
        logFile.Close 
        Set logFile = Nothing
    End If

    Set logFile = fileSystemObj.OpenTextFile(fileSpec, ForAppending, False, True)    
    logFile.WriteLine ("==============="&currentTime&"==========================================")
    \'logFile.WriteLine (currentTime & " : ")
    logFile.WriteLine (message)
    logFile.Close
    Set logFile = Nothing
    
    Set fileSystemObj = Nothing

End Function

 再次改进,当文件夹下小于5个文件时不删除

\'vbs 删除目录下文件创建日期大于7天的文件
dim AllPathFileName \'所有输出的文件路径
dim fs  \'操作文件对象
dim foldername  \'要删除的目录路径(绝对路径)
dim foldername2  \'要删除的当前路径下的 文件夹名
dim currentpath \'当前路径(不用)
dim nowTime
nowTime=Now() \'当前时间
\'获取当前路径
currentpath  = createobject("Scripting.FileSystemObject").GetFolder(".").Path
\'指定绝对路径
foldername =Array("E:\test32\test23\") \' 多个绝对路径在这里添加
\'当前目录下的
foldername2 =Array("\path1\","\path2\") \'多个目录名称

\'MsgBox "begin"

Set fs = CreateObject("scripting.filesystemobject")
For i=0 to UBound(foldername)
    delete(foldername(i))\'调用函数进行查找
Next
For i=0 to UBound(foldername2)
    delete(currentpath&foldername2(i))\'调用函数进行查找
Next
\'msgbox AllPathFileName \'结果显示
AllPathFileName=AllPathFileName &  "==============Operate complated"
WriteLineToFile(AllPathFileName) \'日志记录结果

\'MsgBox "end"

\'删除目录下的文件
Function delete(path)
    If(fs.FolderExists(path)) Then
        Set folder = fs.getfolder(path)
    if (getFileCount(folder)>5) Then
            For Each file In folder.Files
            AllPathFileName=AllPathFileName & file.path  \'找到则追加到变量FileName中
                \'AllPathFileName=AllPathFileName& file.name & file.DateCreated&"__"& file.DateLastModified&"__"& file.DateLastAccessed&"__"& DayCount & vbNewLine
                dim DayCount,fileExt
                DayCount=DateDiff("d",file.DateCreated,nowTime)
                fileExt=lcase(Right(file.name,4))
        
                \'删除文件创建日期大于5天 and 扩展名为psc的文件
                If DayCount>=5 and ((fileExt=".psc") or (fileExt=".psb") or (fileExt=".nb3")) Then 
                    fs.deleteFile(file.path) \'删除文件            
                    AllPathFileName=AllPathFileName&  "__DELETE" 
                else            
                    AllPathFileName=AllPathFileName& "__KEEP"             
                End If
                AllPathFileName=AllPathFileName & vbNewLine      
            Next
    else
        AllPathFileName=AllPathFileName &folder.Path&" is low 5 files"& vbNewLine 
    End If
    Else
    AllPathFileName=AllPathFileName &"Path:"&path&" is not Exist"& vbNewLine 
    End if
End Function

\'得到目录下文件数量(不包含目录)
Function getFileCount(folder) 
    c=0
    for each f in folder.Files
        c=c+1
    next
    getFileCount=c
End Function

\'vbs写日志
Function WriteLineToFile(message)
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim fileSystemObj, fileSpec
    Dim currentTime
    currentTime = Now() 
    Set fileSystemObj =CreateObject("Scripting.FileSystemObject")
    fileSpec = currentpath&"\deleteOldBakLog.txt" \'c:\log.txt 也可根据日期生成
    If Not (fileSystemObj.FileExists(filespec)) Then 
        Set logFile = fileSystemObj.CreateTextFile(fileSpec, ForWriting, True) 
        logFile.WriteLine ("#######################################################################") 
        logFile.WriteLine (currentTime & " : Begin Log. " ) 
        logFile.WriteLine ("#######################################################################") 
        logFile.Close 
        Set logFile = Nothing
    End If

    Set logFile = fileSystemObj.OpenTextFile(fileSpec, ForAppending, False, True)    
    logFile.WriteLine ("==============="&currentTime&"==========================================")
    \'logFile.WriteLine (currentTime & " : ")
    logFile.WriteLine (message)
    logFile.Close
    Set logFile = Nothing
    
    Set fileSystemObj = Nothing

End Function