WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DllCall Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

NTFS File Time Stamps

 Keywords: GetFileTime API DLLCALL file time stamp 

Question:

I know NTFS file time stamps reflect Daylight Savings (changing the time stamp in Oct & April). MS recommends using the GetFileTime() API call to get the UTC time stamp of the file, is there a way in WB to get this stamp? All WB functions I tried seem to return the locally adjusted stamp, which causes comparison issues since I stored the TimeDate in a file (Oct 27 the same files appear different due to the hr offset).

Answer:

Here is an UDF that makes a dllcall to the GetFileTime API, you were referring to.
#DefineFunction GetFileTime(file,request)
	;BOOL GetFileTime(
	;  HANDLE hFile,
	;  LPFILETIME lpCreationTime = 1
	;  LPFILETIME lpLastAccessTime = 2
	;  LPFILETIME lpLastWriteTime = 3
	;)
	hdl = FileOpen(file,"READ")

	Kernel32 = DllLoad(strcat(dirwindows(1),"Kernel32.DLL"))
	lpCreationTime = BinaryAlloc(8)
	lpLastAccessTime = BinaryAlloc(8)
	lpLastWriteTime = BinaryAlloc(8)
			ret = DllCall(Kernel32,long:"GetFileTime",long:hdl,lpbinary:lpCreationTime,lpbinary:lpLastAccessTime,lpbinary:lpLastWriteTime)
	if ret == 0
	    Message("GetFileTime Error",DllLastError())
		 BinaryFree(lpCreationTime)
		 BinaryFree(lpLastAccessTime)
	    BinaryFree(lpLastWriteTime)
	    DllFree(Kernel32)
		 FileClose(hdl)
	    return 0
	endif
	
	BinaryEodSet(lpCreationTime,8)
	BinaryEodSet(lpLastAccessTime,8)
	BinaryEodSet(lpLastWriteTime,8)

	lpdatebuffer = BinaryAlloc(8)
	Switch request
	case 1
   	if BinaryEodget(lpCreationTime) == 0 
			BinaryFree(lpCreationTime)
			BinaryFree(lpLastAccessTime)
			BinaryFree(lpLastWriteTime)
			BinaryFree(lpdatebuffer)
			DllFree(Kernel32)
			FileClose(hdl)
			return ""
		endif
		BinaryPoke4(lpdatebuffer,0,BinaryPeek4(lpCreationTime,0))
		BinaryPoke4(lpdatebuffer,4,BinaryPeek4(lpCreationTime,4))
		break

	case 2
	   if BinaryEodget(lpLastAccessTime) == 0 
			BinaryFree(lpCreationTime)
			BinaryFree(lpLastAccessTime)
			BinaryFree(lpLastWriteTime)
			BinaryFree(lpdatebuffer)
			DllFree(Kernel32)
			FileClose(hdl)
			return ""
		endif
		BinaryPoke4(lpdatebuffer,0,BinaryPeek4(lpLastAccessTime,0))
		BinaryPoke4(lpdatebuffer,4,BinaryPeek4(lpLastAccessTime,4))
		break

	case 3
		if BinaryEodget(lpLastWriteTime) == 0 
			BinaryFree(lpCreationTime)
			BinaryFree(lpLastAccessTime)
			BinaryFree(lpLastWriteTime)
			BinaryFree(lpdatebuffer)
			DllFree(Kernel32)
			FileClose(hdl)
			return ""
		endif
		BinaryPoke4(lpdatebuffer,0,BinaryPeek4(lpLastWriteTime,0))
		BinaryPoke4(lpdatebuffer,4,BinaryPeek4(lpLastWriteTime,4))
		break

	case request    ; default case
		Message("Error", "Invalid request specified")
		 break
	EndSwitch
	
	;BOOL FileTimeToSystemTime(
;	const FILETIME* lpFileTime,
;	LPSYSTEMTIME lpSystemTime)
	lpformateddate = BinaryAlloc(16)

	ret = DllCall(Kernel32,long:"FileTimeToSystemTime",lpbinary:lpdatebuffer,lpbinary:lpformateddate)
	if ret == 0
	    Message("FileTimeToSystemTime Error",DllLastError())
		 BinaryFree(lpCreationTime)
		 BinaryFree(lpLastAccessTime)
	    BinaryFree(lpLastWriteTime)
		 BinaryFree(lpdatebuffer)
		 BinaryFree(lpFormatedDate)
	    DllFree(Kernel32)
		 FileClose(hdl)
	    return 0
	endif


	year = BinaryPeek2(lpformateddate,0)
   month = StrFixLeft(BinaryPeek2(lpformateddate,2),"0",2)
   day = StrFixLeft(BinaryPeek2(lpformateddate,6),"0",2)
   hours = StrFixLeft(BinaryPeek2(lpformateddate,8),"0",2)
   minutes = StrFixLeft(BinaryPeek2(lpformateddate,10),"0",2)
   seconds = StrFixLeft(BinaryPeek2(lpformateddate,12),"0",2)


	ymdhms = StrCat(year,":",month,":",day,":",hours,":",minutes,":",seconds)
	
	BinaryFree(lpCreationTime)
	BinaryFree(lpLastAccessTime)
	BinaryFree(lpLastWriteTime)
	BinaryFree(lpdatebuffer)
	BinaryFree(lpformateddate)
	FileClose(hdl)
	return ymdhms
#EndFunction 

;Main code
file = "C:\temp\test.txt"
ymdhms = GetFileTime(file,1)
Message( "GetFileTime",ymdhms)

Article ID:   W15486
File Created: 2009:04:01:11:19:44
Last Updated: 2009:04:01:11:19:44