# How to tell Dir from file?

**URL:** <https://rubytalk.org/t/how-to-tell-dir-from-file/19467>\
**Category:** ruby-talk\
**Created:** [11 July 2005 21:54 UTC](https://rubytalk.org/t/how-to-tell-dir-from-file/19467 "2005-07-11T21:54:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ezra\_Zygmuntowicz3](https://avatars.discourse-cdn.com/v4/letter/e/c57346/32.png) [@Ezra\_Zygmuntowicz3](https://rubytalk.org/u/Ezra_Zygmuntowicz3)\
**Post date:** [11 July 2005 21:54 UTC](https://rubytalk.org/t/how-to-tell-dir-from-file/19467/1 "2005-07-11T21:54:18Z")

</div>

Hello list-  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I have the following script that recursively descends through some directories and changes the name of a bunch of logos to be webserver compliant.

require 'find'  
require 'ftools'  
path = "/Volumes/Users/ez/LOGOS/"

def rename(filename)  
&nbsp;&nbsp;&nbsp;new\_name = File.join(File.dirname(filename), fix\_name(File.basename(filename)))  
&nbsp;&nbsp;&nbsp;File.move(filename, new\_name)  
end

def fix\_name(name)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name.gsub!(/[\s\_]/, "-")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name.downcase!  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name.gsub!(/[^a-z0-9.-]\*/, '')  
end

Find.find(path) do |file|  
&nbsp;&nbsp;&nbsp;rename(file)  
end

I need to know how to tell the difference between a file with no extension and a directory name. The script works perfectly right now except when it first runs into a directory it makes a 0 byte file with the same name within that directory. I need to know how to test whether the current iteration contains an actual file or is a directory so I can skip renaming it.

You help is much appreciated.

-Ezra Zygmuntowicz  
Yakima Herald-Republic  
WebMaster  
509-577-7732  
[ezra@yakima-herald.com](mailto:ezra@yakima-herald.com)

---

<div class="post-metadata">

**Author:** ![Guillaume\_Marcais1](https://avatars.discourse-cdn.com/v4/letter/g/46a35a/32.png) [@Guillaume\_Marcais1](https://rubytalk.org/u/Guillaume_Marcais1)\
**Post date:** [11 July 2005 22:05 UTC](https://rubytalk.org/t/how-to-tell-dir-from-file/19467/2 "2005-07-11T22:05:19Z")

</div>

Have a look at 'ri test' or 'ri File.directory?'.

HTH,  
Guillaume.

> **···**
>
> On Tue, 2005-07-12 at 06:54 +0900, Ezra Zygmuntowicz wrote:
> 
> > Hello list-  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I have the following script that recursively descends through  
> > some directories and changes the name of a bunch of logos to be  
> > webserver compliant.
> > 
> > require 'find'  
> > require 'ftools'  
> > path = "/Volumes/Users/ez/LOGOS/"
> > 
> > def rename(filename)  
> > &nbsp;&nbsp;&nbsp;new\_name = File.join(File.dirname(filename), fix\_name(File.basename  
> > (filename)))  
> > &nbsp;&nbsp;&nbsp;File.move(filename, new\_name)  
> > end
> > 
> > def fix\_name(name)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name.gsub!(/[\s\_]/, "-")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name.downcase!  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name.gsub!(/[^a-z0-9.-]\*/, '')  
> > end
> > 
> > Find.find(path) do |file|  
> > &nbsp;&nbsp;&nbsp;rename(file)  
> > end
> > 
> > I need to know how to tell the difference between a file with no  
> > extension and a directory name. The script works perfectly right now  
> > except when it first runs into a directory it makes a 0 byte file  
> > with the same name within that directory. I need to know how to test  
> > whether the current iteration contains an actual file or is a  
> > directory so I can skip renaming it.
> > 
> > You help is much appreciated.
> > 
> > -Ezra Zygmuntowicz  
> > Yakima Herald-Republic  
> > WebMaster  
> > 509-577-7732  
> > ezra@yakima-herald.com
