Page 1 of 1 1
Topic Options
#206137 - 2012-11-07 09:09 PM List of Unused Files (Time)
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
How can I create a script to list the time certain files are not being used?

example:

Name ......... Modification
aa.txt ....... 07/11/2012
aabb.txt ..... 07/11/2012
bb.txt ....... 07/10/2011
cc.txt ....... 07/09/2010
ccdd.txt ..... 07/09/2010

result
Recently: 2 File
1 Year: 1 File
2 Years: 2 File
_________________________

Top
#206143 - 2012-11-08 12:43 AM Re: List of Unused Files (Time) [Re: Valentim]
NTDOC Administrator Offline
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 Moderator Offline
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.

 Code:
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
#206152 - 2012-11-08 01:37 PM Re: List of Unused Files (Time) [Re: ShaneEP]
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
Thanks for the tip!

I'm modifying your script to suit my needs.

I'm having trouble function "GetFileSize" return the decimal in the conversion of Bytes to Megabytes

GetFileSize (file1) = 180000/1024 = 175

In the calculator result is 175,78125
_________________________

Top
#206157 - 2012-11-08 03:28 PM Re: List of Unused Files (Time) [Re: Valentim]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Force floating point results when the result could be more than 32000 or you need decimal values. Kix sees you using whole numbers, so returns an Integer whole number. This
 Code:
$Size = 1.0 * GetFileSize($File) / 1024
starts with a floating point, which converts all of the values to floating point and will return the proper decimal value.

Multiplying by 1.0 at the start of an expression is a simple way to tell Kix to work with floating point values.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#206173 - 2012-11-09 01:16 PM Re: List of Unused Files (Time) [Re: Glenn Barnas]
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
finished script, I used UDF DateCalc calculate time of file and DirList

 Code:
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 \:D


Edited by Valentim (2012-11-09 01:23 PM)
Edit Reason: Translating language of Script to English
_________________________

Top
#206176 - 2012-11-09 04:15 PM Re: List of Unused Files (Time) [Re: Valentim]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Not like SerialDate() huh?? Lol. Seems to work though. Might be careful with that static filehandle. I usually use FreeFileHandle() just to be safe.
Top
#206180 - 2012-11-09 08:45 PM Re: List of Unused Files (Time) [Re: ShaneEP]
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
Now came another problem, variable $nSize is showing the negative value -1966681746bytes and correct value would be 6623252846bytes

any other tips to prevent this from happening?
_________________________

Top
#206183 - 2012-11-10 02:26 PM Re: List of Unused Files (Time) [Re: Valentim]
Glenn Barnas Administrator Offline
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
 Code:
$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 like
 Code:
For 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. \:D

Glenn


Edited by Glenn Barnas (2012-11-10 02:50 PM)
Edit Reason: add reference link
_________________________
Actually I am a Rocket Scientist! \:D

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
0 registered and 1538 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.063 seconds in which 0.026 seconds were spent on a total of 13 queries. Zlib compression enabled.

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