Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 42
Default Files in a dir

Can anyone tell me a small loop of code to find the total
number of files in a dir ?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Files in a dir

You could use the FileSystemObject:

Sub test()
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("D:\Temp\Test")
Set Files = folder.Files

' Get the Count
Debug.Print Files.Count

Set folder = Nothing
Set Files = Nothing
Set fso = Nothing

End Sub

Cheers

Leyton

-----Original Message-----
Can anyone tell me a small loop of code to find the total
number of files in a dir ?
.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Files in a dir

Neil:
Look up the FoundFiles Object in Excel VBA Help.
Some excellent examples there.

hth,
Jim Feaver
"Neil" wrote in message
...
Can anyone tell me a small loop of code to find the total
number of files in a dir ?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Files in a dir

Neil,

Here is a simple UDF.

Function FileCount(FolderName As String, _
Optional FileFilter As String = "*", _
Optional FileTypes As Long = 1, _
Optional SubFolders As Boolean = False) As Long
With Application.FileSearch
.NewSearch
.LookIn = FolderName
.SearchSubFolders = SubFolders
.Filename = FileFilter
.MatchTextExactly = True
.FileType = FileTypes
.Execute
FileCount = .FoundFiles.Count
End With
End Function

Use like so

FileCount("C:\myTest") - all files in a directory
FileCount("C:\myTest",,,True) - all files in a directory, including
subfolders
FileCount("C:\myTest","Test") - all files in a directory with 'test' in the
name
FileCount("C:\myTest",,4) - all Excel workbooks in a directory

This can be called from a worksheet or VBA, VBA has the advantage of
allowing the msoFileType Enum for file types.


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Neil" wrote in message
...
Can anyone tell me a small loop of code to find the total
number of files in a dir ?



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Navigating to Excel files over network slow, but not Word files Newbie123 Excel Discussion (Misc queries) 1 December 2nd 09 01:18 PM
Excel 2007 tmp files filling up drive with XLSM files Jim Excel Worksheet Functions 0 September 12th 08 03:31 PM
How to change default Open/Files of Type to "Microsoft Excel Files Tammy Excel Discussion (Misc queries) 2 January 14th 08 11:06 PM
converter tool to convert XL 2007 files to XL 2003 files Dave F Excel Discussion (Misc queries) 6 December 15th 06 12:45 AM
Drive Erased, got Files back but only excel files scrambled, help. Shawnky Excel Discussion (Misc queries) 0 May 8th 06 07:26 PM


All times are GMT +1. The time now is 03:24 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"