Comparing creation dates of files in VBScript

谁说胖子不能爱 提交于 2019-12-24 14:29:46

问题


This may be very obvious to someone out there but I'm having a lot of trouble trying to solve a bug in VBScript. Within the script, I am running through a bunch of .zip files in a directory and processing ones whose creation date is within a specified range.

For instance, if the user enters two arguments 9/3/2014 and 9/5/2014, I only want to process zip files within that date range.

Here is the if statement I am using:

 If Mid(file.NAME,len(file.NAME)-3,4) = ".zip" AND 
 FormatDateTime(file.DateCreated, 2) >= Wscript.Arguments(1) AND 
 FormatDateTime(file.DateCreated, 2) <= Wscript.Arguments(2) then

I am using the FormatDateTime function to remove the times from the file creation date. That way I should just be left with a short date (mm/dd/yyyy).

The problem I am having is that I am processing dates outside of the given range. For example if the given range is 9/3/2014 to 9/5/2014 then I also end up processing 9/30/2014 for some reason. Can anyone help solve this?


回答1:


Both the return value of FormatDateTime() and the items of .Argments are Strings. A string comparison of (stringyfied) numbers will give inconvenient results:

>> WScript.Echo CStr(5 < 30)
>> WScript.Echo CStr("5" < "30")
>>
True
False

Use CDate() to convert the .Arguments to Dates and DateDiff() to compare them against the .DateCreated.




回答2:


Found the source of my problem. FormatDateTime returns a string. Furthermore, the arguments I was being passed were strings also. This means I was actually doing a string comparison instead of a date comparison. The if statement should be:

 If Mid(file.NAME,len(file.NAME)-3,4) = ".zip" AND 
 CDate(FormatDateTime(file.DateCreated, 2)) >= CDate(Wscript.Arguments(1)) AND 
 CDate(FormatDateTime(file.DateCreated, 2)) <= CDate(Wscript.Arguments(2)) then


来源:https://stackoverflow.com/questions/28054053/comparing-creation-dates-of-files-in-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!