I can't even count

Hi, I'm trying to count the number of saved text files that have been saved with the date and time as name.
dlgCom.Filename = Format(Now, "YYYYMMDD HHMMSS")

This is the code I'm working on for the count.

Private Sub NumTrans()
    Dim i As Integer
    Dim ThisFolder As Integer
   
    Set ThisFolder = FSys.GetFolder("A:\Transact")
    Set NumTrans = ThisFolder.files
    For Each File In ThisFolder
            Call NumTrans(File.Count)
    Next
End Sub

I have tried a few variations but have not succeeded at all.
Many thanks for any help.
ned
Ned_KellyAsked:
Who is Participating?
 
hongjunConnect With a Mentor Commented:
Below will return the number of files in your folder. Note the modification done by me.

Private Function AllFiles(ByVal FullPath As String) As Integer
    Dim oFs As New FileSystemObject
    Dim oFolder As Folder
    Dim oFile As File
    Dim intCount As Integer
   
    intCount = 0
    If oFs.FolderExists(FullPath) Then
        Set oFolder = oFs.GetFolder(FullPath)
     
        For Each oFile In oFolder.Files
          intCount = intCount + 1
        Next
    End If
   
    AllFiles = intCount
ErrHandler:
    Set oFs = Nothing
    Set oFolder = Nothing
    Set oFile = Nothing
End Function

Private Sub Form_Load()
    MsgBox AllFiles("A:\Transact") & " files returned"
End Sub

hongjun
0
 
hongjunCommented:
Are you trying to count the number of files in A:\Transact?
If it is then below is the code.
Note that you would need to include Microsoft Scripting Runtime in your Project->References. Below will return all files in yout folder.


Private Function AllFiles(ByVal FullPath As String) As Collection
'***************************************************
'PURPOSE: Returns all files in a folder using
'the FileSystemObject

'PARAMETER: FullPath = FullPath to folder for
'which you want all files

'RETURN VALUE: An array containing a list of
'all file names in FullPath, or a 1-element
'array with an empty string if FullPath
'does not exist or it has no files

'REQUIRES: Reference to Micrsoft Scripting
'          Runtime

'EXAMPLE:

'Dim sFiles() as string
'dim lCtr as long
'sFiles = AllFiles("C:\Windows\System")
'For lCtr = 0 to Ubound(sFiles)
'  Debug.Print sfiles(lctr)
'Next

'REMARKS:  The FileSystemObject does not
'Allow for the use of wild cards (e.g.,
'*.txt.)  If this is what you need, see
'http://wwww.freevbcode.com/ShowCode.asp?ID=1331
'************************************************

Dim oFs As New FileSystemObject
Dim oFolder As Folder
Dim oFile As File
Dim colFiles As New Collection

If oFs.FolderExists(FullPath) Then
    Set oFolder = oFs.GetFolder(FullPath)
 
    For Each oFile In oFolder.Files
      colFiles.Add oFile.Name
    Next
End If

Set AllFiles = colFiles
ErrHandler:
    Set oFs = Nothing
    Set oFolder = Nothing
    Set oFile = Nothing
End Function

Private Sub Form_Load()
    Dim colFiles As Collection
    Dim strFile As Variant
   
    Set colFiles = AllFiles("A:\Transact")
    For Each strFile In colFiles
        MsgBox strFile
    Next
   
    Set colFiles = Nothing
End Sub

hongjun
0
 
wileecoyCommented:
fyi ned kelly,

If you don't want to add the reference the FileSystemObject (Microsoft Scripting Runtime) you can do it all programmatically....



Private Function AllFiles(ByVal FullPath As String) As Integer
    Dim oFs As Object
    Set oFs = CreateObject("Scripting.FileSystemObject")
    Dim oFolder As Object
    Dim oFile As Object
    Dim intCount As Integer
   
   intCount = 0
   If oFs.FolderExists(FullPath) Then
       Set oFolder = oFs.GetFolder(FullPath)
   
       For Each oFile In oFolder.Files
         intCount = intCount + 1
       Next
   End If
   
   AllFiles = intCount
ErrHandler:
   Set oFs = Nothing
   Set oFolder = Nothing
   Set oFile = Nothing
End Function

Private Sub Form_Load()
   MsgBox AllFiles("A:\Transact") & " files returned"
End Sub

0
 
Ned_KellyAuthor Commented:
Hi Honqjun, thanks for your help, it works well.
Ned
0
 
hongjunCommented:
wileecoy suggests using runtime setting references and that is another alternative. But for a beginner, I would suggest you adding references so that when you do a ., you will get the available methods, etc. This would be more helpful that way.

Glad that I can help here.

hongjun
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.