# Visual Basic 6.0 Question

**URL:** <https://boards.straightdope.com/t/visual-basic-6-0-question/212258>\
**Category:** Factual Questions\
**Created:** [November 9, 2003, 5:51pm UTC](https://boards.straightdope.com/t/visual-basic-6-0-question/212258 "2003-11-09T17:51:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![High\_Deity](https://avatars.discourse-cdn.com/v4/letter/h/e9a140/32.png) [@High\_Deity](https://boards.straightdope.com/u/High_Deity)\
**Post date:** [November 9, 2003, 5:51pm UTC](https://boards.straightdope.com/t/visual-basic-6-0-question/212258/1 "2003-11-09T17:51:59Z")

</div>

I need to get a list of files from a directory and store them in an array of some form so I can modify their names to display them in a TreeView. How would one do this?

---

<div class="post-metadata">

**Author:** ![OttoDaFe](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ottodafe/32/356_2.png) [@OttoDaFe](https://boards.straightdope.com/u/OttoDaFe)\
**Post date:** [November 9, 2003, 6:05pm UTC](https://boards.straightdope.com/t/visual-basic-6-0-question/212258/2 "2003-11-09T18:05:43Z")

</div>

I’ve had to do this several times, and my solution has been to use the **dir** command to write the directory to a temp file, then use the temp file to load the array/dropdown/whatever. Sorry I can’t be more specific, but the code I’ve created is on my work computer.

I don’t particularly care this approach, though, because a) I dislike temp files on principle, and b) you have to go through some gyrations to make sure the **dir** finishes before the program proceeds. Perhaps someone will be along with a more elegant solution; if not, I’ll respond in more detail at a later time.

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [November 9, 2003, 6:23pm UTC](https://boards.straightdope.com/t/visual-basic-6-0-question/212258/3 "2003-11-09T18:23:46Z")

</div>

Look at FindFirst file and FindNextFile WIN32 functions.

---

<div class="post-metadata">

**Author:** ![Parallax](https://avatars.discourse-cdn.com/v4/letter/p/bbce88/32.png) [@Parallax](https://boards.straightdope.com/u/Parallax)\
**Post date:** [November 10, 2003, 12:53am UTC](https://boards.straightdope.com/t/visual-basic-6-0-question/212258/4 "2003-11-10T00:53:37Z")

</div>

You guys are making this harder than it needs to be. =) Just use the VB Dir function in a loop to get the directory listing like this:

```
Dim sFile As String

sFile = Dir("c:\")

Do While (Len(sFile) &gt; 0)
    Debug.Print sFile
    sFile = Dir
Loop

```

Inside the loop you can do whatever you’d like with the file name - you could add it right to the treeview or store it in an array or a collection for later use.

---

<div class="post-metadata">

**Author:** ![cstamets](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@cstamets](https://boards.straightdope.com/u/cstamets)\
**Post date:** [November 10, 2003, 3:10am UTC](https://boards.straightdope.com/t/visual-basic-6-0-question/212258/5 "2003-11-10T03:10:29Z")

</div>

> [@](#):
>
> \*Originally posted by +MDI \*  
> \*\*Look at FindFirst file and FindNextFile WIN32 functions. \*\*

I like these API functions for this sort of thing, also.  
Look [here](http://www.mvps.org/vbnet/code/comctl/lvdemo2.htm) for an example.

Parallax’s suggestion is probably simpler. API call performance is probably better. At least it was in my own experience of using it.

---

<div class="post-metadata">

**Author:** ![Armilla](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Armilla](https://boards.straightdope.com/u/Armilla)\
**Post date:** [November 10, 2003, 11:16am UTC](https://boards.straightdope.com/t/visual-basic-6-0-question/212258/6 "2003-11-10T11:16:48Z")

</div>

Call me lazy if you like, but more often than not I just use the objects exposed by the Microsoft Scripting Runtime (FileSystemObject). Saves a lot of hassle, but whether you can use them depends on your target deployment environment.

If I’m only after filenames then FindFirstFile/FindNextFile just seems like too much effort.
