Why f.size doesn't work? (VBScript)

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
OlgaFB
Pro Scripter
Posts: 58
Joined: Mon Nov 01, 2004 3:04 pm
Contact:

Why f.size doesn't work? (VBScript)

Post by OlgaFB » Sun Jan 23, 2005 3:06 pm

Hi,

I saw in a lot of examples and in the documentation as well, that when I want to know the file size in VBScript, I use f.size. Howeever, when I use it in my script, it gives an error.

The code is like this:

Dim fso, f, idxLet
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename, ForReading)
MsgBox f.size

The error generated:
438 Object doesn't support this property or method 'f.size'.

Why is that?

Thank you,
Olga.

User avatar
Captive
Macro Veteran
Posts: 213
Joined: Sun Oct 20, 2002 8:37 pm
Location: Colorado, USA

Post by Captive » Sun Jan 23, 2005 8:50 pm

Use the "GetFile" method to create an object that represents a file.
The "OpenTextFile" creates a "steam" object where you simply write, read or append to.


Try this:

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filename)
MsgBox f.size

OlgaFB
Pro Scripter
Posts: 58
Joined: Mon Nov 01, 2004 3:04 pm
Contact:

Post by OlgaFB » Mon Jan 24, 2005 8:12 am

Thank you, Marcus!

If I after this want to read from this file, should I perform any "Set Nothing" or "f.Close" to free this resource for the later use?

Thankyou very much for your help,
Olga.

User avatar
Captive
Macro Veteran
Posts: 213
Joined: Sun Oct 20, 2002 8:37 pm
Location: Colorado, USA

Post by Captive » Mon Jan 24, 2005 5:24 pm

For a TextStream, f.close should be fine for what you are doing, even if further down in the script you "Set f" as another object/file.

If it's a folder/file used by GetFile, you don't need to "close" it. You can continue to "Set f" as another object/file.

If you're concerned about your script running for a long time, and using too many resources, you can indeed:
Set f = Nothing

OlgaFB
Pro Scripter
Posts: 58
Joined: Mon Nov 01, 2004 3:04 pm
Contact:

Post by OlgaFB » Tue Jan 25, 2005 1:22 pm

Thank you, Captive,

Do you know may be if when I exit from a function, it's best if I do Set f Nothing, or it doesn't matter? I mean, if I exit a function, all the resources allocated to it should be freed right?

I ask because for example in JScript I read that even though assigning a new timeOut to a timer should free the old timer, it doesn't always happen (a bug), and thus it's better to always first clear the old timer first and only then to assign a new.

Thank you,
Olga.

User avatar
Captive
Macro Veteran
Posts: 213
Joined: Sun Oct 20, 2002 8:37 pm
Location: Colorado, USA

Post by Captive » Tue Jan 25, 2005 4:24 pm

From what I've read, Microsoft claim that the resources from any local variables are free'd when the function or subroutine ends.

OlgaFB
Pro Scripter
Posts: 58
Joined: Mon Nov 01, 2004 3:04 pm
Contact:

Post by OlgaFB » Tue Jan 25, 2005 4:31 pm

:)

thank you.

I hope someone having practicing it a lot can say how it works.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts