<?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>Life...as it relates to Technology. &#187; dir function</title>
	<atom:link href="http://www.hobbub.com/tag/dir-function/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hobbub.com</link>
	<description>Technology, Internet and Real World Product Reviews.</description>
	<lastBuildDate>Wed, 14 Jul 2010 04:45:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Iterating over a Directory with a Dir Loop (Loop Through Directory) VB</title>
		<link>http://www.hobbub.com/vba-vb-vsto/iterating-over-a-directory-with-a-dir-loop-loop-through-directory-vb/</link>
		<comments>http://www.hobbub.com/vba-vb-vsto/iterating-over-a-directory-with-a-dir-loop-loop-through-directory-vb/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 20:46:03 +0000</pubDate>
		<dc:creator>scott</dc:creator>
				<category><![CDATA[VBA/VSTO]]></category>
		<category><![CDATA[dir function]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[vba]]></category>

		<guid isPermaLink="false">http://www.hobbub.com/vba-vb-vsto/iterating-over-a-directory-with-a-dir-loop-loop-through-directory-vb/</guid>
		<description><![CDATA[VBA is potentially the greatest desktop &#8216;hammer&#8217; you have at your disposal. You can pop open any Office application, hit ALT+F11 and be writing code before Visual Studio will even be open on most machines. 
For doing ad hoc work around the office, VBA allows you to do things that applications alone simply aren&#8217;t powerful [...]]]></description>
			<content:encoded><![CDATA[<p>VBA is potentially the greatest desktop &#8216;hammer&#8217; you have at your disposal. You can pop open any Office application, hit ALT+F11 and be writing code before Visual Studio will even be open on most machines. </p>
<p>For doing ad hoc work around the office, VBA allows you to do things that applications alone simply aren&#8217;t powerful enough to do. It seems like I&#8217;m always running into a situation where I need to get information about a bunch of files in a directory. Let&#8217;s say that you just need to retrieve the names of all files in a certain directory and write them to a column in an Excel spreadsheet. </p>
<p>Unfortunately, Excel doesn&#8217;t provide a native way to accomplish such a task. However, the task is reduced to less than 15 lines of code using VBA. The <b>VBA Dir Function</b> is great for iterating over a directory.</p>
<h3>Understanding the Dir Function</h3>
<p>This function returns a <b>String</b> value containing the name of a file, directory, or folder  that matches a specified pattern or file attribute. If you&#8217;re working at the root level of a file structure, the Dir function will also return the volume label of a  drive.<b> </p>
<p>Dir</b> returns the first file name that matches <i>pathname</i> parameter. The trick to looping through a directory is that <b><i>calling Dir with no arguments</i></b> returns any  additional file names that match <i>pathname</i>. You can see an example of this in the code snippet below: the line with the comment, &#8220;get next entry&#8221; is where the Dir function will retrieve the next matching filename.</p>
<p>When <b>Dir</b> can no longer return a match, it returns a zero-length  string (&#8221;"). If a zero-length string is returned, either a new <i>pathname</i> must be passed or an error occurs. You can change to a new  <i>pathname</i> without retrieving all of the file names that match the current  <i>pathname</i>. </p>
<p>NOTE: Do not nest <b>Dir loops</b> and don&#8217;t attempt to call the <b>Dir</b> function recursively.  Calling <b>Dir</b> with the <b>vbDirectory</b> attribute does not continually  return subdirectories. If you think you need to recurse directories, first consider whether you can copy the results of a simple Windows Search to a new directory to achieve your desired results.<br />
<h4>Looping Over Files with Dir, Looping Through Files, Looping Through Directories&#8230;</h4>
<p>The following VB code snippet is pretty easy to understand. There are three variables: strPath, strFile and x. strPath is used to pass the initial parameter to the Dir function. This simply tells Dir which directory to traverse. strFile is used to hold the file name as the code executes and continues to retrieve file names. x is used simply as a counter so that the correct cell in the Excel spreadsheet is updated with the correct filename (Dir doesn&#8217;t return files in any specific order, so you may need to sort them afterward).</p>
<p><font face="monospace">Sub LoopThruDirectory()<br />&nbsp;&nbsp;&nbsp; <br />Dim strPath As String<br />Dim strFile As String<br />Dim x As Integer</p>
<p>&nbsp;&nbsp;&nbsp; strPath = &#8220;C:\temp\datajunction\XMLOUT\&#8221;<br />&nbsp;&nbsp;&nbsp; strFile = Dir(strPath)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; Do While strFile &lt;&gt; &#8220;&#8221;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x = x + 1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sheet1.Cells(x, 1) = strFile<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strFile = Dir&nbsp;&nbsp;&nbsp; &#8216; Get next entry.<br />&nbsp;&nbsp;&nbsp; Loop<br />&nbsp;&nbsp;&nbsp; <br />End Sub</font></p>
<p>Wise Man Says, <a href="http://www.hobbub.com/aphorisms/a-bird-in-hand-is-worth-two-in-a-bush/" title="bird in hand">&#8220;A Bird in Hand is Worth Two in Bush&#8221;</a></p>
<img src="http://www.hobbub.com/?ak_action=api_record_view&id=1049&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.hobbub.com/vba-vb-vsto/iterating-over-a-directory-with-a-dir-loop-loop-through-directory-vb/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
