none
vbs脚本输出文件名中添加当前时间,精确到秒 RRS feed

  • 问题

  • 有个清理并打包系统日志的脚本,在输出rar文件时,想在文件名上添加当前时间,精确到秒,不用添加冒号,类似下面的文件名

    sername--9-1-2014-100123.rar.

    脚本内容如下,该如何修改?

    --------------------------------------------------------------------------------

    'rar.exe文件存在的目录.
    rarDirectory = "d:\event_log"
    '当前工作目录,vbs所在的目录.
    currentDirectory = "d:\event_log"
    '输出rar的文件目录
    outDirectory = "d:\event_log"

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate, (Backup, Security)}!\\" _
    & strComputer & "\root\cimv2")

    Set objNTInfo = CreateObject("WinNTSystemInfo")
    strComputer = objNTInfo.ComputerName
     
     
    Set colLogFiles_Security = objWMIService.ExecQuery ("Select * from Win32_NTEventLogFile where LogFileName='Security'")
    Set colLogFiles_Application= objWMIService.ExecQuery ("Select * from Win32_NTEventLogFile where LogFileName='Application'")
    Set colLogFiles_System= objWMIService.ExecQuery ("Select * from Win32_NTEventLogFile where LogFileName='System'")

     
    '---------------------------------


    OutputFile_Name = strComputer & "-" & OutputFile & Day(Now) & "-" & month(now)  & "-" & year(now)
     
     
     
    Set FileObject = CreateObject("Scripting.FileSystemObject")



    '**********************************************************************

    'msgbox currentDirectory

     currentDirectory=currentDirectory &"\"& OutputFile_Name

    if  FileObject.FolderExists(currentDirectory)=false then
         set fso=WScript.createObject("Scripting.FileSystemObject")
         fso.createFolder currentDirectory
    End If
     
    '**********************************************************************
    '-------------------------Security------------------------------------
    For Each objLogfile_Security in colLogFiles_Security
    OutputFile = strComputer & "-" & "Security-"
    OutputFile = OutputFile & Day(Now) & "-" & month(now) & "-" & year(now)
    OutputFile = OutputFile & ".evt"

    Security_evt=currentDirectory &"\"& OutputFile
    errBackupLog = objLogfile_Security.BackupEventLog(Security_evt)

    If errBackupLog = 0 Or errBackupLog = 183 Then
    objLogfile_Security.ClearEventLog()
    Else

    Set fso=CreateObject("scripting.FileSystemObject")
    OutputFile_text = strComputer & "-" & "Security-"
    OutputFile_text = OutputFile_text & Day(Now) & "-" & month(now) & "-" & year(now)
    OutputFile_text = OutputFile_text & ".txt"
    Security_text=currentDirectory &"\"& OutputFile_text
    'msgbox Security_text
    Set f1 = fso.CreateTextFile(Security_text, True)
    f1.Write ("The Security event log could not be backed up")
    f1.Close
    End If

    Next
    '--------------------------Application-----------------------------------
    For Each objLogfile_Application in colLogFiles_Application
    OutputFile = strComputer & "-" & "Application-"
    OutputFile = OutputFile & Day(Now) & "-" & month(now) & "-" & year(now)
    OutputFile = OutputFile & ".evt"


    Application_evt=currentDirectory &"\"& OutputFile
    errBackupLog = objLogfile_Application.BackupEventLog(Application_evt)

    If errBackupLog = 0 Or errBackupLog = 183 Then
    objLogfile_Application.ClearEventLog()
    Else

    Set fso=CreateObject("scripting.FileSystemObject")
    OutputFile_text = strComputer & "-" & "Application-"
    OutputFile_text = OutputFile_text & Day(Now) & "-" & month(now) & "-" & year(now)
    OutputFile_text = OutputFile_text & ".txt"
    Application_text=currentDirectory &"\"& OutputFile_text
    Set f1 = fso.CreateTextFile(Application_text, True)
    f1.Write ("The Application event log could not be backed up")
    f1.Close
    End If

    Next

    '--------------------------System-----------------------------------
    For Each objLogfile_System in colLogFiles_System
    OutputFile = strComputer & "-" & "System-"
    OutputFile = OutputFile & Day(Now) & "-" & month(now) & "-" & year(now)
    OutputFile = OutputFile & ".evt"
    System_evt=currentDirectory &"\"& OutputFile
    errBackupLog = objLogfile_System.BackupEventLog(System_evt)

    If errBackupLog = 0 Or errBackupLog = 183 Then
    objLogfile_System.ClearEventLog()
    Else
    Set fso=CreateObject("scripting.FileSystemObject")
    OutputFile_text = strComputer & "-" & "System-"
    OutputFile_text = OutputFile_text & Day(Now) & "-" & month(now) & "-" & year(now)
    OutputFile_text = OutputFile_text & ".txt"
    System_text=currentDirectory &"\"& OutputFile_text

    Set f1 = fso.CreateTextFile(System_text, True)
    f1.Write ("The System event log could not be backed up")
    f1.Close
    End If

    Next
    '-------------------------RAR-------------------------------------------------------
    Set WshShell = WScript.CreateObject("WScript.Shell")
    rar_parameter= rarDirectory & "\rar.exe -df a "& outDirectory &"/" &OutputFile_Name & ".rar " & currentDirectory &"\*"
    'msgbox rar_parameter
    exCode = WshShell.Run (rar_parameter,2,True)
    '------------------------------DeleteFolder--------------------------------------

    Set DelObject = CreateObject("Scripting.FileSystemObject")
        rar_file=outDirectory & "\" & OutputFile_Name & ".rar "
    if  DelObject.FileExists(rar_file)=true then
        DelObject.DeleteFolder(currentDirectory)
    End If

    2014年1月9日 2:02

全部回复

  • 你好,

    请尝试用下面的文件命名行:

    OutputFile_Name = strComputer & "-" & OutputFile &Second(Now)&Minute(Now)& Day(Now) & "-" & month(now)  & "-" & year(now)

    另外有关脚本的问题,我们可以发帖到脚本论坛去问。


    Regards, Yan Li

    2014年1月15日 2:43
    版主
  • 多谢。另外一个问题,小时怎么加,用什么命令,我试过Hour(Now) ,但是脚本运行出错了。

    HongKun

    2014年1月15日 3:29