写入vbscript

时间:2015-12-07 11:48:25

标签: vbscript singleton

我写了一个小子来创建日志文件

sub WriteLogFileLine(sLogFileNameFull, sLogFileLine) 
    logfolder = "C:\Users\TEMPPAHIR\LearnVB\Logfolder\"
    ScriptName1 = Replace(Wscript.ScriptName, ".vbs", "")
    sLogFileName = ScriptName1 & "_" & date & "_" & hour(now) & "-" & minute(now) & "-" & second(now) & "_log.txt"
    sLogFileNameFull = logfolder & sLogFileName
    dateStamp = Now()
    Set MyLog = objFSO.OpenTextFile(sLogFileNameFull, 8, True)
    MyLog.WriteLine(cstr(dateStamp) + vbTab + "-" + vbTab + sLogFileLine) 
    MyLog.Close
    Set MyLog = Nothing 
end sub

我在我的主vb脚本函数中多次调用此sub,以这种方式将消息写入日志文件,

'******************************************************************************
'* Main Script Body
'******************************************************************************
Function DoAllWork
    On Error Resume Next
    Dim sFile, FromDate, ToDate, FromLocation, MyFile, objFolder, Filename, objArgs, FilenameReq, line, inFile, outFile, outputLines
    Dim FromdateFile, FromdateFileReq, lineDt, CFileDt, CopyFiles, line1, CFile, resultX, stringY, resultY, resultfinal, outputLine
    Dim FromFolderList, ToLocationArg, outputLines1, Arch, ToLocationArg1, writeOutput
    Const ForReading = 1, ForWriting = 2, ForAppending = 8 
    Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Set objArgs = WScript.Arguments 
    Set MyLog = objFSO.OpenTextFile(sLogFileNameFull, 8, True)

    If WScript.Arguments.Count = 4 Then 
        Call WriteLogFileLine(sLogFileNameFull, "Total number of arguments passed are " & WScript.Arguments.Count & " and they are as follows : ")
        'Parameter1, begin with index0
        FromDate = WScript.Arguments(0)
        'Parameter2
        ToDate = WScript.Arguments(1)
        'Parameter3
        FromLocationArg = WScript.Arguments(2)
        'Parameter4
        ToLocation = WScript.Arguments(3)
    Else 
        Call WriteLogFileLine(sLogFileNameFull, "Error, Must pass 4 arguments to the script !" & vbcrlf)
        Wscript.quit 
    End if 
    Call WriteLogFileLine(sLogFileNameFull, "First Argument, the date folder FROM where files needs to be copied is : " & FromDate & vbcrlf)
    Call WriteLogFileLine(sLogFileNameFull, "Second Argument, the date folder TILL where files needs to be copied is : " & ToDate & vbcrlf)
MyLog.Close
    Set MyLog = Nothing 

    DoAllWork = Err.Number
    Call WriteLogFileLine(sLogFileNameFull, "End of the script !" & vbcrlf)
End Function

'******************************************************************************
'* End of the script.
'******************************************************************************

但是在执行脚本时有时候会生成多个日志文件,我想每次时间戳都在改变时,它会创建一个新的日志文件并在该文件中写入日志。

QUE:我希望将所有日志写入一个日志文件,该脚本的时间戳在我的脚本开头被选中。请帮忙!!

2 个答案:

答案 0 :(得分:0)

您的日志记录功能有一个参数,指向日志文件的完整路径,因此您应该在代码的开头定义一次文件名,并在整个脚本中使用该名称(我建议您执行相同的操作) FileSystemObject实例)。从日志记录功能中删除定义文件名的所有代码。

Set objFSO = CreateObject("Scripting.FileSystemObject")

logfolder    = "C:\Users\TEMPPAHIR\LearnVB\Logfolder"
ScriptName1  = Replace(Wscript.ScriptName, ".vbs", "")
sLogFileName = ScriptName1 & "_" & Date & "_" & Hour(Now) & "-" & _
               Minute(Now) & "-" & Second(Now) & "_log.txt"
logfile = objFSO.BuildPath(logfolder, sLogFileName)

Sub WriteLogFileLine(sLogFileNameFull, sLogFileLine)
    Set MyLog = objFSO.OpenTextFile(sLogFileNameFull, 8, True)
    MyLog.WriteLine Now & vbTab & "-" & vbTab & sLogFileLine
    MyLog.Close
End Sub

...
WriteLogFileLine logfile, "some log line"
...

你可能想看看几年前我写的logger class(如果你原谅无耻的插件)。它可能有助于简化日志处理。将类代码复制到脚本中,您可以像这样进行日志记录:

logfolder = "C:\Users\TEMPPAHIR\LearnVB\Logfolder"
logname   = Replace(Wscript.ScriptName, ".vbs", "") & "_" & Date & "_" & _
            Hour(Now) & "-" & Minute(Now) & "-" & Second(Now) & "_log.txt"
logfile   = objFSO.BuildPath(logfolder, logname)

Set clog = New CLogger
clog.LogToConsole = False
clog.LogFile = logfile      'open log
clog.IncludeTimestamp = True

...
clog.LogInfo "some message"
clog.LogError "other message"
...

clog.LogFile = ""           'close log

当脚本终止时,日志文件会自动关闭(如果之前代码中尚未关闭)。

答案 1 :(得分:0)

我创建了这个来做你所要求的。我使用它来保存日志,因为脚本通过计划任务运行。

precision highp float;
uniform sampler2D inputImageTexture;
varying float kernelColors[33];

void main()
{

    kernelColors[0];
    kernelColors[1];
    kernelColors[2];
    kernelColors[3];
    kernelColors[4];
    kernelColors[5];
    kernelColors[6];
    kernelColors[7];
    kernelColors[8];
    kernelColors[9];
    kernelColors[10];
    kernelColors[11];
    kernelColors[12];
    kernelColors[13];
    kernelColors[14];
    kernelColors[15];
    kernelColors[16];
    kernelColors[17];
    kernelColors[18];
    kernelColors[19];
    kernelColors[20];
    kernelColors[21];
    kernelColors[22];
    kernelColors[23];
    kernelColors[24];
    kernelColors[25];
    kernelColors[26];
    kernelColors[27];
    kernelColors[28];
    kernelColors[29];
    kernelColors[30];
    kernelColors[31];

    kernelColors[32]; //if i comment this line out it works


    gl_FragColor = vec4(vec3(.3,.5,.1), 1.0);



}