This is a simple VB logic, it checks if a file exists is another directory can move it there if not. It also logs an entry regarding the move. However, could you spot any problem with this?
- set f = fso.getfile(“c:\temp\myfile.txt”)
- newdir = “c:\temp2”
- If Not fso.FileExists( newdir & “\” & f.name) Then
- fso.MoveFile f.path, newdir
- l.writeline now & “: ” & “Moved file = ” & f.name & “, To = ” & newdir
- End If
When this is run, it returns an error “Object Not Found” at line 5.
It took me awhile to figure it out although it should have been obvious.
The error occurs at line 5 at “f.name” object. The problem is that in line 4, the file object is moved to a new location, but in line 5 we ended up reference a stale object or an object that is no more there, its been moved!
So the correct way to fix is to save the f.name into a variable (e.g. oldfile) and reference in line 5 such as:
l.writeline now & ": " & "Moved file = " & oldfile & ", To = " & newdir