![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Quote: > is set in function four. If there are any questions let me know. Thanks 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 Specs![]() |
| | #3 (permalink) |
| | 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 Quote: > is set in function four. If there are any questions let me know. Thanks > 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 Specs![]() |
| | #4 (permalink) |
| | 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 '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 Specs![]() |
| | #5 (permalink) |
| | 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 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 Specs![]() |
| | #6 (permalink) |
| | 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 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 Specs![]() |
| | #7 (permalink) |
| | 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. 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 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") object. Quote: Quote: >> Set objWritetarget = objWriteFSO.CreateTextFile("share_size_results.txt", >> ForWriting) >> >> While objReadFile.AtEndOfStream <> True while/wend, which is deprecated. Quote: Quote: >> RdLine = objReadFile.Readline >> RdLineArray = split(RdLine,", ") >> RdLineName = RdLineArray(0) >> RdLinePath = RdLineArray(1) Quote: Quote: >> StartProcess RdLinePath, RdLineName >> Wend >> objReadFile.close >> objWritetarget.close >> >> Function StartProcess(SharePath,ShareName) 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) 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 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 Specs![]() |
![]() |
| 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 | |||