Search code examples
vbscriptwindows-7

Object Required: 'FSO.GetFolder(...).Size' 800A01A8


This is my code:

sourcePath = "C:\Users\USER\Desktop\source\*.*"
pastePath  = "C:\Users\USER\Desktop\dest\"

Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CopyFile sourcePath, pastePath
FSO.CopyFolder sourcePath, pastePath

Set sourceSize = FSO.GetFolder(FSO.GetParentFolderName(sourcePath)).Size
Set first_pasteSize = FSO.GetFolder(pastePath).Size
Do Until sourceSize + first_pasteSize = pasteSize
    pasteSize = FSO.GetFolder(pastePath).Size   
    duration = duration + 1
    WScript.Sleep 1000
Loop
MsgBox "File copied successfully." & vbCrLf & "The process took: " & duration _
    & " seconds.", 0+64, "Success!"

I'm trying to make a VBS that copies all the files and folders from a folder into another folder and tells you "File copied successfully." when finished. The problem is that it gives me an error even though I set FSO:

Line: 8; Error: Object Required: 'FSO.GetFolder(…).Size'; Code: 800A01A8


Edit:

The error disappears if I change the code to

sourcePath = "C:\Users\USER\Desktop\source\*.*"
pastePath  = "C:\Users\USER\Desktop\dest\"

Set FSO = CreateObject("Scripting.FileSystemObject")

sourceSize = FSO.GetFolder(FSO.GetParentFolderName(sourcePath)).Size
first_pasteSize = FSO.GetFolder(pastePath).Size

FSO.CopyFile sourcePath, pastePath
FSO.CopyFolder sourcePath, pastePath

Do Until sourceSize + first_pasteSize = pasteSize
    pasteSize = FSO.GetFolder(pastePath).Size   
    duration = duration + 1
    WScript.Sleep 1000
Loop
MsgBox "File copied successfully." & vbCrLf & "The process took: " & duration _
    & " seconds.", 0+64, "Success!"

but I have another problem now. I want to calculate the seconds it takes to copy the files, but each time I run the code it says 1 second.


Solution

  • As Hans Passant already pointed out you must not use the Set keyword for assigning values of primitive data types (like integer) to variables. The keyword must only be used for assigning objects to variables.

    The code from your updated question always reports the copying time as 1 second because now you calculate sourceSize and first_pasteSize first, then copy the files, then loop until the destination size is equal to the sum of source and destination folder size before the copy operation. Which is true after the first iteration of the loop, because the CopyFile and CopyFolder calls are synchronous, meaning they don't return before the operation is completed. IOW when you're entering the loop the copy operation is already finished.

    If you want to calculate the time copying the files/folders takes you should actually calculate the time:

    start = Now
    
    FSO.CopyFile sourcePath, pastePath
    FSO.CopyFolder sourcePath, pastePath
    
    duration = DateDiff("s", start, Now)
    

    Use the Timer function if you need millisecond precision:

    start = Timer
    
    FSO.CopyFile sourcePath, pastePath
    FSO.CopyFolder sourcePath, pastePath
    
    duration = Timer - start