Page 1 of 1 1
Topic Options
#125774 - 2004-08-28 04:52 AM Get the date a file was last accessed
Cybex Offline
Getting the hang of it
*****

Registered: 2004-07-21
Posts: 81
Loc: Brandon, FL.
Is their a way to have Kix retrieve the last accessed date that is kept by windows for each file? I am trying to build an app that will look into the registry for certain licensed software titles we have and go to the installed path(in case non-standard install) and obtain when the program was last used. If the program has not been used in XX months or weeks the user is prompted to uninstall the software thus freeing up the license or we do it automatically and they can request it again later if they need it. I know their are third party apps that can do this but I was wondering if there was a WMI or UDF that could call up the info into a script as a veriable.
_________________________
Common sense isn't so common.

Top
#125775 - 2004-08-28 04:56 AM Re: Get the date a file was last accessed
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Hello Cybex and Welcome to KORG

We have UDFs for such, you could also have the new version of RoboCopy from Microsoft tell you that as well.

fnGetFileProp() - Uses FSO to retrieve file properties
http://www.kixhelp.com/udfs/udf/83404.htm

Top
#125776 - 2004-08-28 05:05 AM Re: Get the date a file was last accessed
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
You better read the EULA carefully because many licenses do not allow for the programmatic transfer from one computer to another.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#125777 - 2004-08-28 05:28 AM Re: Get the date a file was last accessed
Cybex Offline
Getting the hang of it
*****

Registered: 2004-07-21
Posts: 81
Loc: Brandon, FL.
You guys are quick!!!!!!!!

Thanks, that works perfect. I see it was in the Kixhelp UDF list, they keep hiding stuff in plain sight on me. I swear I looked through there before asking.

I think EULA would be respected in the since that the program on flags it or at best uninstalls it. The reallocation of the software is handled via remote delivery software. The licenses are handled by a different division they just hound us on how many instances we can allow to be out at a time.
_________________________
Common sense isn't so common.

Top
#125778 - 2004-08-28 08:33 PM Re: Get the date a file was last accessed
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Well with the help of Mr_Jooel and some UDF power, here is an example of doing this.

You can get the UDFs from the board, but I'll post the full thing here to make it easier to copy and test.


Code:
Break On

Dim $SO
$SO=SetOption('Explicit','On')

Dim $HKLMAppPaths,$sFile,$LADate,$TimeDiff
$HKLMAppPaths='HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths'
$sFile=Trim(ReadValue($HKLMAppPaths + '\powerpnt.exe',''))
$LADate=Split(Trim(Join(Split(fnGetFileProp($sFile,DateLastAccessed),""),"",1)),"/")
$LADate=$LADate[2]+"/"+Right("0"+$LADate[0],2)+"/"+Right("0"+$LADate[1],2)
$TimeDiff=DateCalc(@DATE,$LADate)
If $TimeDiff > 0 ; change to date you want to test
'The file ' + $sFile + ' has not been accessed in over ' + $TimeDiff + ' days.' ?
'The file ' + $sFile + ' Last Accessed: ' + $LADate + Right(fnGetFileProp($sFile,DateLastAccessed),-9) ?
EndIf

Function fnGetFileProp($sFile,$sProp)
Dim $objFSO, $objFile, $nul
$objFSO = CreateObject("Scripting.FileSystemObject")
If Not VarType($objFSO)=9 Exit 1 EndIf
$objFile = $objFSO.GetFile($sFile)
If Not VarType($objFile)=9 Exit 2 EndIf
$nul=Execute("$$fnGetFileProp = $$objFile."+$sProp)
If VarType($fnGetFileProp)=0 Exit 87 EndIf
EndFunction

function DateCalc($date1, $DateOrMod, optional $SingleDigit)
dim $_intDate1, $_intYear1, $_intMonth1, $_intDay1
dim $_intDate2, $_intYear2, $_intMonth2, $_intDay2
$date1 = split($date1,'/')
if ubound($date1) <> 2
exit 1
endif
$_intYear1 = val($date1[0])
$_intMonth1 = val($date1[1])
$_intDay1 = val($date1[2])
if $_intMonth1 < 3
$_intMonth1 = $_intMonth1 + 12
$_intYear1 = $_intYear1 - 1
endif
$_intDate1 = $_intDay1 + ( 153 * $_intMonth1 - 457 ) / 5 + 365 * $_intYear1 +
$_intYear1 / 4 - $_intYear1 / 100 + $_intYear1 / 400 - 306
select
case vartype($DateOrMod) = 3
$_intDate2 = $_intDate1 + $DateOrMod
if instr($_intDate2,'-') $_intDate2 = val(substr($_intDate2,2,len($_intDate2)-1)) endif
$_intYear2 = ( 100 * ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
) + (100*($_intDate2+306)-25)
) / 36525
$_intMonth2 = ( 5 * ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
) + 456
) / 153
$_intDay2 = ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
) - ( 153 * $_intMonth2 - 457
) / 5
if $_intMonth2 > 12 $_intYear2 = $_intYear2 + 1 $_intMonth2 = $_intMonth2 - 12 endif
if not $SingleDigit
if len($_intYear2 ) < 4
$_ = execute("for $i=1 to 4-len($$_intYear2) $$_intYear2 = '0' + $$_intYear2 next")
endif
$_intMonth2 = right("0" + $_intMonth2,2)
$_intDay2 = right("0" + $_intDay2,2)
endif
$DateCalc = '$_intYear2/$_intMonth2/$_intDay2'
case vartype($DateOrMod) = 8
$DateOrMod = split($DateOrMod,'/')
if ubound($DateOrMod) <> 2
exit 1
endif
$_intYear2 = val($DateOrMod[0])
$_intMonth2 = val($DateOrMod[1])
$_intDay2 = val($DateOrMod[2])
if $_intMonth2 < 3
$_intMonth2 = $_intMonth2 + 12
$_intYear2 = $_intYear2 - 1
endif
$_intDate2 = $_intDay2 + ( 153 * $_intMonth2 - 457 ) / 5 + 365 * $_intYear2 +
$_intYear2 / 4 - $_intYear2 / 100 + $_intYear2 / 400 - 306
$DateCalc = $_intDate1 - $_intDate2
;comment the next line if you wish to return negative results also !!!
if instr($DateCalc,'-') $DateCalc = val(substr($DateCalc,2,len($DateCalc)-1)) endif
case 1
exit 1
endselect
endfunction


Top
#125779 - 2004-08-29 09:05 AM Re: Get the date a file was last accessed
Cybex Offline
Getting the hang of it
*****

Registered: 2004-07-21
Posts: 81
Loc: Brandon, FL.
That is great.

The only thing I saw is on line 13...
'The file ' + $sFile + ' Last Accessed: ' + $LADate + Right(fnGetFileProp($sFile,DateLastAccessed),-9
On my sysytem it was returning 2004/08/274 10:36:27 PM

A change from -9 on the end to -10 fixed it up nicely.


Now Doc, the date format.?.?
Growing up in Florida we always used MM/DD/YYYY and in the military it was DD/MMM/YY. The YYYY MMM DD format must be a west coast thing.
_________________________
Common sense isn't so common.

Top
#125780 - 2004-08-30 07:45 AM Re: Get the date a file was last accessed
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Nope, the date is an International thing and YYYY/MM/DD is a better format, especially for computing.

Sorting by other methods sucks, and I had to convert the date format to be compatible with the @DATE macro.

Top
#125781 - 2004-08-30 12:49 PM Re: Get the date a file was last accessed
Cybex Offline
Getting the hang of it
*****

Registered: 2004-07-21
Posts: 81
Loc: Brandon, FL.
TRUE that, for sorting. I just realized I do use that date format for my log files. I tried to wrap your code into an app to run against remote computers listed in an ini file and look for apps listed in the same ini file. But I think the way you scripted your originally will not work against remote computers. Please take a look at my poorly written code and see if this is the case or just reconfirm I don't have a clue. P.S. Sorry, I killed EXPLICIT for testing.

Break On
Dim $SO, $inifile
$SO=SetOption('Explicit','OFF')

; Absolute path for the INI file
$inifile = "K:\Kix Scripts\Kix\test.ini"

ENUMCOMPUTERS($inifile, "Computers")



Function CHECKAGE($CompName, $AppName)
Dim $HKLMAppPaths,$sFile,$LADate,$TimeDiff
$HKLMAppPaths=$CompName +'\HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths'
$sFile=Trim(ReadValue($HKLMAppPaths + '\' + $AppName,''))
$LADate=Split(Trim(Join(Split(fnGetFileProp($sFile,DateLastAccessed),""),"",1)),"/")
$LADate=$LADate[2]+"/"+Right("0"+$LADate[0],2)+"/"+Right("0"+$LADate[1],2)
$TimeDiff=DateCalc(@DATE,$LADate)
If $TimeDiff => 2 ; change to date you want to test
MessageBox('The file ' + $sFile + ' has not been accessed in over ' + $TimeDiff + ' days.','Test',,)
MessageBox('The file ' + $sFile + ' Last Accessed: ' + $LADate + Right(fnGetFileProp($sFile,DateLastAccessed),-10), 'Test',,)
EndIf
EndFunction

Function fnGetFileProp($sFile,$sProp)
Dim $objFSO, $objFile, $nul
$objFSO = CreateObject("Scripting.FileSystemObject")
If NOT VarType($objFSO)=9 Exit 1 EndIf
$objFile = $objFSO.GetFile($sFile)
If NOT VarType($objFile)=9 Exit 2 EndIf
$nul=Execute("$$fnGetFileProp = $$objFile."+$sProp)
If VarType($fnGetFileProp)=0 Exit 87 EndIf
EndFunction

Function DateCalc($date1, $DateOrMod, optional $SingleDigit)
Dim $_intDate1, $_intYear1, $_intMonth1, $_intDay1
Dim $_intDate2, $_intYear2, $_intMonth2, $_intDay2
$date1 = Split($date1,'/')
If Ubound($date1) <> 2
Exit 1
EndIf
$_intYear1 = Val($date1[0])
$_intMonth1 = Val($date1[1])
$_intDay1 = Val($date1[2])
If $_intMonth1 < 3
$_intMonth1 = $_intMonth1 + 12
$_intYear1 = $_intYear1 - 1
EndIf
$_intDate1 = $_intDay1 + ( 153 * $_intMonth1 - 457 ) / 5 + 365 * $_intYear1 +
$_intYear1 / 4 - $_intYear1 / 100 + $_intYear1 / 400 - 306
Select
Case VarType($DateOrMod) = 3
$_intDate2 = $_intDate1 + $DateOrMod
If InStr($_intDate2,'-') $_intDate2 = Val(SubStr($_intDate2,2,Len($_intDate2)-1)) EndIf
$_intYear2 = ( 100 * ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
) + (100*($_intDate2+306)-25)
) / 36525
$_intMonth2 = ( 5 * ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
) + 456
) / 153
$_intDay2 = ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
) - ( 153 * $_intMonth2 - 457
) / 5
If $_intMonth2 > 12 $_intYear2 = $_intYear2 + 1 $_intMonth2 = $_intMonth2 - 12 EndIf
If NOT $SingleDigit
If Len($_intYear2 ) < 4
$_ = Execute("for $i=1 to 4-len($$_intYear2) $$_intYear2 = '0' + $$_intYear2 next")
EndIf
$_intMonth2 = Right("0" + $_intMonth2,2)
$_intDay2 = Right("0" + $_intDay2,2)
EndIf
$DateCalc = '$_intYear2/$_intMonth2/$_intDay2'
Case VarType($DateOrMod) = 8
$DateOrMod = Split($DateOrMod,'/')
If Ubound($DateOrMod) <> 2
Exit 1
EndIf
$_intYear2 = Val($DateOrMod[0])
$_intMonth2 = Val($DateOrMod[1])
$_intDay2 = Val($DateOrMod[2])
If $_intMonth2 < 3
$_intMonth2 = $_intMonth2 + 12
$_intYear2 = $_intYear2 - 1
EndIf
$_intDate2 = $_intDay2 + ( 153 * $_intMonth2 - 457 ) / 5 + 365 * $_intYear2 +
$_intYear2 / 4 - $_intYear2 / 100 + $_intYear2 / 400 - 306
$DateCalc = $_intDate1 - $_intDate2
;comment the next line if you wish to return negative results also !!!
If InStr($DateCalc,'-') $DateCalc = Val(SubStr($DateCalc,2,Len($DateCalc)-1)) EndIf
Case 1
Exit 1
EndSelect
EndFunction

Function ENUMCOMPUTERS($fl,$sectionentry)
Dim $CompKeys,$CompName
$CompKeys=Split(ReadProfileString($fl,$sectionentry,''),Chr(10))
For Each $CompName In $CompKeys
If $CompName <>''
MessageBox($CompName, "Computer",,)
ENUMAPPS($fl, "Apps", $CompName)
EndIf
EndFunction

Function ENUMAPPS($fl, $sectionentry, $CompName)
Dim $AppKeys,$AppName
$AppKeys=Split(ReadProfileString($fl,$sectionentry,''),Chr(10))
For Each $AppName In $AppKeys
; MessageBox("hi","hi",,)
If $AppName <>''
MessageBox($AppName, "Computer and App",,)
CHECKAGE($CompName, $AppName)
EndIf
EndFunction
_________________________
Common sense isn't so common.

Top
#125782 - 2004-08-30 02:27 PM Re: Get the date a file was last accessed
Anonymous
Unregistered


The test.ini looks normal...

[Computers]
bmw=0
tigger=0

[Apps]
powerpoint.exe=0
dialer.exe=0
MPIO manager 2.exe=0


There has to be a better format for the ini file. I was thinking ..
[Computers]
comp1=bmw
comp2=tigger

[Apps]
app1=powerpoint.exe
app2=dialer.exe
app3=MPIO manager 2.exe

Top
#125783 - 2004-08-30 03:32 PM Re: Get the date a file was last accessed
Cybex Offline
Getting the hang of it
*****

Registered: 2004-07-21
Posts: 81
Loc: Brandon, FL.
Woops, forgot to login... The last post about the ini file was me.
_________________________
Common sense isn't so common.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Henriques) and 2516 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.105 seconds in which 0.07 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org