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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clarity on VBS code handling

Status
Not open for further replies.

reyreyreyes

Technical User
Sep 8, 2015
2
GB
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:
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!



 
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:

Code:
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
 
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!
 
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.
 
Also, all variables in VBScript are variants... depending on the value they have, they will be one of a number of [link
]Subtypes[/url], which if needed, can be converted to other subtypes using one of the conversion functions, such as CInt(), CCur(), CDbl() (see [link itknowledgeexchange.techtarget.com/vbscript-systems-administrator/converting-variable-types-in-vbscript-from-one-type-another-type/]the chart[/url] on this page for a list)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top