WD: WordBasic Macro: Displaying a List of Files from a Directory

ID: Q117873

The information in this article applies to:

SUMMARY

This article demonstrates the Microsoft WordBasic Files$() function. This function can be used to assemble a list of file names within a directory. The Files$() function returns the first file name that matches the specified file specification (for example: *.* or *.doc). If you specify a file specification on the first iteration and then omit it thereafter, you can generate a list of files.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/supportnet/refguide/ 

The following sample macro inserts the file names from the current directory into a new document window:

   Sub MAIN
      FileNewDefault
      doc$ = Files$("*.*")
      While doc$ <> ""
         Insert doc$
         InsertPara
         doc$ = Files$()
      Wend
   End Sub

To insert only the file names with a ".doc" extension, change "*.*" to "*.doc" in the above example.

The following macro example determines how many files are in the current directory and defines an array named List$() to hold the file names. Because the Files$() function returns the file names with the full path (for example, C:\Document.doc), the FileNameInfo$() function is used to extract just the file names which are then assigned to elements within the List$() array.

   Sub MAIN
      REM Determine how many files are in the current directory.
      count = - 1
      temp$ = Files$("*.*")
      While temp$ <> ""
         count = count + 1
         temp$ = Files$()
      Wend
      If count > - 1 Then
         Dim List$(count)    'dimension array for file names
      Else
         MsgBox "There are no files in the current directory."
         Goto theend
      EndIf
      REM Extract the file name from the full path string.
      REM Assign the file names to elements in the List$() array.
      doc$ = Files$("*.*")
      For i = 0 To count
         List$(i) = FileNameInfo$(doc$, 3)
         doc$ = Files$()
      Next i
      REM Sort the array of file names and display them in a list box.
      SortArray List$()
      Begin Dialog UserDialog 396, 154, "Microsoft Word"
         OKButton 254, 88, 88, 21
         CancelButton 254, 113, 88, 21
         ListBox 18, 21, 215, 114, List$(), .ListBox1
         Text 16, 6, 175, 13, "Files in Current Directory:", .Text1
      End Dialog
      Dim dlg As UserDialog
      n = Dialog(dlg)
   theend: ' NOTE: This line must be left aligned.
   End Sub

Note to Macintosh users: The third line of the macro which reads

   doc$ = Files$("*.*")

should be replaced with the following line

   doc$ = Files$(MacID$("<TYPE>"))

where <TYPE> is the file type. For example: "TEXT", "W6BN", etc.

NOTE: The disk that accompanies the "Microsoft Word Developer's Kit," includes a macro that allows you to browse through files in any directory. The macro uses a dynamic dialog box to display a file and directory list similar to Word's Open dialog box. The macro is named ch05ex06FileBrowser and is located in the Examples.dot file.

REFERENCES

"Microsoft Word Developer's Kit," version 6.0, pages 128-130

Additional query words: Files$() Array List File Name FileName print

Keywords          : kbmacro wordnt kbmacroexample winword ntword macword word6 word7 word95 
Version           : WINDOWS:6.0,6.0a,6.0c,7.0,7.0a; MACINTOSH:6.0,6.0.1,6.0.1a
Platform          : MACINTOSH Win95 WINDOWS winnt
Issue type        : kbhowto

Last Reviewed: April 9, 1999