<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Winn.ws &#187; Files</title>
	<atom:link href="http://winn.ws/archives/tag/files/feed" rel="self" type="application/rss+xml" />
	<link>http://winn.ws</link>
	<description>Standards-based design &#38; development</description>
	<lastBuildDate>Sat, 28 Aug 2010 07:22:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Listing files in a directory with Rails</title>
		<link>http://winn.ws/archives/118</link>
		<comments>http://winn.ws/archives/118#comments</comments>
		<pubDate>Mon, 18 Feb 2008 02:17:05 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Files]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Showing files]]></category>

		<guid isPermaLink="false">http://www.winn.ws/archives/118</guid>
		<description><![CDATA[
In a recent application I am building I needed to pull file names and directory names from a local directory. I searched all over the net for this and found nothing. After reviewing a few of rails built in functions I came across Dir.glob(). If you feed this method a path to a directory it [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.winn.ws/wp-content/uploads/2008/02/2ndstepimage.png" style="float: left; margin:.5em;" alt="Ruby on Rails" /><br />
In a recent application I am building I needed to pull file names and directory names from a local directory. I searched all over the net for this and found nothing. After reviewing a few of rails built in functions I came across Dir.glob(). If you feed this method a path to a directory it will then come back with an array of the names and directories listed.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-9763253940568418";
/* 468x15, created 5/28/10 */
google_ad_slot = "4528267187";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>So lets say I am going to tell Dir.glob to look in my &#8220;files&#8221; folder. The path I need to feed it is starting from the root of my app so the path would be &#8220;public/files/&#8221;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@files</span> = <span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">glob</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;public/files/*&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>I added &#8220;*&#8221; so I can pull all files and dir’s back. If I just want to pull PDF’s back I can use this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@files</span> = <span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">glob</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;public/files/*.pdf&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>That will show all PDF’s in that folder. You may also give it a file name and just pull one file out.</p>
<p>Anyway back to example one, I am looking for all files and all directories in the folder &#8220;files&#8221;. Below is the array I get back from @files.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">for</span> file <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#0066ff; font-weight:bold;">@files</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> file
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;">#====</span>
Output is:
&nbsp;
public<span style="color:#006600; font-weight:bold;">/</span>files<span style="color:#006600; font-weight:bold;">/</span>test1.<span style="color:#9900CC;">gif</span>
public<span style="color:#006600; font-weight:bold;">/</span>files<span style="color:#006600; font-weight:bold;">/</span>test2.<span style="color:#9900CC;">gif</span>
public<span style="color:#006600; font-weight:bold;">/</span>files<span style="color:#006600; font-weight:bold;">/</span>folder</pre></td></tr></table></div>

<p>This is the best way I have found for pulling files from a directory and listing them in an array.</p>
<p>Need more help with rails? Here is a great book that i recommend for all new and old rails developers: <a href="http://www.amazon.com/gp/product/B0017UEQU2?ie=UTF8&#038;tag=winnws-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=B0017UEQU2">Beginning Ruby: From Novice to Professional</a><img src="http://www.assoc-amazon.com/e/ir?t=winnws-20&#038;l=as2&#038;o=1&#038;a=B0017UEQU2" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/118/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 2/10 queries in 0.002 seconds using memcached
Content Delivery Network via Amazon Web Services: CloudFront: dhbbuddo7esu2.cloudfront.net

Served from: winn.ws @ 2010-09-08 23:17:25 -->