Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Counting files in a folder

Is there an easy way to count files in a given folder?
TIA...
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Counting files in a folder

Hi
try something like the following:


Sub test()
Dim i As Long
With Application.FileSearch
.NewSearch
.LookIn = "C:\Temp\"
.SearchSubFolders = False
.MatchTextExactly = False
.FileType = msoFileTypeAllFiles
If .Execute(msoSortOrderDescending) 0 Then
MsgBox "There were " & .FoundFiles.Count & " file(s) found."
Else
MsgBox "There were no files found."
End If
End With
End Sub



--
Regards
Frank Kabel
Frankfurt, Germany


Hawki wrote:
Is there an easy way to count files in a given folder?
TIA...

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Counting files in a folder

Try something like

Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Debug.Print FSO.GetFolder("C:\Temp").Files.Count


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Hawki" wrote in message
...
Is there an easy way to count files in a given folder?
TIA...



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Counting files in a folder

Hi Chip

Is there also a easy way to count only the Excel files for example
with your example.


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Chip Pearson" wrote in message ...
Try something like

Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Debug.Print FSO.GetFolder("C:\Temp").Files.Count


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Hawki" wrote in message
...
Is there an easy way to count files in a given folder?
TIA...





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Counting files in a folder

Ron,

Is there also a easy way to count only the Excel files for

example

You'd have to loop through the Files collection and check the
extension.

Dim FSO As Object
Dim F As Object
Dim N As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
For Each F In FSO.GetFolder("H:\Temp").Files
If StrComp(Right$(F.Name, 4), ".xls") = 0 Then
N = N + 1
End If
Next F
Debug.Print N



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Ron de Bruin" wrote in message
...
Hi Chip

Is there also a easy way to count only the Excel files for

example
with your example.


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Chip Pearson" wrote in message

...
Try something like

Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Debug.Print FSO.GetFolder("C:\Temp").Files.Count


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Hawki" wrote in

message
...
Is there an easy way to count files in a given folder?
TIA...









  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default Counting files in a folder

Ron,

I think you'll have to loop for that:

Public Function glCountXLFilesInFolder(rsPath _
As String) As Long
Dim FSO As Object
Dim fil As Object
Dim lCount As Long

Set FSO = CreateObject("Scripting.FileSystemObject")
For Each fil In FSO.GetFolder(rsPath).Files
If StrComp(Right$(fil.Name, 4), ".xls", _
vbTextCompare) = 0 Then lCount = lCount + 1
Next fil
glCountXLFilesInFolder = lCount
Set FSO = Nothing
End Function

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


Ron de Bruin wrote:
Hi Chip

Is there also a easy way to count only the Excel files for example
with your example.



"Chip Pearson" wrote in message
...
Try something like

Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Debug.Print FSO.GetFolder("C:\Temp").Files.Count


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Hawki" wrote in message
...
Is there an easy way to count files in a given folder?
TIA...

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Counting files in a folder

ThanksChip

I was hoping for a other(faster) way<g

I always use the code that Frank posted
with .FileType = msoFileTypeExcelWorkbooks


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Chip Pearson" wrote in message ...
Ron,

Is there also a easy way to count only the Excel files for

example

You'd have to loop through the Files collection and check the
extension.

Dim FSO As Object
Dim F As Object
Dim N As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
For Each F In FSO.GetFolder("H:\Temp").Files
If StrComp(Right$(F.Name, 4), ".xls") = 0 Then
N = N + 1
End If
Next F
Debug.Print N



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Ron de Bruin" wrote in message
...
Hi Chip

Is there also a easy way to count only the Excel files for

example
with your example.


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Chip Pearson" wrote in message

...
Try something like

Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Debug.Print FSO.GetFolder("C:\Temp").Files.Count


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Hawki" wrote in

message
...
Is there an easy way to count files in a given folder?
TIA...








  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Counting files in a folder

This was precisely what I needed (much better than what I
had come up with)...thanks a bunch.
-----Original Message-----
Ron,

Is there also a easy way to count only the Excel files

for
example

You'd have to loop through the Files collection and check

the
extension.

Dim FSO As Object
Dim F As Object
Dim N As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
For Each F In FSO.GetFolder("H:\Temp").Files
If StrComp(Right$(F.Name, 4), ".xls") = 0 Then
N = N + 1
End If
Next F
Debug.Print N



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Ron de Bruin" wrote in message
...
Hi Chip

Is there also a easy way to count only the Excel files

for
example
with your example.


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Chip Pearson" wrote in message

...
Try something like

Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Debug.Print FSO.GetFolder("C:\Temp").Files.Count


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Hawki" wrote in

message
...
Is there an easy way to count files in a given

folder?
TIA...






.

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
Pulling pdf files from general folder to specific folder [email protected] Excel Discussion (Misc queries) 2 September 8th 09 09:41 PM
Name of files in a folder Irfan Khan[_2_] Excel Discussion (Misc queries) 2 August 26th 08 08:32 AM
Copying all files in a folder to new folder michaelberrier Excel Discussion (Misc queries) 2 June 20th 06 05:35 AM
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven Excel Discussion (Misc queries) 1 January 24th 06 03:28 PM
counting files in a folder jefe Excel Programming 2 March 2nd 04 07:36 PM


All times are GMT +1. The time now is 03:16 AM.

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

About Us

"It's about Microsoft Excel"