This sounds like the sort of thing for which I'd use my adverb
"generalWalkTree": http://www.jsoftware.com/jwiki/NYCJUG/2009-09-08#*
GeneralWalkTree .  This is an adverb which applies a verb to each directory
starting from the specified path and working down the tree.

For example, if I have "findFile" defined

   findFile=: ([: < '\' ,~ ]) ,&.> [ (] #~ ] +./ .E.&>~ [: < [) [: {."1 [:
dir '\*' ,~ ]

used like this

   'misc' findFile 'C:\amisc\CurrentJCode'
+---------------------------------+
|C:\amisc\CurrentJCode\miscSAS.ijs|
+---------------------------------+

I can apply it to find all my ".gz" files at or below my "C:\amisc"
directory like this:

   6!:2 'whgz=. (''.gz''&findFile) generalWalkTree ''C:\amisc'''
137.661
   $whgz
9862
   $whgz-.a:
36

Looking at one of the non-empty finds:

   >0{whgz-.a:
+------------------------------------------------------------------------------------------+
|C:\amisc\Books\ImplementationOfFunctionalLanguagesBySimonPeytonJones_slpj-book-1987.tar.gz|
+------------------------------------------------------------------------------------------+
   flst=. shell 'gunzip -l
C:\amisc\Books\ImplementationOfFunctionalLanguagesBySimonPeytonJones_slpj-book-1987.tar.gz'
   $flst
162
   flst
compressed  uncompr. ratio uncompressed_name
 31367195  40693760  22.9%
C:\amisc\Books\ImplementationOfFunctionalLanguagesBySimonPeytonJones_slpj-book-1987.tar

generalWalkTree=: 1 : 0
   (1 0) u generalWalkTree y  NB. Default: breadth-first, flattened result.
:
   ct=. 0 [ rr=. '' [ stack=. ,boxopen y [ x=. 2{.x
   ctr=. (0{x){_1 0           NB. First we build the stack.
   while. ct<#stack do.                           NB. Get subdir names:
       subds=. ((('d'e.&>4{"1])#0{"1])@:(1!:0@<)) (>ctr{stack),'\*'
       subds=. subds (],&.>'\',&.>[) ctr{stack    NB. -> full path names
       if. 0{x do. stack=. stack,subds            NB. Breadth or
       else.       stack=. subds,stack  end.      NB.  depth first
       ctr=. ctr+(0{x){_1 1 [ ct=. >:ct           NB. Go forward or backward
   end.
   dpth=. (]-<./)'\'+/ . =&>stack

   if. 1{x do. ;&.>(<:~.dpth) depthEnc dpth </. u&.>stack  NB. Preserve tree
   else.       u&.>stack              end.                 NB.  or flatten
it.
NB.EG ([:;2{"1 [:dir '*',~],'\'-.{:) generalWalkTree 'C:\' NB. All file
sizes
)

   depthEnc=: 4 : '<^:(>:x)]y'"0
   boxopen=: <^:(L. = 0:)

*
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to