Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,388
Default Check if a folder has x files in it.

Hi Everyone,

I am trying to do the following.

I have a folder e.g. C:\Folder1

I have this path defined as folderPath1.

I want to run a macro that will check that Folder1 has 24 files in it. If
not, a message box will display, prompting the user to check the folder again.

Can anyone help?

Thanks
Dave
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,316
Default Check if a folder has x files in it.

Try something along these lines, if the variable l does not equal 24 a
message box is displayed with the file count:

Sub CountEm()

Dim l As Long
Dim strDir As String

strDir = Dir("C:\*.*")

Do Until strDir = ""
l = l + 1
strDir = Dir

Loop

If l = 24 Then
Exit Sub
Else
MsgBox "You have " & l & " files in that directory."
End If

--
Kevin Backmann


"Dave" wrote:

Hi Everyone,

I am trying to do the following.

I have a folder e.g. C:\Folder1

I have this path defined as folderPath1.

I want to run a macro that will check that Folder1 has 24 files in it. If
not, a message box will display, prompting the user to check the folder again.

Can anyone help?

Thanks
Dave

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Check if a folder has x files in it.

Option Explicit
Sub testme()
Dim FSO As Object
Dim FolderName As String

FolderName = "c:\yourfoldernamehere"

Set FSO = CreateObject("Scripting.FileSystemObject")

If FSO.FolderExists(FolderName) = False Then
MsgBox FolderName & " not found"
Else
if FSO.GetFolder(FolderName).Files.Count < 24 then
msgbox "Please check that folder!"
end if
End If
End Sub


Dave wrote:

Hi Everyone,

I am trying to do the following.

I have a folder e.g. C:\Folder1

I have this path defined as folderPath1.

I want to run a macro that will check that Folder1 has 24 files in it. If
not, a message box will display, prompting the user to check the folder again.

Can anyone help?

Thanks
Dave


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default Check if a folder has x files in it.

On Sun, 11 Nov 2007 16:39:00 -0800, Dave
wrote:

Hi Everyone,

I am trying to do the following.

I have a folder e.g. C:\Folder1

I have this path defined as folderPath1.

I want to run a macro that will check that Folder1 has 24 files in it. If
not, a message box will display, prompting the user to check the folder again.

Can anyone help?

Thanks
Dave


You can use the FileSystemObject.

The code below sets a reference to Microsoft Scripting Runtime. You could use
late binding but if you set a reference, it's easier to get Help while writing
the routines.

As written, the count returned includes all files, including any that might be
Hidden or various System Files.

But you could also use the FSO to test the files to see if they should be
counted based on their attributes and type.


==================================
Option Explicit
Sub filecount()
'Requires setting reference to Microsoft Scripting Runtime
Dim fso As FileSystemObject
Dim fo As Folder
Dim fi As File
Dim i As Long

'change this to reflect your actual path
Const fldname As String = "C:\"

Set fso = New FileSystemObject
Set fo = fso.getfolder(fldname)

i = fo.Files.Count

If i < 24 Then
MsgBox ("Folder has " & i & " files. Should have 24")
End If


'Below was just used for debugging
'might be useful if you want to filter the count based on file type

'On Error GoTo errMgr 'some files gave an error looking at ShortName
'For Each fi In fo.Files
' i = i + 1
' Debug.Print fi.ShortName, fi.Name, , fi.Attributes, fi.Type
'Next fi
'Exit Sub

'errMgr:
' Debug.Print "**Error**" & vbLf & fi.Name, , fi.Attributes, fi.Type & vbLf
'Resume Next
End Sub
==========================================
--ron
  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,388
Default Check if a folder has x files in it.

If i wanted to build this into a current macro, what could I use instead of
Exit Sub?

Thanks!
Dave

"Kevin B" wrote:

Try something along these lines, if the variable l does not equal 24 a
message box is displayed with the file count:

Sub CountEm()

Dim l As Long
Dim strDir As String

strDir = Dir("C:\*.*")

Do Until strDir = ""
l = l + 1
strDir = Dir

Loop

If l = 24 Then
Exit Sub
Else
MsgBox "You have " & l & " files in that directory."
End If

--
Kevin Backmann


"Dave" wrote:

Hi Everyone,

I am trying to do the following.

I have a folder e.g. C:\Folder1

I have this path defined as folderPath1.

I want to run a macro that will check that Folder1 has 24 files in it. If
not, a message box will display, prompting the user to check the folder again.

Can anyone help?

Thanks
Dave



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 7,247
Default Check if a folder has x files in it.

Try something like

Function FileCount(FolderName As String) As Long
On Error Resume Next
FileCount = CreateObject("Scripting.FileSystemObject"). _
GetFolder(FolderName).Files.Count
End Function

If you want to count files in nested subfolders, use
Function FileCount2(FolderName As String) As Long
On Error Resume Next
Dim FSO As Object
Dim N As Long
Dim FF As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FF = FSO.getfolder(FolderName)
N = FSO.getfolder(FF).Files.Count
If FF.subfolders.Count 0 Then
For Each FF In FF.subfolders
N = N + FileCount2(FF.Path)
Next FF
End If
FileCount2 = N
End Function


You can then call this function with code like the following.

Dim N As Long
N = FileCount("C:\Test")
If N = 24 Then
Debug.Print "OK: " & CStr(N) & " files"
Else
Debug.Print "Not OK: " & CStr(N) & " files"
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)




"Dave" wrote in message
...
Hi Everyone,

I am trying to do the following.

I have a folder e.g. C:\Folder1

I have this path defined as folderPath1.

I want to run a macro that will check that Folder1 has 24 files in it. If
not, a message box will display, prompting the user to check the folder
again.

Can anyone help?

Thanks
Dave


  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,388
Default Check if a folder has x files in it.

Hi Chip,

Thanks for this. It does work if I use a direct link e.g. C:\ but if I want
to use a link referred to as legacy1path it doesn't work. I get an error of:

Compile error:
ByRef Argument type mismatch.

Any tips.

Thanks!
Dave

"Chip Pearson" wrote:

Try something like

Function FileCount(FolderName As String) As Long
On Error Resume Next
FileCount = CreateObject("Scripting.FileSystemObject"). _
GetFolder(FolderName).Files.Count
End Function

If you want to count files in nested subfolders, use
Function FileCount2(FolderName As String) As Long
On Error Resume Next
Dim FSO As Object
Dim N As Long
Dim FF As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FF = FSO.getfolder(FolderName)
N = FSO.getfolder(FF).Files.Count
If FF.subfolders.Count 0 Then
For Each FF In FF.subfolders
N = N + FileCount2(FF.Path)
Next FF
End If
FileCount2 = N
End Function


You can then call this function with code like the following.

Dim N As Long
N = FileCount("C:\Test")
If N = 24 Then
Debug.Print "OK: " & CStr(N) & " files"
Else
Debug.Print "Not OK: " & CStr(N) & " files"
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)




"Dave" wrote in message
...
Hi Everyone,

I am trying to do the following.

I have a folder e.g. C:\Folder1

I have this path defined as folderPath1.

I want to run a macro that will check that Folder1 has 24 files in it. If
not, a message box will display, prompting the user to check the folder
again.

Can anyone help?

Thanks
Dave


  #8   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Check if a folder has x files in it.

How did you declare legacy1path? Did you use "As String" or "As Variant" or did
you not declare that variable at all (and excel sees those undeclared variables
as Variants).

I'd declare it as a string or pass cstr(legacy1path) to the function.



Dave wrote:

Hi Chip,

Thanks for this. It does work if I use a direct link e.g. C:\ but if I want
to use a link referred to as legacy1path it doesn't work. I get an error of:

Compile error:
ByRef Argument type mismatch.

Any tips.

Thanks!
Dave

"Chip Pearson" wrote:

Try something like

Function FileCount(FolderName As String) As Long
On Error Resume Next
FileCount = CreateObject("Scripting.FileSystemObject"). _
GetFolder(FolderName).Files.Count
End Function

If you want to count files in nested subfolders, use
Function FileCount2(FolderName As String) As Long
On Error Resume Next
Dim FSO As Object
Dim N As Long
Dim FF As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FF = FSO.getfolder(FolderName)
N = FSO.getfolder(FF).Files.Count
If FF.subfolders.Count 0 Then
For Each FF In FF.subfolders
N = N + FileCount2(FF.Path)
Next FF
End If
FileCount2 = N
End Function


You can then call this function with code like the following.

Dim N As Long
N = FileCount("C:\Test")
If N = 24 Then
Debug.Print "OK: " & CStr(N) & " files"
Else
Debug.Print "Not OK: " & CStr(N) & " files"
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)




"Dave" wrote in message
...
Hi Everyone,

I am trying to do the following.

I have a folder e.g. C:\Folder1

I have this path defined as folderPath1.

I want to run a macro that will check that Folder1 has 24 files in it. If
not, a message box will display, prompting the user to check the folder
again.

Can anyone help?

Thanks
Dave



--

Dave Peterson
  #9   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,388
Default Check if a folder has x files in it.

legacy1path has been declared above like this:

Set Fobj = CreateObject("Scripting.FileSystemObject")
Set wbCodeBook = ThisWorkbook
Set legacy1 = Application.FileDialog(msoFileDialogFolderPicker)
legacy1.Show
legacy1path = CurDir()

"Dave Peterson" wrote:

How did you declare legacy1path? Did you use "As String" or "As Variant" or did
you not declare that variable at all (and excel sees those undeclared variables
as Variants).

I'd declare it as a string or pass cstr(legacy1path) to the function.



Dave wrote:

Hi Chip,

Thanks for this. It does work if I use a direct link e.g. C:\ but if I want
to use a link referred to as legacy1path it doesn't work. I get an error of:

Compile error:
ByRef Argument type mismatch.

Any tips.

Thanks!
Dave

"Chip Pearson" wrote:

Try something like

Function FileCount(FolderName As String) As Long
On Error Resume Next
FileCount = CreateObject("Scripting.FileSystemObject"). _
GetFolder(FolderName).Files.Count
End Function

If you want to count files in nested subfolders, use
Function FileCount2(FolderName As String) As Long
On Error Resume Next
Dim FSO As Object
Dim N As Long
Dim FF As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FF = FSO.getfolder(FolderName)
N = FSO.getfolder(FF).Files.Count
If FF.subfolders.Count 0 Then
For Each FF In FF.subfolders
N = N + FileCount2(FF.Path)
Next FF
End If
FileCount2 = N
End Function


You can then call this function with code like the following.

Dim N As Long
N = FileCount("C:\Test")
If N = 24 Then
Debug.Print "OK: " & CStr(N) & " files"
Else
Debug.Print "Not OK: " & CStr(N) & " files"
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)




"Dave" wrote in message
...
Hi Everyone,

I am trying to do the following.

I have a folder e.g. C:\Folder1

I have this path defined as folderPath1.

I want to run a macro that will check that Folder1 has 24 files in it. If
not, a message box will display, prompting the user to check the folder
again.

Can anyone help?

Thanks
Dave


--

Dave Peterson

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
Hyperlink files same folder only? dahljam Excel Discussion (Misc queries) 2 July 3rd 07 07:00 PM
Copying all files in a folder to new folder michaelberrier Excel Discussion (Misc queries) 2 June 20th 06 05:35 AM
How to check if a file exists in an ftp folder LL Cool A Excel Discussion (Misc queries) 3 May 16th 06 09:22 PM
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
add same cell from all files in same folder Richard Varcoe Excel Discussion (Misc queries) 1 May 23rd 05 02:24 PM


All times are GMT +1. The time now is 12:54 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"