#206143 - 2012-11-08 12:43 AM
Re: List of Unused Files (Time)
[Re: Valentim]
|
NTDOC
Administrator
   
Registered: 2000-07-28
Posts: 11629
Loc: CA
|
First you need to know which file time you mean.
You might also want to give this link a read and then let us know more specifically what it is you're trying to do as there may be a better method or tool to do it. Most of us love KiXtart but sometimes there is a better tool to do things.
http://blogs.msdn.com/b/oldnewthing/archive/2011/10/10/10222560.aspx
|
|
Top
|
|
|
|
#206144 - 2012-11-08 12:49 AM
Re: List of Unused Files (Time)
[Re: NTDOC]
|
ShaneEP
MM club member
   
Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
|
Here is a good start. I'm doing something wrong with the ReDim's because the total count is 5 off. But as I'm out of time, I figured I would post what I had...Maybe someone else can spot the flaw.
Dim $recent[],$1years[],$2years[],$3years[],$4years[],$older[]
$today = SerialDate(@Date)
$dir = "C:\Windows"
$files = DirList($dir,2)
For Each $file in $files
$moddate = GetFileTime($file)
$sermod = SerialDate($moddate)
If ($today-$sermod) < 365
ReDim Preserve $recent[1+ubound($recent)]
$recent[ubound($recent)] = $file
Else
If ($today-$sermod) < 730
ReDim Preserve $1years[1+ubound($1years)]
$1years[ubound($1years)] = $file
Else
If ($today-$sermod) < 1095
ReDim Preserve $2years[1+ubound($2years)]
$2years[ubound($2years)] = $file
Else
If ($today-$sermod) < 1460
ReDim Preserve $3years[1+ubound($3years)]
$3years[ubound($3years)] = $file
Else
If ($today-$sermod) < 1825
ReDim Preserve $4years[1+ubound($4years)]
$4years[ubound($4years)] = $file
Else
ReDim Preserve $older[1+ubound($older)]
$older[ubound($older)] = $file
Endif
Endif
Endif
Endif
Endif
Next
? "Total Files: "+UBound($files)
? "Files Under 1 Year : "+UBound($recent)
? "Files Under 2 Years : "+UBound($1years)
? "Files Under 3 Years : "+UBound($2years)
? "Files Under 4 Years : "+UBound($3years)
? "Files Under 5 Years : "+UBound($4years)
? "Files Over 5 Years : "+UBound($older)
get $
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; SerialDate() - Converts date to a numerical value. Used for date calculations. ;;;
;;; Written By: Jens Meyer ;;;
;;; http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Board=7&Number=82574 ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function serialdate($ExpD)
Dim $z,$h,$a,$b,$c,$y,$m,$d
If InStr($ExpD,'/')
$ExpD=Split($ExpD,'/')
$y=Val($ExpD[0])
$m=Val($ExpD[1])
$d=Val($ExpD[2])
If $m<3
$m=$m+12
$y=$y-1
EndIf
$SerialDate=$d+(153*$m-457)/5+365*$y+$y/4-$y/100+$y/400-306
Else
$z=0+$ExpD+306
$h=100*$z-25
$a=$h/3652425
$b=$a-$a/4
$y=(100*$b+$h)/36525
$c=$b+$z-365*$y-$y/4
$m=(5*$c+456)/153
$d=$c-(153*$m-457)/5
If $m>12
$y=$y+1
$m=$m-12
EndIf
$SerialDate=Right('0000'+$y,4)+'/'+Right('00'+$m,2)+'/'+Right('00'+$d,2)
EndIf
EndFunction
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; DirList() - Enumerates the files in a directory into an array. ;;;
;;; Written By: Jens Meyer ;;;
;;; http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Board=7&Number=82581 ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function dirlist($dirname, optional $options)
dim $filename, $counter, $filepath, $mask
dim $list, $sublist, $subcounter
$counter=-1
$dirname=trim($dirname)
if $dirname=''
$dirname=@CURDIR
endif
if right($dirname,1)='\'
$dirname=left($dirname,len($dirname)-1)
endif
if getfileattr($dirname) & 16
$mask='*.*'
else
$mask=substr($dirname,instrrev($dirname,'\')+1)
$dirname=left($dirname,len($dirname)-len($mask)-1)
endif
redim $list[10]
$filename=dir($dirname+'\'+$mask)
while $filename<>'' and @ERROR=0
if $filename<>'.' and $filename<>'..'
select
case (getfileattr($dirname+'\'+$filename) & 16)
if $options & 1
$counter=$counter+1
if $options & 2
$list[$counter]=$dirname+'\'+$filename+'\'
else
$list[$counter]=$filename+'\'
endif
endif
if ($options & 4)
$sublist=dirlist($dirname+'\'+$filename+'\'+$mask,$options)
if ubound($sublist)+1
redim preserve $list[ubound($list)+ubound($sublist)+1]
for $subcounter=0 to ubound($sublist)
$counter=$counter+1
if $options & 2
$list[$counter]=$dirname+'\'+$filename+'\'+$sublist[$subcounter]
else
$list[$counter]=$filename+'\'+$sublist[$subcounter]
endif
next
endif
endif
case ($options & 2)
$counter=$counter+1
$list[$counter]=$dirname+'\'+$filename
case 1
$counter=$counter+1
$list[$counter]=$filename
endselect
if $counter mod 10
redim preserve $list[$counter+10]
endif
endif
$filename = dir('')
loop
if $counter+1
redim preserve $list[$counter]
else
$list=''
endif
if $mask<>'*.*' and ($options & 4)
$filename=dir($dirname+'\*.*')
while $filename<>'' and @ERROR=0
if $filename<>'.' and $filename<>'..'
if (getfileattr($dirname+'\'+$filename) & 16)
$sublist=dirlist($dirname+'\'+$filename+'\'+$mask,4)
if ubound($sublist)+1
redim preserve $list[ubound($list)+ubound($sublist)+1]
for $subcounter=0 to ubound($sublist)
$counter=$counter+1
if $options & 2
$list[$counter]=$dirname+'\'+$filename+'\'+$sublist[$subcounter]
else
$list[$counter]=$filename+'\'+$sublist[$subcounter]
endif
next
endif
endif
endif
$filename = dir('')
loop
endif
if $counter+1
redim preserve $list[$counter]
else
$list=''
endif
$dirlist=$list
endfunction
|
|
Top
|
|
|
|
#206173 - 2012-11-09 01:16 PM
Re: List of Unused Files (Time)
[Re: Glenn Barnas]
|
Valentim
Fresh Scripter
Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
|
finished script, I used UDF DateCalc calculate time of file and DirList
Break ON
Dim $aRecent[], $a1Year[], $a2Years[], $a3Years[], $a4Years[], $aOld[]
$dir = "C:\temp"
$log = "C:\temp\Unused Files [Temp].log"
$lArq = "N"
$Files = DirList($dir+"\*.*",2+4)
If EXIST ($log)
DEL $log
EndIf
If Open( 3 , $log , 5 ) <> 0
Beep
? "Error Code : [" + @ERROR + "]"
EndIf
For Each $File in $Files
SELECT
CASE DateCalc(@date,substr(getfiletime($File),1,10)) <= 365
REDIM PRESERVE $aRecent[1+UBOUND($aRecent)]
$aRecent[UBOUND($aRecent)] = $File
CASE DateCalc(@date,substr(getfiletime($File),1,10)) >= 366 And DateCalc(@date,substr(getfiletime($File),1,10)) <= 730
REDIM PRESERVE $a1Year[1+UBOUND($a1Year)]
$a1Year[UBOUND($a1Year)] = $File
CASE DateCalc(@date,substr(getfiletime($File),1,10)) >= 731 And DateCalc(@date,substr(getfiletime($File),1,10)) <= 1095
REDIM PRESERVE $a2Years[1+UBOUND($a2Years)]
$a2Years[UBOUND($a2Years)] = $File
CASE DateCalc(@date,substr(getfiletime($File),1,10)) >= 1096 And DateCalc(@date,substr(getfiletime($File),1,10)) <= 1460
REDIM PRESERVE $a3Years[1+UBOUND($a3Years)]
$a3Years[UBOUND($a3Years)] = $File
CASE DateCalc(@date,substr(getfiletime($File),1,10)) >= 1461 And DateCalc(@date,substr(getfiletime($File),1,10)) <= 1825
REDIM PRESERVE $a4Years[1+UBOUND($a4Years)]
$a4Years[UBOUND($a4Years)] = $File
CASE DateCalc(@date,substr(getfiletime($File),1,10)) >= 1826
REDIM PRESERVE $aOld[1+UBOUND($aOld)]
$aOld[UBOUND($aOld)] = $File
ENDSELECT
Next
$x = WriteLine( 3 , "Files : " + (UBOUND($Files)+1) + @CRLF )
$x = WriteLine( 3 , " "+ @CRLF )
$x = WriteLine( 3 , "Recent : " + (UBOUND($aRecent)+1) + @CRLF )
MyFunc( $aRecent, "Recent" )
$x = WriteLine( 3 , "1 Year : " + (UBOUND($a1Year)+1) + @CRLF )
MyFunc( $a1Year, "1 Ano" )
$x = WriteLine( 3 , "2 Years : " + (UBOUND($a2Years)+1) + @CRLF )
MyFunc( $a2Years, "2 Years" )
$x = WriteLine( 3 , "3 Years : " + (UBOUND($a3Years)+1) + @CRLF )
MyFunc( $a3Years, "3 Years" )
$x = WriteLine( 3 , "4 Years : " + (UBOUND($a4Years)+1) + @CRLF )
MyFunc( $a4Years, "4 Years" )
$x = WriteLine( 3 , "Above 5 Years : " + (UBOUND($aOld)+1) + @CRLF )
MyFunc( $aOld, "Above 5 Years" )
$x = Close(3)
Function MyFunc( $pArray, $cTime )
$nSize = 0
For Each $Element In $pArray
If $lArq == "S"
$x = WriteLine( 3 , " - " + $Element + @CRLF )
EndIf
$nSize = $nSize + GetFileSize($Element)
Next
$x = WriteLine( 3 , " * Size ["+$cTime+"] : " + FormatNumber((1.0 * $nSize), 2) + " bytes" + @CRLF )
$x = WriteLine( 3 , "" + @CRLF )
$x = WriteLine( 3 , "" + @CRLF )
EndFunction
Thanks to everyone for help
Edited by Valentim (2012-11-09 01:23 PM) Edit Reason: Translating language of Script to English
_________________________
|
|
Top
|
|
|
|
#206183 - 2012-11-10 02:26 PM
Re: List of Unused Files (Time)
[Re: Valentim]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
|
Use the "$Var = 1.0 * GetFileSize()" when you first reference the function, not later when you use the value. By immediately casting to a float, Kix will retain it as a float.
Also, your code is fairly ineffecient.. your Select/Case calls the "DateCalc(@date,substr(getfiletime($File),1,10))" over and over until a match is found. Try something like
$Age = DateCalc(@date,substr(getfiletime($File),1,10))
Select
Case $Age <= 365
stuff..
Case $Age >365 And $Age <= 730
stuff..
EndSelect Also - this method doesn't account for leap years, which may or may not be important. The TimeDiff() UDF can return the age in years as a decimal value, so if a file is 547 days old, the value returned would be "1.5" You could then do something likeFor Each $File in $Files
; TimeDiff - calculate time between two date/time values
; Arg 1: Start Date/Time
; Arg 2: End Date - use "now" for calculating age to the minute, "today" to calculate on today's date only
; Arg 3: 1 char value to return time in Minutes, Hours, Days, or Years
; Arg 4: (not used here) Calculate difference to ms instead of seconds
$Years = TimeDiff(getfiletime($File),1,10), 'today', 'y')
SELECT
; work in reverse age order
Case $Years > 5 ; OLD!
REDIM PRESERVE $aOld[1+UBOUND($aOld)]
$aOld[UBOUND($aOld)] = $File
Case $Years >= 4 ;
REDIM PRESERVE $a4Years[1+UBOUND($a4Years)]
$a4Years[UBOUND($a4Years)] = $File
Case $Years >= 3
REDIM PRESERVE $a3Years[1+UBOUND($a3Years)]
$a3Years[UBOUND($a3Years)] = $File
Case $Years >= 2
REDIM PRESERVE $a2Years[1+UBOUND($a2Years)]
$a2Years[UBOUND($a2Years)] = $File
Case $Years >= 1
REDIM PRESERVE $a1Year[1+UBOUND($a1Year)]
$a1Year[UBOUND($a1Year)] = $File
Case 1
REDIM PRESERVE $aRecent[1+UBOUND($aRecent)]
$aRecent[UBOUND($aRecent)] = $File
EndSelect
Next By working "backwards", you don't have to perform range calculations on each Case. You can just compare the years as being greater or equal to some value. For example, if a file is 5.3 years old, it trips the first Case and the rest are ignored.. if a file is 2.3 years old, the >=5, >=4 and >=3 case comparisons fail, and the >=2 Case triggers and the remaining are ignored.
TimeDiff() is also leap-year aware, eliminating any chance for error. The latest version of TimeDiff is on my web site's Resources/Kix UDF Library page. TimeConvert() is also available to convert between cTime and string date/time values. Feed it a date/time and you get a cTime value. Feed it a cTime value, you get a Date/time string. Supports negative cTime values and a definable epoch so you can perform calculations from BC to the present. Note that it does not handle the calendar change of September 1752. If your dates pass over that range, you will need to subtract the missing calendar days from your result. It's unlikely that this will ever impact your file age logic. 
Glenn
Edited by Glenn Barnas (2012-11-10 02:50 PM) Edit Reason: add reference link
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 235 anonymous users online.
|
|
|