Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Passing of variables

Reply
 
Old 08-08-2008   #1 (permalink)
MacMan0295


 
 

Passing of variables

All,

I am trying to call a variable from a function. Here is how my script is
setup.

<...code here...>
var1 = value
one(var1)
write var2

Function one(var1)
<...code here...>
two(var1)
End Function

Function two(var1)
<...code here...>
three(var1)
End Function

Function three(var1)
<...code here...>
four(var1)
End Function

Function four(var1)
<...code here...>
var2 = something
End Function

Hopefully this makes sense. I want to be able to write to a file var2 which
is set in function four. If there are any questions let me know. Thanks

Craig

My System SpecsSystem Spec
Old 08-08-2008   #2 (permalink)
Todd Vargo


 
 

Re: Passing of variables

"MacMan0295" <MacMan0295@xxxxxx> wrote in message
news:A59A4D48-1E98-4950-819E-EC650C4A0AB1@xxxxxx
Quote:

> All,
>
> I am trying to call a variable from a function. Here is how my script is
> setup.
>
> <...code here...>
> var1 = value
> one(var1)
> write var2
>
> Function one(var1)
> <...code here...>
> two(var1)
> End Function
>
> Function two(var1)
> <...code here...>
> three(var1)
> End Function
>
> Function three(var1)
> <...code here...>
> four(var1)
> End Function
>
> Function four(var1)
> <...code here...>
> var2 = something
> End Function
>
> Hopefully this makes sense. I want to be able to write to a file var2
which
Quote:

> is set in function four. If there are any questions let me know. Thanks
You need to declare var2 at the top of your code.

Dim var1, var2


Note, you are using function routines but not returning the values through
the functions. The following shows that the variables are not needed to
return a result. As the value is passed to each function, the result is
returned back to the calling function.


Wscript.Echo one("myvalue")

Function one(inp1)
one = two(inp1)
End Function

Function two(inp2)
two = three(inp2)
End Function

Function three(inp3)
three = four(inp3)
End Function

Function four(inp4)
four = inp4 & " is " & Len(inp4) & " characters long"
End Function

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

My System SpecsSystem Spec
Old 08-08-2008   #3 (permalink)
Anthony Jones


 
 

Re: Passing of variables

"MacMan0295" <MacMan0295@xxxxxx> wrote in message
news:A59A4D48-1E98-4950-819E-EC650C4A0AB1@xxxxxx
Quote:

> All,
>
> I am trying to call a variable from a function. Here is how my script is
> setup.
>
> <...code here...>
> var1 = value
> one(var1)
> write var2
>
> Function one(var1)
> <...code here...>
> two(var1)
> End Function
>
> Function two(var1)
> <...code here...>
> three(var1)
> End Function
>
> Function three(var1)
> <...code here...>
> four(var1)
> End Function
>
> Function four(var1)
> <...code here...>
> var2 = something
> End Function
>
> Hopefully this makes sense. I want to be able to write to a file var2
which
Quote:

> is set in function four. If there are any questions let me know. Thanks
>
Dim var2

before all of that code would do it but I wouldn't recomend it:-

This is a better approach:-

<...code here...>
var1 = value
one(var1)
write one(var1)

Function one(var1)
<...code here...>
one = two(var1)
End Function

Function two(var1)
<...code here...>
two = three(var1)
End Function

Function three(var1)
<...code here...>
three = four(var1)
End Function

Function four(var1)
<...code here...>
four = something
End Function


--
Anthony Jones - MVP ASP/ASP.NET


My System SpecsSystem Spec
Old 08-08-2008   #4 (permalink)
Pegasus \(MVP\)


 
 

Re: Passing of variables


"MacMan0295" <MacMan0295@xxxxxx> wrote in message
news:A59A4D48-1E98-4950-819E-EC650C4A0AB1@xxxxxx
Quote:

> All,
>
> I am trying to call a variable from a function. Here is how my script is
> setup.
>
> <...code here...>
> var1 = value
> one(var1)
> write var2
>
> Function one(var1)
> <...code here...>
> two(var1)
> End Function
>
> Function two(var1)
> <...code here...>
> three(var1)
> End Function
>
> Function three(var1)
> <...code here...>
> four(var1)
> End Function
>
> Function four(var1)
> <...code here...>
> var2 = something
> End Function
>
> Hopefully this makes sense. I want to be able to write to a file var2
> which
> is set in function four. If there are any questions let me know. Thanks
>
> Craig
You must set the function name to the desired variable like so:

'Main program
wscript.echo four(xxx)

Function four(var1)
<...code here...>
var2 = something
four = var2 '*** This is the line you're missing.
End Function

By the way, it's a bad idea to use the same variable name in
your main program and in your function. It will cause confusion
at best and unexpected results at worst. Instead of writing

wscript square(x)

sub square(x)
square=x * x
end function

you should write

wscript square(a)

sub square(x)
square=x * x
end function


My System SpecsSystem Spec
Old 08-11-2008   #5 (permalink)
MacMan0295


 
 

Re: Passing of variables

I see where you are going with this, but I am unable to figure how to apply
to my script. Here is the script that I am building. Can you please let me
know how to appy to this script? Thanks

--------------------------------------------------------------------------------------------

Dim ObjFso,objReadFSO,objWriteFSO,OldDateLastAccessed
'On Error Resume Next
Const ForReading = 1
Const ForWriting = 2
i = 1
OldDateLastAccessed = DateValue("1/1/1900")

Set objReadFSO = CreateObject("Scripting.FileSystemObject")
Set objReadFile = objReadFSO.OpenTextFile("results2.txt", ForReading)
Set objWriteFSO = CreateObject("Scripting.FileSystemObject")
Set objWritetarget = objWriteFSO.CreateTextFile("share_size_results.txt",
ForWriting)

While objReadFile.AtEndOfStream <> True
RdLine = objReadFile.Readline
RdLineArray = split(RdLine,", ")
RdLineName = RdLineArray(0)
RdLinePath = RdLineArray(1)
StartProcess RdLinePath, RdLineName
Wend
objReadFile.close
objWritetarget.close

Function StartProcess(SharePath,ShareName)
Dim StrFolderName

StrFolderName = SharePath
Set ObjFso = CreateObject("Scripting.FileSystemObject")

Call ListSubFoldersAndFiles(StrFolderName)
WScript.Echo i & ". " & ShareName & " - " & SharePath & " - " &
OldDateLastAccessed
objWritetarget.writeline ShareName & "," & SharePath & "," &
OldDateLastAccessed
i = i + 1
End Function

Function ListSubFoldersAndFiles(StrSubFolderPath)
Dim ObjFolder
Dim ObjFileCollection
Dim ObjSubFolderCollection
Dim ObjSubFolder
Dim ObjFile

Set ObjFolder=ObjFso.GetFolder(StrSubFolderPath)
Set ObjFileCollection=ObjFolder.Files
For Each ObjFile In ObjFileCollection
GetLastAccessed(ObjFile.Path)
Next

Set ObjSubFolderCollection=ObjFolder.SubFolders
For Each ObjSubFolder In ObjSubFolderCollection
Call ListSubFoldersAndFiles(ObjSubFolder.Path)
Next
End Function

Function GetLastAccessed(FilePath)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(FilePath)

NewDateLastAccessed = DateValue(objFile.DateLastModified)
newValue = DateDiff("m",OldDateLastAccessed,NewDateLastAccessed)

If OldDateLastAccessed < NewDateLastAccessed then
OldDateLastAccessed = NewDateLastAccessed
End If

' Wscript.Echo "Date last accessed: " & objFile.DateLastAccessed
' Wscript.Echo "Date last modified: " & objFile.DateLastModified
End Function

--------------------------------------------------------------------------------------------

"Todd Vargo" wrote:
Quote:

> "MacMan0295" <MacMan0295@xxxxxx> wrote in message
> news:A59A4D48-1E98-4950-819E-EC650C4A0AB1@xxxxxx
Quote:

> > All,
> >
> > I am trying to call a variable from a function. Here is how my script is
> > setup.
> >
> > <...code here...>
> > var1 = value
> > one(var1)
> > write var2
> >
> > Function one(var1)
> > <...code here...>
> > two(var1)
> > End Function
> >
> > Function two(var1)
> > <...code here...>
> > three(var1)
> > End Function
> >
> > Function three(var1)
> > <...code here...>
> > four(var1)
> > End Function
> >
> > Function four(var1)
> > <...code here...>
> > var2 = something
> > End Function
> >
> > Hopefully this makes sense. I want to be able to write to a file var2
> which
Quote:

> > is set in function four. If there are any questions let me know. Thanks
>
> You need to declare var2 at the top of your code.
>
> Dim var1, var2
>
>
> Note, you are using function routines but not returning the values through
> the functions. The following shows that the variables are not needed to
> return a result. As the value is passed to each function, the result is
> returned back to the calling function.
>
>
> Wscript.Echo one("myvalue")
>
> Function one(inp1)
> one = two(inp1)
> End Function
>
> Function two(inp2)
> two = three(inp2)
> End Function
>
> Function three(inp3)
> three = four(inp3)
> End Function
>
> Function four(inp4)
> four = inp4 & " is " & Len(inp4) & " characters long"
> End Function
>
> --
> Todd Vargo
> (Post questions to group only. Remove "z" to email personal messages)
>
>
My System SpecsSystem Spec
Old 08-11-2008   #6 (permalink)
MacMan0295


 
 

Re: Passing of variables

I got it to work. I made sure that I Dimmed the value at the top and I moved
the "OldDateLastAccessed = DateValue("1/1/1900")" within the 1st function I
was trying to write the final variable to. Then I just reset the variable
"OldDateLastAccessed" to the new value in the 4th function.

Thanks for all the help. If anyone sees a problem with the way I am doing
this I am open for suggestions.

Craig

"MacMan0295" wrote:
Quote:

> I see where you are going with this, but I am unable to figure how to apply
> to my script. Here is the script that I am building. Can you please let me
> know how to appy to this script? Thanks
>
> --------------------------------------------------------------------------------------------
>
> Dim ObjFso,objReadFSO,objWriteFSO,OldDateLastAccessed
> 'On Error Resume Next
> Const ForReading = 1
> Const ForWriting = 2
> i = 1
> OldDateLastAccessed = DateValue("1/1/1900")
>
> Set objReadFSO = CreateObject("Scripting.FileSystemObject")
> Set objReadFile = objReadFSO.OpenTextFile("results2.txt", ForReading)
> Set objWriteFSO = CreateObject("Scripting.FileSystemObject")
> Set objWritetarget = objWriteFSO.CreateTextFile("share_size_results.txt",
> ForWriting)
>
> While objReadFile.AtEndOfStream <> True
> RdLine = objReadFile.Readline
> RdLineArray = split(RdLine,", ")
> RdLineName = RdLineArray(0)
> RdLinePath = RdLineArray(1)
> StartProcess RdLinePath, RdLineName
> Wend
> objReadFile.close
> objWritetarget.close
>
> Function StartProcess(SharePath,ShareName)
> Dim StrFolderName
>
> StrFolderName = SharePath
> Set ObjFso = CreateObject("Scripting.FileSystemObject")
>
> Call ListSubFoldersAndFiles(StrFolderName)
> WScript.Echo i & ". " & ShareName & " - " & SharePath & " - " &
> OldDateLastAccessed
> objWritetarget.writeline ShareName & "," & SharePath & "," &
> OldDateLastAccessed
> i = i + 1
> End Function
>
> Function ListSubFoldersAndFiles(StrSubFolderPath)
> Dim ObjFolder
> Dim ObjFileCollection
> Dim ObjSubFolderCollection
> Dim ObjSubFolder
> Dim ObjFile
>
> Set ObjFolder=ObjFso.GetFolder(StrSubFolderPath)
> Set ObjFileCollection=ObjFolder.Files
> For Each ObjFile In ObjFileCollection
> GetLastAccessed(ObjFile.Path)
> Next
>
> Set ObjSubFolderCollection=ObjFolder.SubFolders
> For Each ObjSubFolder In ObjSubFolderCollection
> Call ListSubFoldersAndFiles(ObjSubFolder.Path)
> Next
> End Function
>
> Function GetLastAccessed(FilePath)
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.GetFile(FilePath)
>
> NewDateLastAccessed = DateValue(objFile.DateLastModified)
> newValue = DateDiff("m",OldDateLastAccessed,NewDateLastAccessed)
>
> If OldDateLastAccessed < NewDateLastAccessed then
> OldDateLastAccessed = NewDateLastAccessed
> End If
>
> ' Wscript.Echo "Date last accessed: " & objFile.DateLastAccessed
> ' Wscript.Echo "Date last modified: " & objFile.DateLastModified
> End Function
>
> --------------------------------------------------------------------------------------------
>
> "Todd Vargo" wrote:
>
Quote:

> > "MacMan0295" <MacMan0295@xxxxxx> wrote in message
> > news:A59A4D48-1E98-4950-819E-EC650C4A0AB1@xxxxxx
Quote:

> > > All,
> > >
> > > I am trying to call a variable from a function. Here is how my script is
> > > setup.
> > >
> > > <...code here...>
> > > var1 = value
> > > one(var1)
> > > write var2
> > >
> > > Function one(var1)
> > > <...code here...>
> > > two(var1)
> > > End Function
> > >
> > > Function two(var1)
> > > <...code here...>
> > > three(var1)
> > > End Function
> > >
> > > Function three(var1)
> > > <...code here...>
> > > four(var1)
> > > End Function
> > >
> > > Function four(var1)
> > > <...code here...>
> > > var2 = something
> > > End Function
> > >
> > > Hopefully this makes sense. I want to be able to write to a file var2
> > which
Quote:

> > > is set in function four. If there are any questions let me know. Thanks
> >
> > You need to declare var2 at the top of your code.
> >
> > Dim var1, var2
> >
> >
> > Note, you are using function routines but not returning the values through
> > the functions. The following shows that the variables are not needed to
> > return a result. As the value is passed to each function, the result is
> > returned back to the calling function.
> >
> >
> > Wscript.Echo one("myvalue")
> >
> > Function one(inp1)
> > one = two(inp1)
> > End Function
> >
> > Function two(inp2)
> > two = three(inp2)
> > End Function
> >
> > Function three(inp3)
> > three = four(inp3)
> > End Function
> >
> > Function four(inp4)
> > four = inp4 & " is " & Len(inp4) & " characters long"
> > End Function
> >
> > --
> > Todd Vargo
> > (Post questions to group only. Remove "z" to email personal messages)
> >
> >
My System SpecsSystem Spec
Old 08-14-2008   #7 (permalink)
Al Dunbar


 
 

Re: Passing of variables


"MacMan0295" <MacMan0295@xxxxxx> wrote in message
news:B3CB68FA-455C-41B8-8F4D-DE20CEDDA41A@xxxxxx
Quote:

>I got it to work. I made sure that I Dimmed the value at the top and I
>moved
> the "OldDateLastAccessed = DateValue("1/1/1900")" within the 1st function
> I
> was trying to write the final variable to. Then I just reset the variable
> "OldDateLastAccessed" to the new value in the 4th function.
>
> Thanks for all the help. If anyone sees a problem with the way I am doing
> this I am open for suggestions.
Below are a few comments for you, but they refer more to scripting practice
in general than to your specific application..
Quote:

> Craig
>
> "MacMan0295" wrote:
>
Quote:

>> I see where you are going with this, but I am unable to figure how to
>> apply
>> to my script. Here is the script that I am building. Can you please let
>> me
>> know how to appy to this script? Thanks
>>
>> --------------------------------------------------------------------------------------------
>>
>> Dim ObjFso,objReadFSO,objWriteFSO,OldDateLastAccessed
>> 'On Error Resume Next
>> Const ForReading = 1
>> Const ForWriting = 2
>> i = 1
it is dangerous to use a global variable (especially one with a name you
might inadvertently use elsewhere) in a function that relies on its being
initialized in the main line program.
Quote:
Quote:

>> OldDateLastAccessed = DateValue("1/1/1900")
>>
>> Set objReadFSO = CreateObject("Scripting.FileSystemObject")
>> Set objReadFile = objReadFSO.OpenTextFile("results2.txt", ForReading)
>> Set objWriteFSO = CreateObject("Scripting.FileSystemObject")
There is never a need to create more than one instance of the file system
object.
Quote:
Quote:

>> Set objWritetarget = objWriteFSO.CreateTextFile("share_size_results.txt",
>> ForWriting)
>>
>> While objReadFile.AtEndOfStream <> True
better to use do/loop with a while condition on the do rather than
while/wend, which is deprecated.
Quote:
Quote:

>> RdLine = objReadFile.Readline
>> RdLineArray = split(RdLine,", ")
>> RdLineName = RdLineArray(0)
>> RdLinePath = RdLineArray(1)
The above line will likely fail if a line in the file has no comma.
Quote:
Quote:

>> StartProcess RdLinePath, RdLineName
>> Wend
>> objReadFile.close
>> objWritetarget.close
>>
>> Function StartProcess(SharePath,ShareName)
Although it is perfectly OK to invoke functions as if they were subs, your
doing so here suggests you may not appreciate the difference between a SUB
and a FUNCTION. As some have tried to point out to you, the main difference
is that a function is capable of returning a value to the calling routine by
assigning it to its own name, i.e.:

result = add2(12,23)

function add2(first, second)
add2 = first + second
end function
Quote:
Quote:

>> Dim StrFolderName
>>
>> StrFolderName = SharePath
>> Set ObjFso = CreateObject("Scripting.FileSystemObject")
>>
>> Call ListSubFoldersAndFiles(StrFolderName)
here you use an explicit call statement, yet elsewhere you use the implicit
form, i.e.:

StartProcess RdLinePath, RdLineName

since these are interchangeable, the mixture of forms creates a bit of
ambiguity as to meaning.
Quote:
Quote:

>> WScript.Echo i & ". " & ShareName & " - " & SharePath & " - " &
>> OldDateLastAccessed
>> objWritetarget.writeline ShareName & "," & SharePath & "," &
>> OldDateLastAccessed
>> i = i + 1
>> End Function
>>
>> Function ListSubFoldersAndFiles(StrSubFolderPath)
>> Dim ObjFolder
>> Dim ObjFileCollection
>> Dim ObjSubFolderCollection
>> Dim ObjSubFolder
>> Dim ObjFile
>>
>> Set ObjFolder=ObjFso.GetFolder(StrSubFolderPath)
>> Set ObjFileCollection=ObjFolder.Files
>> For Each ObjFile In ObjFileCollection
>> GetLastAccessed(ObjFile.Path)
>> Next
>>
>> Set ObjSubFolderCollection=ObjFolder.SubFolders
>> For Each ObjSubFolder In ObjSubFolderCollection
>> Call ListSubFoldersAndFiles(ObjSubFolder.Path)
>> Next
>> End Function
>>
>> Function GetLastAccessed(FilePath)
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> Set objFile = objFSO.GetFile(FilePath)
>>
>> NewDateLastAccessed = DateValue(objFile.DateLastModified)
>> newValue = DateDiff("m",OldDateLastAccessed,NewDateLastAccessed)
>>
>> If OldDateLastAccessed < NewDateLastAccessed then
>> OldDateLastAccessed = NewDateLastAccessed
>> End If
>>
>> ' Wscript.Echo "Date last accessed: " & objFile.DateLastAccessed
>> ' Wscript.Echo "Date last modified: " & objFile.DateLastModified
>> End Function
Code written as above may become difficult to maintain. The main issue I see
is a lack of clarity as to the scope of the variables.

First, I would suggest that if you are going to DIM any variables, that you
DIM them all, and force yourself to do so with an Option Explicit statement.
If you are not going to DIM all variables, then I think you should not DIM
any of them.

Second, you should use global variables as sparingly as possible. These are
variable defined in your main program, or outside of any SUB or FUNCTION.
The problem is that they are accessible to the code in ALL of your SUBS and
FUNCTIONS, where an accidental reference can be a very hard problem to
identify or find.

The way I avoid global variables is to create a MAIN function (or sub) and
move all of the main-line program into it. This makes all of the previously
global variables local to the MAIN program, and inaccessible to the SUBs and
FUNCTIONs.

You will, of course, still need to have a main-line program in order to call
the MAIN program. This can also be a good place to define a global FSO
object to be used throughout the code, and constants such as forReading.
Here is a brief example of how your program above might be structured:

option explicit
dim FSO : set FSO = CreateObject("Scripting.FileSystemObject")
const forReading =
Const ForReading = 1
Const ForWriting = 2
CALL MAIN
wscript.quit

SUB MAIN
Dim ObjFso,objReadFSO,objWriteFSO,OldDateLastAccessed
... and etc.
END SUB
Function StartProcess(SharePath,ShareName)

... as before ...
end function
Function ListSubFoldersAndFiles(StrSubFolderPath)
... as before ...
end function

You will not be able to do this restructuring without making other changes -
one being the "i" variable which will no longer be global.

/Al
Quote:
Quote:

>>
>> --------------------------------------------------------------------------------------------
>>
>> "Todd Vargo" wrote:
>>
Quote:

>> > "MacMan0295" <MacMan0295@xxxxxx> wrote in message
>> > news:A59A4D48-1E98-4950-819E-EC650C4A0AB1@xxxxxx
>> > > All,
>> > >
>> > > I am trying to call a variable from a function. Here is how my
>> > > script is
>> > > setup.
>> > >
>> > > <...code here...>
>> > > var1 = value
>> > > one(var1)
>> > > write var2
>> > >
>> > > Function one(var1)
>> > > <...code here...>
>> > > two(var1)
>> > > End Function
>> > >
>> > > Function two(var1)
>> > > <...code here...>
>> > > three(var1)
>> > > End Function
>> > >
>> > > Function three(var1)
>> > > <...code here...>
>> > > four(var1)
>> > > End Function
>> > >
>> > > Function four(var1)
>> > > <...code here...>
>> > > var2 = something
>> > > End Function
>> > >
>> > > Hopefully this makes sense. I want to be able to write to a file
>> > > var2
>> > which
>> > > is set in function four. If there are any questions let me know.
>> > > Thanks
>> >
>> > You need to declare var2 at the top of your code.
>> >
>> > Dim var1, var2
>> >
>> >
>> > Note, you are using function routines but not returning the values
>> > through
>> > the functions. The following shows that the variables are not needed to
>> > return a result. As the value is passed to each function, the result is
>> > returned back to the calling function.
>> >
>> >
>> > Wscript.Echo one("myvalue")
>> >
>> > Function one(inp1)
>> > one = two(inp1)
>> > End Function
>> >
>> > Function two(inp2)
>> > two = three(inp2)
>> > End Function
>> >
>> > Function three(inp3)
>> > three = four(inp3)
>> > End Function
>> >
>> > Function four(inp4)
>> > four = inp4 & " is " & Len(inp4) & " characters long"
>> > End Function
>> >
>> > --
>> > Todd Vargo
>> > (Post questions to group only. Remove "z" to email personal messages)
>> >
>> >

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: Passing variables into a CMD command PowerShell
Passing variables to script PowerShell
passing parameters to an external program using PowerShell variables PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46