zoukankan      html  css  js  c++  java
  • 计划任务 vbs 不运行 vbs删除目录下文件创建日期大于7天的文件

     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:MysqlDBBakdb1","D:MysqlDBBakdb2") '多个目录在这里添加
    
    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:akak1","E:akak2") '多个绝对路径在这里添加
    '当前目录下的
    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
  • 相关阅读:
    A
    MongoDB小结17
    MongoDB小结16
    金蝶无法生成下推发票
    MongoDB小结15
    MongoDB小结14
    MongoDB小结13
    MongoDB小结12
    MongoDB小结11
    MongoDB小结10
  • 原文地址:https://www.cnblogs.com/q149072205/p/12295495.html
Copyright © 2011-2022 走看看