【发布时间】:2011-03-12 01:28:01
【问题描述】:
我有以下代码(从网络空间中一个有帮助的灵魂操纵了一些代码)。 它的目的是将所有 .csv 文件压缩到一个目录中,并用当前时间戳命名 zip 文件。
我的问题 - 我已经设置了一个计划任务来每天执行以下代码。如您所见,每次执行代码时,我都会返回一天。意思是,用昨天的“一天”压缩今天的文件。当它在每个月的 1 日运行时,我显然遇到了问题。它将抓住昨天的一天(这很好),但现在的月份。如何操作代码以检查昨天是否是本月的最后一天并为文件名输入正确的时间戳?
非常感谢任何帮助。
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( "Shell.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
【问题讨论】:
-
@TiNS: 用类似的东西尝试了stackoverflow并被定向到超级用户:)
-
好吧,让他们在 SO 上解释一下为什么它更适合 SU。
-
要正确格式化一段代码,除了代码内的缩进外,整个东西还需要缩进4个空格。