RSS

VBScript: object not found, but its there… I am sure.

16 Oct

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?

  1. set f = fso.getfile(“c:\temp\myfile.txt”)
  2. newdir = “c:\temp2”
  3. If Not fso.FileExists( newdir & “\” & f.name) Then
  4. fso.MoveFile f.path, newdir
  5. l.writeline now & “: ” & “Moved file = ” & f.name & “, To = ” & newdir
  6. 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
Advertisements
 
Leave a comment

Posted by on October 16, 2012 in Scripts, VBScripts

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: