This post provides two methods of converting the 64-bit Windows filetime structure - the Windows Epoch giving the number of 100 nanosecond intervals since 01/01/1601 UTC.
The first is PowerShell which is relatively easy, compared to the second which is some VBScript that I have used upon occasion, typically when I'm trying to convert 64-bit integers from AD (eg returned from dsquery pwdlastset), or vice versa.
Neither of these scripts are revolutionary, but I haven't come across a simple function to convert between the two in vbs, and I thought I'd include the powershell for comparison.
# ConvertFileTime.ps1
$now = [datetime]::Now
$now
$fileTime = $now.ToFileTime()
$fileTime
[datetime]::FromFileTime($fileTime)
# This then parses the date, determining whether it is a valid date or not (in this case it always will be beacuse it's from datetime, but you could use this to parse other date strings, using your culture)
foreach ($date in [string[]]$now.ToString()) {
write-output $date
$oCulture= [System.Globalization.CultureInfo]"en-AU"
$dtOut = new-object DateTime
[datetime]::TryParse($date, $oCulture, [System.Globalization.DateTimeStyles]::None, [ref]$dtOut)
[datetime]::TryParse($date, [ref]$dtOut)
}
-
' ConvertFileTime.vbs
' VBScript doesn't support 64-bit integers, so it can't handle the number of 100 nanosecond intervals since 01/01/1601
' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting09102002.asp
' Either use ADSI provider and the IADs/IADsLargeInteger object
' LargeIntValue = objLargeInt.HighPart * 2^32 + objLargeInt.LowPart
' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/iadslargeinteger.asp'
' Or WMI, which handles the conversion between 64-bit datetime structure / UTC / and VB var datetime
If Wscript.Arguments.UnNamed.Count > 0 Then
strDateTime = Wscript.Arguments.UnNamed(0)
Set objDateTime = CreateObject("WbemScripting.SWbemDateTime")
If IsDate(strDateTime) Then
Call objDateTime.SetVarDate(strDateTime, False)
wscript.echo objDateTime.GetFileTime
Else
Call objDateTime.SetFileTime(strDateTime, False)
wscript.echo objDateTime.GetVarDate
End If
intReturn = 0
Else
WScript.Echo "Specify a filetime or a date to convert, eg 127076450620627215, or ""11/04/2006 11:17:10 AM"""
intReturn = 2
End If
WScript.Quit(intReturn)
Wayne's World of IT (WWoIT), Copyright 2008 Wayne Martin.
2 comments:
Nice one, cheers! :o)
Thank you so much. You are a lifesaver! This script helped me convert list of 7039 file time dates to readable datetime format.
Post a Comment