VBS 脚本问题 - 操作日期
我有以下代码(从网络空间中一位有用的灵魂那里操纵了一些代码)。 它的目的是压缩目录中的所有 .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
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( "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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需检查当天(现在)值即可。它 = 1,则月 = 月 -1。
Just check Day(now) value. It it = 1, then Month = Month -1.