![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | recursion help request hi, I have a vbscript that I can not get to recurs properly. If the user enters '5' for input, the script should end. For ALL other input: invalid data returns to the inputbox (this works), and valid data should execute the corresponding sub-routines, and then return to the inputbox. I am unable to format this so far to get it to do this. I have tried loops and ended up with mostly endless loops. I have also tried to call (a 2nd call) the sub-procedure for the main menu (mainmenu()) at the end of the script, but it simply stops after the previous sub-routines finish. I have also tried to add the mainmenu() call at the end of each sub-routine for option 1thru4 to no avail, it still stops after each sub-routine. The inputbox and the use of subroutines for the selections 1-4 are required, so I can not re-write this entire thing. Any suggestions? the script: __________________________________________________________ Option Explicit Dim fso, objshl, textfile, title, choice, inputtest, program, exitnow Set fso = CreateObject("Scripting.FileSystemObject") Set objshl = CreateObject("WScript.Shell") textfile = "c:\scriptlog.txt" title = "Main Menu - Current Date: " & Date '************************* 'Main Processing Section * '************************* mainmenu() If choice = "1" Then Logfile() Powerpoint() ElseIf choice = "2" then Logfile() Excel() ElseIf choice = "3" Then Viewlogfile() ElseIf choice = "4" then Deletelogfile() End If '******************************************** 'Main Menu for player input and value testing '******************************************** Sub mainmenu() Do Until inputtest = "valid" choice = (InputBox(("1. Run PowerPoint") & vbCrLf & ("2. Run Excel")_ & vbCrLf & ("3. View Log File") & vbCrLf &_ ("4. Delete Log File") & vbCrLf & ("5. Quit") & vbCrLf &_ vbCrLf & vbTab & vbTab & vbTab & "Choose By Number",title)) If choice = "1" Then inputtest = "valid" program = "PowerPoint" ElseIf choice = "2" then inputtest = "valid" program = "Excel" ElseIf choice = "3" Then inputtest = "valid" ElseIf choice = "4" then inputtest = "valid" ElseIf choice = "5" Then WScript.Quit Else MsgBox("Your selection was not valid") End If Loop End Sub '********************************************** 'Create 'start' logfile text for option 1 and 2 '********************************************** Sub Logfile() Dim log If (fso.FileExists(textfile)) Then Set log = fso.OpenTextFile(textfile,8) log.WriteLine "-- -- -- -- -- -- -- -- -- -- -- --" log.WriteLine ((program) & " started on ") & Date log.WriteLine ((program) & " started at ") & Time log.WriteLine "-- -- -- --" log.close Else Set log = fso.OpenTextFile(textfile, 2, "true") log.WriteLine "-- -- -- -- -- -- -- -- -- -- -- --" log.WriteLine ((program) & " started on ") & Date log.WriteLine ((program) & " started at ") & Time log.WriteLine "-- -- -- --" log.close End If End Sub '************************************** 'Process tasks for option '1' selection '************************************** Sub Powerpoint() Dim log objshl.Run "powerpnt.exe",,True Set log = fso.OpenTextFile(textfile, 8) log.WriteLine "PowerPoint stopped on " & Date log.WriteLine "PowerPoint stopped at " & Time log.WriteLine "-- -- -- -- -- -- -- -- -- -- -- --" log.close() End Sub '************************************** 'Process tasks for option '2' selection '************************************** Sub Excel() Dim log objshl.Run "excel.exe",,True Set log = fso.OpenTextFile(textfile, 8) log.WriteLine "Excel stopped on " & Date log.WriteLine "Excel stopped at " & Time log.WriteLine "-- -- -- -- -- -- -- -- -- -- -- --" log.close() End Sub '************************************** 'Process tasks for option '3' selection '************************************** Sub Viewlogfile() Dim output, log Set log = fso.OpenTextFile(textfile, 1) If (fso.FileExists(textfile)) Then output = log.ReadAll log.Close MsgBox output Else MsgBox("The log file does not exist") End If End Sub '************************************** 'Process tasks for option '4' selection '************************************** Sub Deletelogfile() Dim confirm, file If Not (fso.FileExists(textfile)) Then MsgBox "There is no file to delete", 0 Else confirm = MsgBox("Are you sure you wish to delete this?", 4, "Confirm Selection") If confirm = vbYes Then fso.DeleteFile textfile Set file = fso.OpenTextFile(textfile, 2, "true") file.WriteLine "Logfile created on " & Date & " at " & Time file.close End if End If End Sub |
My System Specs![]() |
| | #2 (permalink) |
| | Re: recursion help request "Vgolfmaster" <Vgolfmaster@xxxxxx> wrote in message news:811E3A53-97D6-4C04-AD36-F3C6D1FDC10C@xxxxxx Quote: > > hi, > > I have a vbscript that I can not get to recurs properly. If the user > enters > '5' for input, the script should end. For ALL other input: invalid data > returns to the inputbox (this works), and valid data should execute the > corresponding sub-routines, and then return to the inputbox. I am unable > to > format this so far to get it to do this. I have tried loops and ended up > with > mostly endless loops. I have also tried to call (a 2nd call) the > sub-procedure for the main menu (mainmenu()) at the end of the script, but > it > simply stops after the previous sub-routines finish. I have also tried to > add > the mainmenu() call at the end of each sub-routine for option 1thru4 to no > avail, it still stops after each sub-routine. > > The inputbox and the use of subroutines for the selections 1-4 are > required, > so I can not re-write this entire thing. Any suggestions? > > the script: > > __________________________________________________________ > Option Explicit > > Dim fso, objshl, textfile, title, choice, inputtest, program, exitnow > > Set fso = CreateObject("Scripting.FileSystemObject") > Set objshl = CreateObject("WScript.Shell") > textfile = "c:\scriptlog.txt" > title = "Main Menu - Current Date: " & Date > > '************************* > 'Main Processing Section * > '************************* > > mainmenu() > > If choice = "1" Then > Logfile() > Powerpoint() > ElseIf choice = "2" then > Logfile() > Excel() > ElseIf choice = "3" Then > Viewlogfile() > ElseIf choice = "4" then > Deletelogfile() > End If > > '******************************************** > 'Main Menu for player input and value testing > '******************************************** > Sub mainmenu() > Do Until inputtest = "valid" > choice = (InputBox(("1. Run PowerPoint") & vbCrLf & ("2. Run Excel")_ > & vbCrLf & ("3. View Log File") & vbCrLf &_ > ("4. Delete Log File") & vbCrLf & ("5. Quit") & vbCrLf &_ > vbCrLf & vbTab & vbTab & vbTab & "Choose By > Number",title)) > > If choice = "1" Then > inputtest = "valid" > program = "PowerPoint" > ElseIf choice = "2" then > inputtest = "valid" > program = "Excel" > ElseIf choice = "3" Then > inputtest = "valid" > ElseIf choice = "4" then > inputtest = "valid" > ElseIf choice = "5" Then > WScript.Quit > Else MsgBox("Your selection was not valid") > End If > Loop > End Sub > > '********************************************** > 'Create 'start' logfile text for option 1 and 2 > '********************************************** > Sub Logfile() > Dim log > If (fso.FileExists(textfile)) Then > Set log = fso.OpenTextFile(textfile,8) > log.WriteLine "-- -- -- -- -- -- -- -- -- -- -- --" > log.WriteLine ((program) & " started on ") & Date > log.WriteLine ((program) & " started at ") & Time > log.WriteLine "-- -- -- --" > log.close > Else > Set log = fso.OpenTextFile(textfile, 2, "true") > log.WriteLine "-- -- -- -- -- -- -- -- -- -- -- --" > log.WriteLine ((program) & " started on ") & Date > log.WriteLine ((program) & " started at ") & Time > log.WriteLine "-- -- -- --" > log.close > End If > End Sub > > '************************************** > 'Process tasks for option '1' selection > '************************************** > Sub Powerpoint() > Dim log > objshl.Run "powerpnt.exe",,True > Set log = fso.OpenTextFile(textfile, 8) > log.WriteLine "PowerPoint stopped on " & Date > log.WriteLine "PowerPoint stopped at " & Time > log.WriteLine "-- -- -- -- -- -- -- -- -- -- -- --" > log.close() > End Sub > > '************************************** > 'Process tasks for option '2' selection > '************************************** > Sub Excel() > Dim log > objshl.Run "excel.exe",,True > Set log = fso.OpenTextFile(textfile, 8) > log.WriteLine "Excel stopped on " & Date > log.WriteLine "Excel stopped at " & Time > log.WriteLine "-- -- -- -- -- -- -- -- -- -- -- --" > log.close() > End Sub > > '************************************** > 'Process tasks for option '3' selection > '************************************** > Sub Viewlogfile() > Dim output, log > Set log = fso.OpenTextFile(textfile, 1) > If (fso.FileExists(textfile)) Then > output = log.ReadAll > log.Close > MsgBox output > Else > MsgBox("The log file does not exist") > End If > End Sub > > '************************************** > 'Process tasks for option '4' selection > '************************************** > Sub Deletelogfile() > Dim confirm, file > If Not (fso.FileExists(textfile)) Then > MsgBox "There is no file to delete", 0 > Else > confirm = MsgBox("Are you sure you wish to delete this?", 4, "Confirm > Selection") > If confirm = vbYes Then > fso.DeleteFile textfile > Set file = fso.OpenTextFile(textfile, 2, "true") > file.WriteLine "Logfile created on " & Date & " at " & Time > file.close > End if > End If > End Sub until the user selects to quit. There must be several ways to do this, but I remove the code before the Subs (except the call to mainmenu), then I would revise Sub mainmenu as follow: =========== Sub mainmenu() inputtest = "valid" Do Until inputtest = "quit" choice = (InputBox(("1. Run PowerPoint") _ & vbCrLf & ("2. Run Excel") _ & vbCrLf & ("3. View Log File") _ & vbCrLf & ("4. Delete Log File") _ & vbCrLf & ("5. Quit") _ & vbCrLf & vbCrLf _ & vbTab & vbTab & vbTab & "Choose By Number",title)) Select Case choice Case "1" Logfile() Powerpoint() Case "2" Logfile() Excel() Case "3" Viewlogfile() Case "4" Deletelogfile() Case "5" inputtest = "quit" Case Else MsgBox("Your selection was not valid") End Select Loop End Sub ======== This worked for me. -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net -- |
My System Specs![]() |
| | #3 (permalink) |
| | Re: recursion help request "Vgolfmaster" <Vgolfmaster@xxxxxx> wrote in message news:811E3A53-97D6-4C04-AD36-F3C6D1FDC10C@xxxxxx Quote: > > hi, > > I have a vbscript that I can not get to recurs properly. If the user > enters > '5' for input, the script should end. For ALL other input: invalid data > returns to the inputbox (this works), and valid data should execute the > corresponding sub-routines, and then return to the inputbox. I am unable > to > format this so far to get it to do this. I have tried loops and ended up > with > mostly endless loops. I have also tried to call (a 2nd call) the > sub-procedure for the main menu (mainmenu()) at the end of the script, but > it > simply stops after the previous sub-routines finish. I have also tried to > add > the mainmenu() call at the end of each sub-routine for option 1thru4 to no > avail, it still stops after each sub-routine. > > The inputbox and the use of subroutines for the selections 1-4 are > required, > so I can not re-write this entire thing. Any suggestions? > > the script: you expect a defined set of input response from the user, the program cries out for a "Case Select" construct rather than a nested if-then-elseif set of statements. Here is the main part of your program again. Please note that I did not review or test the remainder of the code. '************************* 'Main Processing Section * '************************* mainmenu() Do Select Case choice Case 1 program = "PowerPoint" Logfile() Powerpoint() Case 2 program = "Excel" Logfile() Excel() Case 3 Viewlogfile() Case 4 Deletelogfile() Case Else WScript.quit End Select Loop '******************************************** 'Main Menu for player input and value testing '******************************************** Sub mainmenu() Do choice = (InputBox(_ ("1. Run PowerPoint") & VbCrLf & _ ("2. Run Excel") & VbCrLf & _ ("3. View Log File") & VbCrLf &_ ("4. Delete Log File") & VbCrLf & _ ("5. Quit") & VbCrLf & VbCrLf & vbTab & vbTab & vbTab & _ "Choose By Number",title)) if IsNumeric(choice) then if (choice > 0) and (choice < 6) then Exit Do MsgBox("Your selection was not valid") Loop End Sub |
My System Specs![]() |
| | #4 (permalink) |
| | Re: recursion help request Thanks Richard, I agree, recursion was the wrong term. I modified the script using your advice and some other input I got, and it is now working! Thanks much "Richard Mueller [MVP]" wrote: Quote: > > "Vgolfmaster" <Vgolfmaster@xxxxxx> wrote in message > news:811E3A53-97D6-4C04-AD36-F3C6D1FDC10C@xxxxxx Quote: > > > > hi, > > > > I have a vbscript that I can not get to recurs properly. If the user > > enters > > '5' for input, the script should end. For ALL other input: invalid data > > returns to the inputbox (this works), and valid data should execute the > > corresponding sub-routines, and then return to the inputbox. I am unable > > to > > format this so far to get it to do this. I have tried loops and ended up > > with > > mostly endless loops. I have also tried to call (a 2nd call) the > > sub-procedure for the main menu (mainmenu()) at the end of the script, but > > it > > simply stops after the previous sub-routines finish. I have also tried to > > add > > the mainmenu() call at the end of each sub-routine for option 1thru4 to no > > avail, it still stops after each sub-routine. > > > > The inputbox and the use of subroutines for the selections 1-4 are > > required, > > so I can not re-write this entire thing. Any suggestions? > > > > the script: > > > > __________________________________________________________ > > Option Explicit > > > > Dim fso, objshl, textfile, title, choice, inputtest, program, exitnow > > > > Set fso = CreateObject("Scripting.FileSystemObject") > > Set objshl = CreateObject("WScript.Shell") > > textfile = "c:\scriptlog.txt" > > title = "Main Menu - Current Date: " & Date > > > > '************************* > > 'Main Processing Section * > > '************************* > > > > mainmenu() > > > > If choice = "1" Then > > Logfile() > > Powerpoint() > > ElseIf choice = "2" then > > Logfile() > > Excel() > > ElseIf choice = "3" Then > > Viewlogfile() > > ElseIf choice = "4" then > > Deletelogfile() > > End If > > > > '******************************************** > > 'Main Menu for player input and value testing > > '******************************************** > > Sub mainmenu() > > Do Until inputtest = "valid" > > choice = (InputBox(("1. Run PowerPoint") & vbCrLf & ("2. Run Excel")_ > > & vbCrLf & ("3. View Log File") & vbCrLf &_ > > ("4. Delete Log File") & vbCrLf & ("5. Quit") & vbCrLf &_ > > vbCrLf & vbTab & vbTab & vbTab & "Choose By > > Number",title)) > > > > If choice = "1" Then > > inputtest = "valid" > > program = "PowerPoint" > > ElseIf choice = "2" then > > inputtest = "valid" > > program = "Excel" > > ElseIf choice = "3" Then > > inputtest = "valid" > > ElseIf choice = "4" then > > inputtest = "valid" > > ElseIf choice = "5" Then > > WScript.Quit > > Else MsgBox("Your selection was not valid") > > End If > > Loop > > End Sub > > > > '********************************************** > > 'Create 'start' logfile text for option 1 and 2 > > '********************************************** > > Sub Logfile() > > Dim log > > If (fso.FileExists(textfile)) Then > > Set log = fso.OpenTextFile(textfile,8) > > log.WriteLine "-- -- -- -- -- -- -- -- -- -- -- --" > > log.WriteLine ((program) & " started on ") & Date > > log.WriteLine ((program) & " started at ") & Time > > log.WriteLine "-- -- -- --" > > log.close > > Else > > Set log = fso.OpenTextFile(textfile, 2, "true") > > log.WriteLine "-- -- -- -- -- -- -- -- -- -- -- --" > > log.WriteLine ((program) & " started on ") & Date > > log.WriteLine ((program) & " started at ") & Time > > log.WriteLine "-- -- -- --" > > log.close > > End If > > End Sub > > > > '************************************** > > 'Process tasks for option '1' selection > > '************************************** > > Sub Powerpoint() > > Dim log > > objshl.Run "powerpnt.exe",,True > > Set log = fso.OpenTextFile(textfile, 8) > > log.WriteLine "PowerPoint stopped on " & Date > > log.WriteLine "PowerPoint stopped at " & Time > > log.WriteLine "-- -- -- -- -- -- -- -- -- -- -- --" > > log.close() > > End Sub > > > > '************************************** > > 'Process tasks for option '2' selection > > '************************************** > > Sub Excel() > > Dim log > > objshl.Run "excel.exe",,True > > Set log = fso.OpenTextFile(textfile, 8) > > log.WriteLine "Excel stopped on " & Date > > log.WriteLine "Excel stopped at " & Time > > log.WriteLine "-- -- -- -- -- -- -- -- -- -- -- --" > > log.close() > > End Sub > > > > '************************************** > > 'Process tasks for option '3' selection > > '************************************** > > Sub Viewlogfile() > > Dim output, log > > Set log = fso.OpenTextFile(textfile, 1) > > If (fso.FileExists(textfile)) Then > > output = log.ReadAll > > log.Close > > MsgBox output > > Else > > MsgBox("The log file does not exist") > > End If > > End Sub > > > > '************************************** > > 'Process tasks for option '4' selection > > '************************************** > > Sub Deletelogfile() > > Dim confirm, file > > If Not (fso.FileExists(textfile)) Then > > MsgBox "There is no file to delete", 0 > > Else > > confirm = MsgBox("Are you sure you wish to delete this?", 4, "Confirm > > Selection") > > If confirm = vbYes Then > > fso.DeleteFile textfile > > Set file = fso.OpenTextFile(textfile, 2, "true") > > file.WriteLine "Logfile created on " & Date & " at " & Time > > file.close > > End if > > End If > > End Sub > I wouldn't call this recursion, but you want to keep running in the loop > until the user selects to quit. There must be several ways to do this, but I > remove the code before the Subs (except the call to mainmenu), then I would > revise Sub mainmenu as follow: > =========== > Sub mainmenu() > inputtest = "valid" > Do Until inputtest = "quit" > choice = (InputBox(("1. Run PowerPoint") _ > & vbCrLf & ("2. Run Excel") _ > & vbCrLf & ("3. View Log File") _ > & vbCrLf & ("4. Delete Log File") _ > & vbCrLf & ("5. Quit") _ > & vbCrLf & vbCrLf _ > & vbTab & vbTab & vbTab & "Choose By Number",title)) > > Select Case choice > Case "1" > Logfile() > Powerpoint() > Case "2" > Logfile() > Excel() > Case "3" > Viewlogfile() > Case "4" > Deletelogfile() > Case "5" > inputtest = "quit" > Case Else > MsgBox("Your selection was not valid") > End Select > Loop > End Sub > ======== > This worked for me. > > -- > Richard Mueller > MVP Directory Services > Hilltop Lab - http://www.rlmueller.net > -- > > > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: recursion help request Thanks as well Pegasus, Looks like you and Richard both had me led in the right direction - sorry that you both spent time on this and duplicated the effort but I'll gladly take more feedback as compared to none any day! "Pegasus [MVP]" wrote: Quote: > > "Vgolfmaster" <Vgolfmaster@xxxxxx> wrote in message > news:811E3A53-97D6-4C04-AD36-F3C6D1FDC10C@xxxxxx Quote: > > > > hi, > > > > I have a vbscript that I can not get to recurs properly. If the user > > enters > > '5' for input, the script should end. For ALL other input: invalid data > > returns to the inputbox (this works), and valid data should execute the > > corresponding sub-routines, and then return to the inputbox. I am unable > > to > > format this so far to get it to do this. I have tried loops and ended up > > with > > mostly endless loops. I have also tried to call (a 2nd call) the > > sub-procedure for the main menu (mainmenu()) at the end of the script, but > > it > > simply stops after the previous sub-routines finish. I have also tried to > > add > > the mainmenu() call at the end of each sub-routine for option 1thru4 to no > > avail, it still stops after each sub-routine. > > > > The inputbox and the use of subroutines for the selections 1-4 are > > required, > > so I can not re-write this entire thing. Any suggestions? > > > > the script: > Your specification does not require a recursion - it requires a loop. Since > you expect a defined set of input response from the user, the program cries > out for a "Case Select" construct rather than a nested if-then-elseif set of > statements. Here is the main part of your program again. Please note that I > did not review or test the remainder of the code. > > '************************* > 'Main Processing Section * > '************************* > mainmenu() > > Do > Select Case choice > Case 1 > program = "PowerPoint" > Logfile() > Powerpoint() > Case 2 > program = "Excel" > Logfile() > Excel() > Case 3 > Viewlogfile() > Case 4 > Deletelogfile() > Case Else > WScript.quit > End Select > Loop > > '******************************************** > 'Main Menu for player input and value testing > '******************************************** > Sub mainmenu() > Do > choice = (InputBox(_ > ("1. Run PowerPoint") & VbCrLf & _ > ("2. Run Excel") & VbCrLf & _ > ("3. View Log File") & VbCrLf &_ > ("4. Delete Log File") & VbCrLf & _ > ("5. Quit") & VbCrLf & VbCrLf & vbTab & vbTab & vbTab & _ > "Choose By Number",title)) > if IsNumeric(choice) then if (choice > 0) and (choice < 6) then Exit Do > MsgBox("Your selection was not valid") > Loop > End Sub > > > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: recursion help request "Vgolfmaster" <Vgolfmaster@xxxxxx> wrote in message news:26DD2FF5-6533-44F9-A31E-E593E2615E0B@xxxxxx Quote: > Thanks as well Pegasus, > > Looks like you and Richard both had me led in the right direction - sorry > that you both spent time on this and duplicated the effort but I'll gladly > take more feedback as compared to none any day! your post at much the same time. |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| RE: file rename with folder recursion | PowerShell | |||
| Xml, Recursion and Base64 | PowerShell | |||
| Xml and Recursion | PowerShell | |||
| Xml, recursion and Ôase64 | PowerShell | |||
| How do I limit subfolder recursion? | VB Script | |||