We help IT Professionals succeed at work.

VBScript - Capture Folder Contents Into Array

DWStovall
DWStovall asked
on
I need to capture the contents (filename and filesize only) of a folder into a two-dimensional array.

For example - from the path "C:\TEMP\FOLDER23", I would want the script to capture the names of all the files along with the sizes of all the files, and put the data into a two-dimensional array named"aFileNameSize".

I would hope that a query of the array would produce:

         strFileName = aFileNameSize(0,0)
         intFileSize = aFileNameSize(0,1)

         Msgbox strFileName & ", " & intFileSize

       Display:  GeeseInThePark.pdf, 3596238

Thank you...DavidS
Comment
Watch Question

Commented:
if hope i understand

path in line 1
strFolder = "C:\TEMP\FOLDER23"
Dim aFileNameSize(0, 1)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFolder = objFSO.GetFolder(strFolder)
Set colFiles = colFolder.Files

For Each strFile In colFiles
aFileNameSize(0,0) = strfile.name
aFileNameSize(0,1) = strfile.size
strFileName = aFileNameSize(0,0)
intFileSize = aFileNameSize(0,1)
MsgBox strFileName & ", " & intFileSize & " bytes"
Next

Open in new window

Author

Commented:
I apologize, in my effort to be succint in the original question, I failed to state that I would gather the data into an array in one part of the script, and use the array in another part of the script to compare the contents of one folder with another.

This is a great start, but I needed to capture all the files in the folder so that I can compare the contents of one folder against another.

I believe we will need a Redim on the array with a "Preserve", but I have no experience in arrays.  I know it isn't difficult, but I'm pressed for time and need something that works with having to "trial-and-error" syntax.

Thank you....
Bill PrewTest your restores, not your backups...
Top Expert 2016
Commented:
Not sure exactly how you will use the array(s), but you might want to consider a dictionary object.  If I'm reading you right, you might want to compare lists of files by name, and match the names to check the sizes.  That will be hard to do in two arrays, if a file has been added or removed to one of the arrays, then the indexes won't match up.  A dictionary object could have the file name as the element hey, and then the size as it's value.  Then you can reference a file from the list via it's name rather than having to search the entire array for it.  Let me know more about how you intend to process the array data and I can advise further on this approach, if it makes sense.

~bp

Author

Commented:
billprew,

Thanks for the "Dictionary" suggestion.  I looked at it, but it didn't seem as functional as the multidimensional array.

Here's what I'm trying to do.

A group of our laptop users travel with a set of files in a specific folder.  When they return, they have the option of copying/backing-up those files to a quota-limited network folder.  Because of the quota limitation on the folder, the users need to know in advance if they will go over their quota before attampting to copy/backup the files.

What I intended to do is capture the contents of the quota-limited destination folder (filename, size) and compare that to the local folder which the user intends to copy.  In the comparison, we will first build a intSize variable of all the items already in the destination folder.  Then as we compare with the items in the source folder (determining if the items in the source are changed (or altogether new) we will add or subtract from the intSize variable to determine if we will exceed the known quota limit.

I hope that makes sense.
Top Expert 2010

Commented:
Hello DWStovall,

With respect, you can't necessarily tell from comparing file name and size whether the files are different.

I can update a file to be the same exact size as before, but have different content.

I should think you would do better with commercial back-up software here...

Regards,

Patrick

Author

Commented:
matthewspatrick:  you are absolutely correct; however, considering that the files at the destination folder will be overwritten by the files in the source folder, the point is moot.

all:  I have reconsidered the process and realized that I don't need to bother with arrays at all.  Here what I decided to do:
Dim strDestFolder, strSourceFolder, objFSO, objSourceFolder
Dim objDestFolder, objFile, intDestSize, strOverallSize 
Dim objDestFile, objSourceFile, colSourceFiles, colDestFiles

strOverallSize = 0
strSourceFolder = "C:\TEMP1"
strDestFolder = "C:\TEMP2"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSourceFolder = objFSO.GetFolder(strSourceFolder)
Set objDestFolder = objFSO.GetFolder(strDestFolder)
Set colSourceFiles = objSourceFolder.Files
Set colDestFiles = objDestFolder.Files

'The object is to determine if copying the total volume of
'the source folder into the destination folder would
'exceed the quota limits of the destination folder, considering
'that there may be new files in the source and old files in the
'destination and that some in the source might over-write some
'in the destination...

'First we get the size of all in the source folder
For Each objSourceFile In colSourceFiles
   strOverallSize = strOverallSize + objSourceFile.Size
Next

'Next we go through all files in the destination folder
For Each objDestFile In colDestFiles
    binFileMatch = vbFalse
    'We compare file looking for a name match
    For Each objSourceFile In colSourceFiles
        If objDestFile.Name = objSourceFile.Name Then
           binFileMatch = vbTrue
        End If
    Next
    'If we don't match the destination file with a source file,
    'we add the size of the destination file.
    If Not binFileMatch Then 
       strOverallSize = strOverallSize + objDestFile.Size
    End If
 Next
 MsgBox strOverallSize

Open in new window

Author

Commented:
yehudaha - provided an answere to my question.

billprew - got me to thinking about a different solution.

I then came up with the means to resolve the issue.

Thank you all for the help...

DavidS
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Here's a suggestion for a small improvement in accuracy.

Making the attached change will yield slightly more accurate results in the cases where the new source file to be copied is smaller than the existing destination file.  Basically when we find a matching file name, if the new size is smaller than the old size, then we compute the difference in file sizes and subtract that from the total size.  This is more accurate since the new file will replace the old file when copied, thus freeing up the extra space.

Hope this helps,

~bp
'We compare file looking for a name match
    For Each objSourceFile In colSourceFiles
        If objDestFile.Name = objSourceFile.Name Then
           binFileMatch = vbTrue
           If objDestFile.Size > objSourceFile.Size Then
              strOverallSize = strOverallSize - (objDestFile.Size - objSourceFile.Size)
           End If
        End If
    Next

Open in new window

Author

Commented:
billprew:  Thanks for going the extra distance.