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

I have a spreadsheet that I would like to indicate how
many files are present in a folder on my hard drive.
Is it possible to write some sort of logic formula or VBA
to count the files in folder x?

Thanks,
Jefe
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default counting files in a folder

Jefe,

Here are 2 versions, one a simple (slow) Fiilesearch, the second a faster
FileSystemObject version.

The reason I included both is that as expected they return different results
on my machine. Who knows which is correct, not me.

'----------------------------------------------------------------------
' FileSearch
'----------------------------------------------------------------------
Function FilesFound(Drive As String)
Dim fs

Set fs = Application.FileSearch
With fs
.LookIn = Drive
.SearchSubFolders = True
.FileName = "*.*"
If .Execute() 0 Then
FilesFound = .FoundFiles.Count
Else
FilesFound = -1
End If
End With


End Function

'----------------------------------------------------------------------
' FileSystemObject
'----------------------------------------------------------------------
Dim FSO As Object

Function FilesFoundFSO(Drive As String) As Long
Dim i As Long
Dim cfiles As Double
Dim ODrive As Object
Dim oFolder As Object

Set FSO = CreateObject("Scripting.FileSystemObject")

cfiles = 0
' Set ODrive = FSO.GETDRIVE(FSO.getdrivename(Drive))
' For Each oFolder In ODrive.folders
SelectFiles Drive, cfiles
' Next oFolder

FilesFoundFSO = cfiles

End Function

'-----------------------------------------------------------------------
Sub SelectFiles(ByVal sFolder, ByRef FileCount As Double)
'-----------------------------------------------------------------------
Dim fldr As Object
Dim Folder As Object
Dim file As Object
Dim Files As Object

Set Folder = FSO.GetFolder(sFolder)

On Error GoTo exit_sub
FileCount = FileCount + Folder.Files.Count
For Each fldr In Folder.Subfolders
SelectFiles fldr.Path, FileCount
Next
exit_sub:
End Sub



--

HTH

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

"jefe" wrote in message
...
I have a spreadsheet that I would like to indicate how
many files are present in a folder on my hard drive.
Is it possible to write some sort of logic formula or VBA
to count the files in folder x?

Thanks,
Jefe



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default counting files in a folder

Jefe,

mis-read your question, read it as a drive not folder. Both sill work on a
single folder, just use
MsgBox FoundFiles("C:\myTest")
or
MsgBox FoundFilesFSO("C:\myTest")

By the way, they also count all subfolder files, not just the top level.

--

HTH

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

"Bob Phillips" wrote in message
...
Jefe,

Here are 2 versions, one a simple (slow) Fiilesearch, the second a faster
FileSystemObject version.

The reason I included both is that as expected they return different

results
on my machine. Who knows which is correct, not me.

'----------------------------------------------------------------------
' FileSearch
'----------------------------------------------------------------------
Function FilesFound(Drive As String)
Dim fs

Set fs = Application.FileSearch
With fs
.LookIn = Drive
.SearchSubFolders = True
.FileName = "*.*"
If .Execute() 0 Then
FilesFound = .FoundFiles.Count
Else
FilesFound = -1
End If
End With


End Function

'----------------------------------------------------------------------
' FileSystemObject
'----------------------------------------------------------------------
Dim FSO As Object

Function FilesFoundFSO(Drive As String) As Long
Dim i As Long
Dim cfiles As Double
Dim ODrive As Object
Dim oFolder As Object

Set FSO = CreateObject("Scripting.FileSystemObject")

cfiles = 0
' Set ODrive = FSO.GETDRIVE(FSO.getdrivename(Drive))
' For Each oFolder In ODrive.folders
SelectFiles Drive, cfiles
' Next oFolder

FilesFoundFSO = cfiles

End Function

'-----------------------------------------------------------------------
Sub SelectFiles(ByVal sFolder, ByRef FileCount As Double)
'-----------------------------------------------------------------------
Dim fldr As Object
Dim Folder As Object
Dim file As Object
Dim Files As Object

Set Folder = FSO.GetFolder(sFolder)

On Error GoTo exit_sub
FileCount = FileCount + Folder.Files.Count
For Each fldr In Folder.Subfolders
SelectFiles fldr.Path, FileCount
Next
exit_sub:
End Sub



--

HTH

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

"jefe" wrote in message
...
I have a spreadsheet that I would like to indicate how
many files are present in a folder on my hard drive.
Is it possible to write some sort of logic formula or VBA
to count the files in folder x?

Thanks,
Jefe





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
Check Folder For Files Dave Excel Discussion (Misc queries) 6 November 20th 07 11:16 PM
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


All times are GMT +1. The time now is 02:37 PM.

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"