PC Review


Reply
Thread Tools Rate Thread

counting excel files in a folder

 
 
dstiefe
Guest
Posts: n/a
 
      13th Aug 2009
I have the file path...

now I need to count the number of xcel files
 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      13th Aug 2009
One way is to use DIR with a loop....

Sub Macro()
Dim strFolder As String, strFile As String
Dim intCount As Integer
strFolder = "d:\"
strFile = Dir(strFolder & "*.xls", vbNormal)
Do While strFile <> ""
intCount = intCount + 1
strFile = Dir
Loop
MsgBox intCount & " files found"
End Sub

--
If this post helps click Yes
---------------
Jacob Skaria


"dstiefe" wrote:

> I have the file path...
>
> now I need to count the number of xcel files

 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      13th Aug 2009
Here is another method

Sub test()
folder = "c:\temp"

Set FS = Application.FileSearch
With FS
.NewSearch
.LookIn = folder
.SearchSubFolders = False
'.Filename = "Run"
'.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks

FileCount = .Execute

If FileCount > 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
Else
MsgBox "There were no files found."
End If

End With


End Sub


"dstiefe" wrote:

> I have the file path...
>
> now I need to count the number of xcel files

 
Reply With Quote
 
RB Smissaert
Guest
Posts: n/a
 
      13th Aug 2009
This is how you can do it with the Windows API.
Haven't tested, but might be the fastest way.

Option Explicit

Private Const vbDot = 46
Private Const MAX_PATH = 260
Private Const INVALID_HANDLE_VALUE = -1
Private Const vbBackslash = "\"
Private Const ALL_FILES = "*.*"

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type

Private Declare Function FindClose Lib "kernel32" _
(ByVal hFindFile As Long) As Long
Private Declare Function FindFirstFile Lib "kernel32" _
Alias "FindFirstFileA" _
(ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA)
As Long
Private Declare Function FindNextFile Lib "kernel32" _
Alias "FindNextFileA" _
(ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As
Long
Private Declare Function lstrlen Lib "kernel32" _
Alias "lstrlenW" (ByVal lpString As Long)
As Long

Function CountFilesInFolder(sRoot As String, _
strFile As String) As Long

Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
Dim strFileName As String

hFile = FindFirstFile(sRoot & ALL_FILES, WFD)

If hFile <> INVALID_HANDLE_VALUE Then
Do
'if a folder call method again
If (WFD.dwFileAttributes And vbDirectory) Then
If Asc(WFD.cFileName) <> vbDot Then
'--------------------
'DEALING WITH FOLDERS
'--------------------
CountFilesInFolder _
sRoot & TrimNull(WFD.cFileName) & vbBackslash, _
strFile
End If
Else
'------------------
'DEALING WITH FILES
'------------------
strFileName = TrimNull(WFD.cFileName)
If strFileName Like strFile Then
CountFilesInFolder = CountFilesInFolder + 1
End If
End If
Loop While FindNextFile(hFile, WFD)
End If

FindClose hFile

End Function

Function TrimNull(strString As String) As String
TrimNull = Left$(strString, lstrlen(StrPtr(strString)))
End Function


RBS



"dstiefe" <(E-Mail Removed)> wrote in message
news:6CE0D32A-2167-49BD-81B4-(E-Mail Removed)...
>I have the file path...
>
> now I need to count the number of xcel files


 
Reply With Quote
 
dstiefe
Guest
Posts: n/a
 
      13th Aug 2009
dies this work inexcel 2007

when i do this ittells me that "Object doesn't support this action"

any ideas?

"Joel" wrote:

> Here is another method
>
> Sub test()
> folder = "c:\temp"
>
> Set FS = Application.FileSearch
> With FS
> .NewSearch
> .LookIn = folder
> .SearchSubFolders = False
> '.Filename = "Run"
> '.MatchTextExactly = True
> .FileType = msoFileTypeExcelWorkbooks
>
> FileCount = .Execute
>
> If FileCount > 0 Then
> MsgBox "There were " & .FoundFiles.Count & _
> " file(s) found."
> Else
> MsgBox "There were no files found."
> End If
>
> End With
>
>
> End Sub
>
>
> "dstiefe" wrote:
>
> > I have the file path...
> >
> > now I need to count the number of xcel files

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting xls files in a folder dstiefe Microsoft Excel Programming 8 13th Aug 2009 06:16 PM
counting excel files dstiefe Microsoft Excel Programming 2 13th Aug 2009 05:49 PM
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven Microsoft Excel Discussion 1 24th Jan 2006 02:58 PM
Counting files in a folder Hawki Microsoft Excel Programming 7 14th Apr 2004 05:54 PM
counting files in a folder jefe Microsoft Excel Programming 2 2nd Mar 2004 07:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:09 PM.