开发者

VBS scripting question - manipulating dates

I have the following code (manipulated some code from a helpful soul out in cyberspace). What it is meant to do is to zip all .csv files in a directory and name the zip file with the current timestamp.

My problem - I have setup a scheduled task to execute the below code everyday. As you can see, I am going back a day each time the code is executed. Meaning, zipping the file today with yesterday's "day". When it runs on the 1st of every month, I obviously run into a problem. It will grab yesterday's day (which is fine) but the current month. How can I manipulate the code so that it checks if yesterday was the last day of the month and put the correct timestamp for the filename?

Any assistance is greatly appreciated.

strFilepath = "c:\files"
strDestination = "c:\files\completed\"      '"#
strExtension = "csv"

strYear = Year(Now)  
strMonth = Right("0" & Month(Now), 2)  
strDay = Right("0" & Day(Now -1), 2)  
strHour = Right ("0" & Hour(Now), 2)  
strMinute = Right ("0" & Minute (Now), 2)  
strZip = strFilepath & "\" & strYear & strMonth & strDay & strHour & strMinute & ".zip" '"#

Set objFSO = CreateObject("Scripting.FileSystemObject")  
Set objFolder = objFSO.GetFolder(strFilepath)  
For Each objFile in objFolder.Files  
    strFileExt = objFSO.GetExtensionName(objFile.Path)  

        If LCase(strFileExt) = LCase(strExtension) Then
        ZipFile objFile.Path, strZip
    End If
Next

Sub ZipFile(strFileToZip, strArchive)
    Set objFSO = CreateObject("Scripting.FileSystemObject")  

    If Not objFSO.FileExists(strArchive) Then
        Set objTxt = objFSO.CreateTextFile(strArchive)
        objTxt.Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
        objTxt.Close
    End If

    Set objApp = CreateObject( "She开发者_运维知识库ll.Application" )

    intCount = objApp.NameSpace(strArchive).Items.Count + 1

    objApp.NameSpace(strArchive).CopyHere strFileToZip

    Do
        WScript.Sleep 200
        set objNameSpace = objApp.NameSpace(strArchive)

        If Not objNameSpace is nothing Then        
            If objNameSpace.Items.Count = intCount Then
                Exit Do
            End If
        End If
    Loop
End Sub


Just check Day(now) value. It it = 1, then Month = Month -1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜