home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / euphoria / walkdir.ex < prev    next >
Text File  |  1994-01-21  |  4KB  |  129 lines

  1.     -- "walk" through a directory structure
  2.  
  3. -- example: 
  4. --    ex walkdir c:\
  5. -- will examine your whole C: hard disk
  6.  
  7. -- This program recursively searches a directory and all of its subdirectories.
  8. -- For now we just make a sorted list of the biggest files encountered in 
  9. -- the search. This shows how to use the dir() built-in function.
  10. -- You might want to modify the look_at() routine below to report other 
  11. -- information. 
  12.  
  13. -- usage:  ex walkdir [path-name]  -- defaults to current directory
  14.  
  15. constant YES = 1, NO = 0
  16.  
  17. -------- Select the information you want to see ------------------------------
  18. constant FILE_INFO = YES,          -- display info on each file
  19.      BIGGEST = YES,               -- print a list of the biggest files
  20.      BIG_NUM = 20,           -- number of big files to display
  21.      RUNNING_FILE_COUNT = YES  -- display running count of files
  22. ------------------------------------------------------------------------------
  23.  
  24. include file.e
  25. include sort.e
  26.  
  27. constant SCREEN = 1, ERR = 2
  28. sequence cmd
  29.  
  30. sequence big_list
  31. big_list = {{-1, "dummy"}}
  32.  
  33. constant INDENT_AMOUNT = 4
  34.  
  35. integer indent
  36. indent = 0
  37.  
  38. integer nfiles
  39. nfiles = 0
  40.  
  41. procedure show_biggest_files()
  42. -- print list of biggest files
  43.  
  44.     if big_list[length(big_list)][1] = -1 then
  45.     -- delete dummy entry
  46.     big_list = big_list[1..length(big_list)-1]
  47.     if length(big_list) = 0 then
  48.         return
  49.     end if
  50.     end if
  51.     printf(SCREEN, "\nThe Top %d\n\n", length(big_list))
  52.     for i = 1 to length(big_list) do
  53.     printf(SCREEN, "%7d %-14s\n", big_list[i])
  54.     end for
  55. end procedure
  56.  
  57. procedure look_at(sequence path_name, sequence entry)
  58. -- "look at" a file or directory entry
  59.  
  60.     if FILE_INFO then
  61.         puts(SCREEN, repeat(' ', indent))
  62.         printf(SCREEN, "%-14s%3s %6d  %4d/%02d/%02d  %02d:%02d:%02d\n", entry)
  63.     end if
  64.     if find('d', entry[D_ATTRIBUTES]) then
  65.     return
  66.     end if
  67.     nfiles = nfiles + 1
  68.     if not FILE_INFO and RUNNING_FILE_COUNT then
  69.     printf(SCREEN, "%d \r", nfiles)
  70.     end if
  71.  
  72.     -- should it be added to the big files list?
  73.     for i = 1 to length(big_list) do
  74.     if entry[D_SIZE] > big_list[i][1] then
  75.             -- insert new big file
  76.             big_list = append(big_list[1..i-1], 
  77.                   {entry[D_SIZE], path_name & '\\' & entry[D_NAME]}) &
  78.                    big_list[i..length(big_list)]
  79.         if length(big_list) > BIG_NUM then
  80.         -- drop the smallest file
  81.         big_list = big_list[1..length(big_list)-1]
  82.         end if
  83.         exit
  84.     end if
  85.     end for
  86. end procedure
  87.  
  88. procedure walk_dir(sequence path_name)
  89. -- walk through a directory and its subdirectories 
  90. -- in alphabetical order, "looking" at each file
  91.     object d
  92.  
  93.     d = dir(path_name)
  94.     while find(path_name[length(path_name)], " \\") do
  95.     path_name = path_name[1..length(path_name)-1]
  96.     end while
  97.     if atom(d) then
  98.     puts(ERR, "Couldn't open " & path_name)
  99.     end if
  100.     d = sort(d)
  101.     for i = 1 to length(d) do
  102.     if not find(d[i][D_NAME], {".", ".."}) then
  103.         look_at(path_name, d[i])
  104.         if find('d', d[i][D_ATTRIBUTES]) then
  105.         indent = indent + INDENT_AMOUNT
  106.             walk_dir(path_name & '\\' & d[i][D_NAME])
  107.         indent = indent - INDENT_AMOUNT
  108.         end if
  109.     end if
  110.     end for
  111. end procedure
  112.  
  113. cmd = command_line()
  114. if length(cmd) = 2 then
  115.     cmd = append(cmd, ".") -- default to current directory
  116. elsif length(cmd) != 3 then
  117.     puts(ERR, "usage: ex walkdir path-name\n")
  118.     abort(1)
  119. end if
  120.  
  121. walk_dir(cmd[3])
  122.  
  123. if BIGGEST and length(dir(cmd[3])) > 1 then
  124.     show_biggest_files()
  125. end if
  126. printf(SCREEN, "%d files scanned\n", nfiles)
  127. without warning
  128.  
  129.