Hi,
On 15.12.2008, at 19:12 , Aldric Giacomoni wrote:

> Einar Magnů¸ Boson wrote:
>> On 15.12.2008, at 16:17 , Aldric Giacomoni wrote:
>>
>>
>>> This is pretty ugly and doesn't work.. Can't figure out why, but  
>>> it  doesn't actually go through the main loop more than once. I'm  >> a  little stuck here.. Any help is welcome :)
>>>
>>> def keep_iterating_until_not_directory()
>>> Dir.glob('*') do |filename|
>>>  if File.directory?(filename)
>>>    Dir.chdir(filename)
>>>    keep_iterating_until_not_directory()
>>>  end
>>>  break
>>> end
>>> end
>>>
>>> def come_back_to_nycom
>>> Dir.chdir("..") while Dir.pwd != "C:/Documents and Settings/ 
>>> username/ My Documents/NYCOM"
>>> end
>>>
>>> Dir.glob('*') do |filename|
>>> if File.directory?(filename)
>>>  keep_iterating_until_not_directory()
>>> end
>>> puts Dir.pwd
>>> image = DICOM::DObject.new(Dir.glob('*')[0], verbose=false,   
>>> library=lib)
>>> come_back_to_nycom()
>>> if image.get_raw("0008,1090") == "HDI 5000"
>>>  File.rename(filename, "HDI5000" + filename)
>>> end
>>> end
>>>
>>>
>>>
>>
>>
>> something I wrote a while back that works:
>>
>> def getFilesInDirectory(dir)
>>   Dir[File.join(dir, "*")].collect do |f|
>>     if File.directory?(f) then
>>       getFilesInDirectory(f)
>>     else
>>       f
>>     end
>>   end.flatten
>> end
>>
>> then you can use it like:
>> getFilesInDirectory(Dir.pwd).each do |f|
>> 	puts f
>> end
>>
>> einarmagnus
>>
>>
>>
>>
>>
> I think I see what this does.. It's pretty neat. How would I use  
> this to only parse the first file of any given directory? I can't  
> quite picture this.
>
> --Aldric
>

You only want the first file in every directory and every sub- 
directory? is that it?

With this setup:
/dir1/file1
/dir1/dir11/file1
/dir1/dir11/file2
/dir1/dir12/file1
/dir1/dir12/file2

you only want /dir1/file1, /dir1/dir11/file1 and dir1/dir12/file1, is  hat correct?

I guess you could do something like this:

def getFilesInDirectory(dir)
   dirs = []
   files = []
   Dir[File.join(dir, "*")].each do |f|
     if File.directory?(f) then
       dirs << f
     elsif files.empty?
       files = [f]
     end
   end
   dirs.each do |dir|
     files += getFilesInDirectory(dir)
   end
   files
end


einarmagnus