Solved

Date Arithmetic example Powershell and VB Script

Posted on 2010-09-01
9
952 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:Westez
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 13

Assisted Solution

by:soostibi
soostibi earned 125 total points
ID: 33580627
I'd rather do the following. Let's assume, that the $date variable contains the date that you would like to check:

$date = [datetime] "2010.09.01"

Then the '$date is today' check is this:
if(((get-date) - $date).days -eq 0){"this is today"}

$date is not today is this:
if(((get-date) - $date).days -ne 0){"this is not today"}
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 125 total points
ID: 33580662
Fairly easy in VBS, you can do things like:

d=CDate(#4/22/10#)
If d = Now() Then
  wscript.echo "Equal"
Else
  wscript.echo "Not Equal"
End If

You also might want to look at the DateDiff() function, it can be very useful.  A nice consice reference here:

http://www.w3schools.com/vbscript/vbscript_ref_functions.asp#date

~bp
0
 
LVL 6

Assisted Solution

by:rwskas
rwskas earned 125 total points
ID: 33580791
Just an example.. This would be similar to how you would say delete files older than x - using only 1 if statement ofcourse
$Today = Get-Date

$Minus15Days = (Get-Date).AddDays(-15)

$Minus45Minutes = (Get-Date).AddMinutes(-45)

$Files = Get-ChildItem C:\Temp



Foreach ($File in $Files) {

$Timestamp = $File.LastWriteTime

	If ($TimeStamp -le $Today) { "$File is older that today" }

		If ($TimeStamp -le $Minus15Days) { "$File is older than 15 days" }

			if ($Timestamp -le $Minus45Minutes) { "$File is older than 45 Minutes"}

}

Open in new window

0
 

Author Comment

by:Westez
ID: 33583590
First let me say thanks for helping me out.

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 is older than today
iis_log_manage_example.txt is older than 45 Minutes
iis_log_manage_example2.txt is older than today
iis_log_manage_example2.txt is older than 45 Minutes
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 is older than today
iis_log_manage_example.txt is older than 15 days
iis_log_manage_example.txt is older than 45 Minutes
iis_log_manage_example2.txt is older than today
iis_log_manage_example2.txt is older than 15 days
iis_log_manage_example2.txt is older than 45 Minutes
shop.ps1 is older than today
shop.ps1 is older than 15 days
shop.ps1 is older than 45 Minutes
PS C:\scripts>
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 70

Expert Comment

by:Chris Dent
ID: 33584368

> 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
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 125 total points
ID: 33584419

Minor edit, the date above should be changed to todays day, not yesterdays :)

A more comprehensive example below.

Chris
' This is already a date
Date_1 = #09/02/2010#
' This includes Time
Date_2 = Now()
' This does not include Time
Date_3 = Date()

' Test to see if these are equal
If Date_1 = Date_2 Then
  Date_1_and_2 = "Equal"
Else
  Date_1_and_2 = "Not Equal"
End If

' Test to see if these are equal
If Date_1 = Date_3 Then
  Date_1_and_3 = "Equal"
Else
  Date_1_and_3 = "Not Equal"
End If

WScript.Echo "Date 1: " & Date_1 & vbCrLf & _
  "Date 2: " & Date_2 & vbCrlf & _
  "Date 3: " & Date_3 & VbCrlf & VbCrLf & _
  "Date 1 and 2: " & Date_1_and_2 & VbCrLf & _
  "Date 1 and 3: " & Date_1_and_3 & VbCrlf & VbCrlf & _
  "DateDiff 1 and 2: " & DateDiff("d", Date_1, Date_2) & VbCrLf & _
  "DateDiff 1 and 3: " & DateDiff("d", Date_1, Date_3)

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33585494
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
0
 

Author Comment

by:Westez
ID: 33601005
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.
0
 

Author Closing Comment

by:Westez
ID: 33621112
All - thanks for helping me out here, this is a good start.  I don't find stuff like this when I search the web.  
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Synchronize a new Active Directory domain with an existing Office 365 tenant
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now