Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Clarity on VBS code handling

Clarity on VBS code handling

Clarity on VBS code handling

Hi all

I'm brand new to VBScript, but came across this forum during my search for help on finding a way to copy a file automatically.

Anyway, I found that (thanks to people here), but I'm curious about one thing. Why is it, in this portion of code:

CODE --> VBScript

for each g in ofolder.files
    if strcomp(left(g.name,2),"Le",1)=0 then
        objFso.copyfile g.path,flDestination,true 

Can I replace the 'g' with 'f' or any other letter and it still work? How does it know that when I'm saying 'for each g' that I mean each file? Does it just assume whatever letter/word is there is a nickname for each file within the collection I'm referring to?

Sorry if this is a stupid question, but I don't get it, unless what I said above is right!

Thank you!

RE: Clarity on VBS code handling

In that statement, g is a variable name that gets replaced with each item in the "ofolder.files" collection. You can use any name you want for a variable, like g, or f, or ThisFileName, or anything that isn't a reserved word in vbscript.


>How does it know that when I'm saying 'for each g' that I mean each file?

Because presumably, ofolder is a "FileSystem Object" folder object. Set something like this:


Set fso = CreateObject("Scripting.FileSystemObject")
Set ofolder = fso.GetFolder("C:\Data\MyFolder") 

FSO Folder objects contain a "Files" property (among many others), which is a collection of file objects in that folder

RE: Clarity on VBS code handling

Thanks guitarzan - great points that I can understand.

I think this also revealed my lack of knowledge with 'for each' because I was still wondering (and kind of still am) why you can just throw a variable after 'For each...' without having previously declared it in the normal 'Dim' fashion. Is that because the data-type would be a variant, and undeclared data-types, by default, are variant?

Thanks again!

RE: Clarity on VBS code handling

You SHOULD declare all variables with a Dim statement for a variety of reasons, but unless you use the line Option Explicit as the first line of your script, you are not FORCED to declare it. I definitely recommend using Option Explicit. If you use it, and you have a typo in a variable name, you will get a "Variable is undefined" error (and you have the ability to correct it). If you don't use Option Explicit, your code will think the misspelled variable is simply a new variable, giving you unexpected results.

RE: Clarity on VBS code handling

Also, all variables in VBScript are variants... depending on the value they have, they will be one of a number of Subtypes, which if needed, can be converted to other subtypes using one of the conversion functions, such as CInt(), CCur(), CDbl() (see the chart on this page for a list)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close