Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FSO not a collection? 1

Status
Not open for further replies.

tinyang

MIS
Jun 7, 2002
4
US
Hello All!
I have run into some difficulty getting a file collection to be
recognized as a collection in a script. In my logfile, I keep getting a
"An error occured while processing. 451, Object not a collection.
Stopped on error. 451, Object not a collection" error, but I don't
understand why. See procedure code below. Any input would be greatly
appreciated.

Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFS.GetFolder(strDestOrig)
' Set pointer to files in the folder
Set objFiles = objFolder.Files

On error resume next

For each objFile in objFiles 'this is where I get the error.
nTotalFile = nTotalFile + 1
strOldFile = mid(objFile.Path, len(strSrcOrig) + 1)
strNewFile = strDestOrig & strOldFile
If objFS.FileExists(strNewFile) Then
' File exists on target. Check dates
Set objDestFile = objFS.GetFile(strNewFile)
If objFile.DateLastModified > objDestFile.DateLastModified Then
' Source is newer
nFilesCopied = nFilesCopied + 1
' Turn off Readonly attr
If objDestFile.Attributes and 1 Then
objDestFile.Attributes = objDestFile.Attributes - 1
End if
' Turn of Hidden attr
If objDestFile.Attributes and 2 Then
objDestFile.Attributes = objDestFile.Attributes - 2
End if
' Turn of System attr
If objDestFile.Attributes and 4 Then
objDestFile.Attributes = objDestFile.Attributes - 4
End if
objLogFile.WriteLine("Updated -> " & objFile.Path & " " &
objFile.DateLastModified)
objFS.CopyFile objFile.Path, objDestFile.Path, True
Else
'Destination is not older
objLogFile.WriteLine("Skipped -> " & objFile.Path & " " &
objFile.DateLastModified)
End If
Else
'Doesn't exist on target
objLogFile.WriteLine("Added -> " & objFile.Path & " " &
objFile.DateLastModified)
nFilesCopied = nFilesCopied + 1
objFS.CopyFile objFile.Path, strNewFile, True
End if
' Errors?
If Err then
objLogFile.WriteLine("Stopped on error. " & Err.number &
Err.Description)

objLogFile.Close
Set objLogFile = Nothing
End if
Set objFS = Nothing
End if
Next
 
tinyang,

You are using two different variables.
strDestOrig and strSrcOrig


Set objFolder = objFS.GetFolder(strDestOrig)

strOldFile = mid(objFile.Path, len(strSrcOrig) + 1)


fengshui_1998

 
Thanks much, fengshui_1998. I fixed this variable inconsistency and still get the same error (must be multiple errors :( ). See anything else wrong with it?
 
Did you omit a portion of this script - I'm seeing several problems.

1) Where do you set objLogFile? I only see you writing to it and closing it.
2) There's an extra End If.
3) The line of code Set objFS = Nothing should be placed after the Next statement or else objFS will be discarded after processing the first file. Jon Hawkins
 
tinyang,

If you write out objFolder.Files.count, how many files does it see?

fengshui_1998

 
Greetings to both of you! To answer your questions, jonscott8: this is just a snipit out of my script (one function where I seem to get the error). I do my startup sub (including setting up the objLogFile) earlier in the script. Also, please point out the extra end if to me, as I do not see it. And I have moved the "Set ObjFS = Nothing" line to after the Next where it belongs, thanks to your keen eye. See anything else? I still seem to be missing something.
Fengshui_1998: It counts 110 files, which is correct as I have double checked it in explorer. But I'm still not sure what my problem is. Any ideas?

Thanks to both of you for pointing out my mistakes to me and having patience with them. I'm still kinda new at this.
 
kimbly,

What are you trying to do with these two lines of code or what would you like to do?

strOldFile = mid(objFile.Path, len(strSrcOrig) + 1)
strNewFile = strDestOrig & strOldFile

fengshui_1998
 
Fengshui,

I am taking the current file (objfile) in the strSrcOrig directory (F:\My Documents) and creating a new string = to strOldFile which will list the objFile.Path while returning the length of strSrcOrig + 1 (I think...lol)
then combining strDestOrig and strOldFile = to strNewFile which will become the path for the file when it is copied over to strDestOrig or the destination directory.

What this enables the script to do is to copy the exact file and directory structure from the source to the destination, but only if it needs to be updated. See what I mean?
 
In the snippet above, there are 6 If statements and 7 End If statements. Jon Hawkins
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top