Link to home
Start Free TrialLog in
Avatar of DWStovall
DWStovallFlag for United States of America

asked on

VBScript - Capture Folder Contents Into Array

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
ASKER CERTIFIED SOLUTION
Avatar of yehudaha
yehudaha
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DWStovall

ASKER

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....
SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
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

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
Avatar of Bill Prew
Bill Prew

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

billprew:  Thanks for going the extra distance.