# Help: File.file?, ftype fails oddly

**URL:** <https://www.ruby-forum.com/t/help-file-file-ftype-fails-oddly/102064>\
**Category:** Ruby\
**Created:** [February 7, 2006, 6:16pm UTC](https://www.ruby-forum.com/t/help-file-file-ftype-fails-oddly/102064 "2006-02-07T18:16:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [February 7, 2006, 6:16pm UTC](https://www.ruby-forum.com/t/help-file-file-ftype-fails-oddly/102064/1 "2006-02-07T18:16:35Z")

</div>

Hello all,

I’ve been working on a class+script to crawl through my directories and  
thought I had it working just right, when it crashed some 13 minutes  
into  
running. I included the stack at the bottom, in case it gives a better  
hint to anyone here.

The code where it fails is this method - the if checks fail to see that  
filename is a file, and even errors out on the else’s  
File.ftype(filename)  
call:

def get\_dir\_info\_recursive change\_into\_dir, write\_to\_file,  
put\_in\_container  
Dir.chdir( change\_into\_dir )  
Dir.foreach(".") {|filename|  
if File.directory?(filename) then  
# write\_to\_file.puts stuff  
# … recursively call this method  
elsif File.file?(filename) then  
# write\_to\_file.puts stuff  
# … simple gather info and add to collection  
else  
puts “ERROR: TYPE=#{File.ftype(filename)} for #{filename} in  
#{put\_in\_container.path}”  
end  
}  
Dir.chdir(’…’)  
end

My problem solving skills came up with a few alternatives:

1. Maybe the filename/directory path is too long. After all the path +  
filename end up being right about 255 characters long.  
NOPE: Tried starting the loop right in the directory where the file  
errors out, even at earlier directories, the error doesn’t happen -  
the file is correctly identified as a file!

2. Maybe there’s a problem with the output file? It is 11\_892\_316 bytes  
at the error.  
NOPE: Used irb to append another several k to the file with seek  
then  
putc - no problem there.

3. Is there a hidden runtime problem?  
RAM consumption? NOPE, I’ve run the script on larger, though not  
deeper directories without it crashing. Though I would like to know  
how to set the RAM usage. Anyone?

Any help and ideas are much appreciated.

Thanks,

-Chris

* * *

Try #1: IF file? ELSE treat it as a directory  
Starting file fdata\_Q Mon Feb 06 22:40:22 Central Standard Time 2006  
drive7.rb:118:in `chdir': Invalid argument - _EJSRemoteStatelessCrystalItemReconControllerHome_Tie.java (Errno::EINVAL) ... ... 55 levels... ... Try #2 with drive8: do directory? THEN file? THEN try printing File.ftype: C:\CHRIS\ruby\MyFiles>ruby drive8.rb Q Starting file fdata_Q Mon Feb 06 23:45:27 Central Standard Time 2006 Finished file fdata_Q Mon Feb 06 23:58:37 Central Standard Time 2006 drive8.rb:139:in`ftype’: No such file or directory -  
\_EJSRemoteStatelessCrystalItemReconControllerHome\_Tie.java (Errno:  
:ENOENT)  
from drive8.rb:139:in `get_dir_info_recursive' from drive8.rb:122:in`foreach’  
from drive8.rb:122:in `get_dir_info_recursive' from drive8.rb:128:in`get\_dir\_info\_recursive’  
from drive8.rb:122:in `foreach' from drive8.rb:122:in`get\_dir\_info\_recursive’  
from drive8.rb:128:in `get_dir_info_recursive' from drive8.rb:122:in`foreach’  
… 54 levels…  
from drive8.rb:122:in `get_dir_info_recursive' from drive8.rb:163 from drive8.rb:148:in`each’  
from drive8.rb:148

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [February 7, 2006, 6:31pm UTC](https://www.ruby-forum.com/t/help-file-file-ftype-fails-oddly/102064/2 "2006-02-07T18:31:58Z")

</div>

DÅ?a Utorok 07 FebruÃ¡r 2006 18:15 Chris napÃ­sal:  
–snip–

> 1. Maybe the filename/directory path is too long. After all the path +  
> filename end up being right about 255 characters long.  
> NOPE: Tried starting the loop right in the directory where the file  
> errors out, even at earlier directories, the error doesn’t happen -  
> the file is correctly identified as a file!

–snip–

> ```
> Ruby + NTFS + WinXP = ?occasional problem?
> 
> ```

Actually, I think NTFS does have some arbitrary limit on the path  
length. Or  
the filename length. Or something. That said, I don’t think it’s quite  
related, since you are #chdir-ing around, but who knows.

Try to rework the script to see if you occur it even if using File.find  
or  
feeding the filenames via lessay Cygwin find, as opposed to recursing  
the  
directories by hand?

David V.

David V.

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [February 8, 2006, 7:24am UTC](https://www.ruby-forum.com/t/help-file-file-ftype-fails-oddly/102064/3 "2006-02-08T07:24:41Z")

</div>

Chris wrote:

> ```
> puts "ERROR: TYPE=#{File.ftype(filename)} for #{filename} in
> 
> ```
> 
> filename end up being right about 255 characters long.  
> NOPE: Tried starting the loop right in the directory where the file  
> errors out, even at earlier directories, the error doesn’t happen -  
> the file is correctly identified as a file!

YEP. You’re hitting the MAX\_PATH character limit. Try the following  
sample code to see what I mean:

#initial = “\\?\C:\really\_long\_test\_path\_to\_be\_deleted”  
initial = “C:\really\_long\_test\_path\_to\_be\_deleted”  
Dir.mkdir(initial)  
Dir.chdir(initial)

1.upto(40){ |n|  
dir = “this\_is\_long\_dir\_number\_#{n}”  
file = “_some\_really\_long\_file_#{n}.bogus”

File.open(file, “w+”){} # touch  
puts “Created file: #{file}”

Dir.mkdir(dir)  
puts “Created dir: #{dir}”

Dir.chdir(dir)  
puts “Changed into dir: #{dir}”

puts "Current dir length: " + Dir.pwd.length.to\_s  
puts  
}

And here’s the output:

C:\eclipse\workspace\ruby-foo\>ruby deepdir.rb  
Created file: \_some\_really\_long\_file\_1.bogus  
Created dir: this\_is\_long\_dir\_number\_1  
Changed into dir: this\_is\_long\_dir\_number\_1  
Current dir length: 68

Created file: \_some\_really\_long\_file\_2.bogus  
Created dir: this\_is\_long\_dir\_number\_2  
Changed into dir: this\_is\_long\_dir\_number\_2  
Current dir length: 94

Created file: \_some\_really\_long\_file\_3.bogus  
Created dir: this\_is\_long\_dir\_number\_3  
Changed into dir: this\_is\_long\_dir\_number\_3  
Current dir length: 120

Created file: \_some\_really\_long\_file\_4.bogus  
Created dir: this\_is\_long\_dir\_number\_4  
Changed into dir: this\_is\_long\_dir\_number\_4  
Current dir length: 146

Created file: \_some\_really\_long\_file\_5.bogus  
Created dir: this\_is\_long\_dir\_number\_5  
Changed into dir: this\_is\_long\_dir\_number\_5  
Current dir length: 172

Created file: \_some\_really\_long\_file\_6.bogus  
Created dir: this\_is\_long\_dir\_number\_6  
Changed into dir: this\_is\_long\_dir\_number\_6  
Current dir length: 198

Created file: \_some\_really\_long\_file\_7.bogus  
Created dir: this\_is\_long\_dir\_number\_7  
Changed into dir: this\_is\_long\_dir\_number\_7  
Current dir length: 224

Created file: \_some\_really\_long\_file\_8.bogus  
deepdir.rb:14:in `mkdir': No such file or directory - this_is_long_dir_number_8 (Errno::ENOENT) from deepdir.rb:14 from deepdir.rb:7:in`upto’  
from deepdir.rb:7

Note that even adding the leading “\?” to the directory doesn’t seem  
to help (though it should).

This isn’t the best error message, but that could be Microsoft’s fault

- I haven’t checked to see what error is returned for this case using C  
and GetLastError().

Regards,

Dan
