Westez
asked on
Date Arithmetic example Powershell and VB Script
I'm asking for examples of date arithmetic using Powershell and VB Script. Just some basic stuff, nothing fancy, I need some help in getting started.
If date ne to today then do something, move a file, delete a file, etc.
If date eq to today then do something, move a file, delete a file, etc.
Thanks
If date ne to today then do something, move a file, delete a file, etc.
If date eq to today then do something, move a file, delete a file, etc.
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> Am I making a mistake with the date?
Yes, you're forgetting time. These are the values you're comparing and they're not equal.
WScript.Echo CDate(#09/01/2010#)
WScript.Echo Now()
Soostibi's version sidesteps issues with the Time by measuring days only from a TimeSpan (what you get back when you take one date away from another). If you want to use the same method in VbScript you need DateDiff:
Date1 = CDate(#09/01/2010#)
Date2 = Now()
WScript.Echo DateDiff("d", Date1, Date2)
Where d tells it to return the difference in Days.
Chris
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, in my example if you just wanted to compare to the data, then it should be:
d=CDate(#4/22/10#)
If d = Date() Then
wscript.echo "Equal"
Else
wscript.echo "Not Equal"
End If
There are two system functions that can be used, Date() returns just the current date, Now() returns the current date and the current time. Sorry for the confusion.
~bp
d=CDate(#4/22/10#)
If d = Date() Then
wscript.echo "Equal"
Else
wscript.echo "Not Equal"
End If
There are two system functions that can be used, Date() returns just the current date, Now() returns the current date and the current time. Sorry for the confusion.
~bp
ASKER
Thanks for details guys. I'm going to pick back up on this later tonight. I need a bit more time to dig into this and work with the code.
ASKER
All - thanks for helping me out here, this is a good start. I don't find stuff like this when I search the web.
ASKER
Soostibi - your code worked straight out.
Bill - I changed the date in the code to today's date and it returns Not Equal. I was expecting Equal. I saved the file with the name date1.vbs. Am I making a mistake with the date?
d=CDate(#09/01/2010#)
If d = Now() Then
wscript.echo "Equal"
Else
wscript.echo "Not Equal"
End If
PS C:\scripts> cscript date1.vbs
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.
Not Equal
rwskas - The files that begin with the word date have today's time stamp on them. The date3.ps1 file, I just copied and pasted your code into notepad and saved it. The date6.ps1 file, I changed the operator from le to ne and got a different result. I was expecting the files with today's time stamp to be excluded from the output after I changed the operator. Would you clue me in as to what's up with the results?
PS C:\scripts> c:\scripts\date3.ps1
date1.vbs is older than today
date1.vbs is older than 45 Minutes
date2.ps1 is older than today
date2.ps1 is older than 45 Minutes
date3.ps1 is older than today
date3.ps1 is older than 45 Minutes
folder permissions.txt is older than today
folder permissions.txt is older than 45 Minutes
folderpermissions.vbs is older than today
folderpermissions.vbs is older than 45 Minutes
iislog2.ps1 is older than today
iislog2.ps1 is older than 45 Minutes
iis_log_manage_example.txt
iis_log_manage_example.txt
iis_log_manage_example2.tx
iis_log_manage_example2.tx
shop.ps1 is older than today
shop.ps1 is older than 45 Minutes
PS C:\scripts> c:\scripts\date6.ps1
date1.vbs is older than today
date1.vbs is older than 15 days
date1.vbs is older than 45 Minutes
date2.ps1 is older than today
date2.ps1 is older than 15 days
date2.ps1 is older than 45 Minutes
date3.ps1 is older than today
date3.ps1 is older than 15 days
date3.ps1 is older than 45 Minutes
folder permissions.txt is older than today
folder permissions.txt is older than 15 days
folder permissions.txt is older than 45 Minutes
folderpermissions.vbs is older than today
folderpermissions.vbs is older than 15 days
folderpermissions.vbs is older than 45 Minutes
iislog2.ps1 is older than today
iislog2.ps1 is older than 15 days
iislog2.ps1 is older than 45 Minutes
iis_log_manage_example.txt
iis_log_manage_example.txt
iis_log_manage_example.txt
iis_log_manage_example2.tx
iis_log_manage_example2.tx
iis_log_manage_example2.tx
shop.ps1 is older than today
shop.ps1 is older than 15 days
shop.ps1 is older than 45 Minutes
PS C:\scripts>