Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Run sub based on InputBox value

Posted on 2011-05-11
5
Medium Priority
?
509 Views
Last Modified: 2012-05-11
Hello, I need help creating a vbscript to run subs or loops or whatever is best to run a robocopy command string based on user input.
Attached is the effort so far (that doesn't work).

This is a modification of another process that does work (no subs, just put in folder names)
Looking forward to your help!
Dim iBernieFolder

	iBernieFolder = InputBox("Enter Letter of Permissions to set for new folder")

	if iBernieFolder = r Then
Do
strProjectName = InputBox("Enter the Employee Resources Folder Name to Create (ie. 09069.0 Richmond, etc. :")
Do While strProjectName = ""
MsgBox "Please enter the Employee Resources Folder Name to proceed!"
strProjectName = InputBox("Employee Resources Folder Name to Create:")
strSource = "\\Mch10\public\Projects\_ProjectFolderTools\Employee Resources Folders\R_NTFS"
strDest1 = "\\Mch10\public\Employee Resources\SHARED\_Engineering" & "\" & strProjectName

   dim oExec, WshShell
   strParameters = chr(34) & strSource & chr(34) & chr(32) & chr(34) & strDest1 & chr(34) & chr(32) & "/E /COPYALL /V /LOG:copy.log /R:10 /W:30"
   
   Set WshShell = wscript.createobject("wscript.shell")
   Set oExec = WshShell.Exec ("\\mch10\public\Projects\_ProjectFolderTools\robocopy.exe" & chr(32) & strParameters)
   set WshShell = nothing
Loop
   


	elseif iBernieFolder = w Then
Do
strProjectName = InputBox("Enter the Employee Resources Folder Name to Create (ie. 09069.0 Richmond, etc. :")
Do While strProjectName = ""
MsgBox "Please enter the Employee Resources Folder Name to proceed!"
strProjectName = InputBox("Employee Resources Folder Name to Create:")
strSource = "\\Mch10\public\Projects\_ProjectFolderTools\Employee Resources Folders\W_NTFS"
strDest1 = "\\Mch10\public\Employee Resources\SHARED\_Engineering" & "\" & strProjectName

   dim oExec, WshShell
   strParameters = chr(34) & strSource & chr(34) & chr(32) & chr(34) & strDest1 & chr(34) & chr(32) & "/E /COPYALL /V /LOG:copy.log /R:10 /W:30"
   
   Set WshShell = wscript.createobject("wscript.shell")
   Set oExec = WshShell.Exec ("\\mch10\public\Projects\_ProjectFolderTools\robocopy.exe" & chr(32) & strParameters)
   set WshShell = nothing
Loop  



		elseif iBernieFolder = x Then
Do
strProjectName = InputBox("Enter the Employee Resources Folder Name to Create (ie. 09069.0 Richmond, etc. :")
Do While strProjectName = ""
MsgBox "Please enter the Employee Resources Folder Name to proceed!"
strProjectName = InputBox("Employee Resources Folder Name to Create:")
strSource = "\\Mch10\public\Projects\_ProjectFolderTools\Employee Resources Folders\X_NTFS"
strDest1 = "\\Mch10\public\Employee Resources\SHARED\_Engineering" & "\" & strProjectName

   dim oExec, WshShell
   strParameters = chr(34) & strSource & chr(34) & chr(32) & chr(34) & strDest1 & chr(34) & chr(32) & "/E /COPYALL /V /LOG:copy.log /R:10 /W:30"
   
   Set WshShell = wscript.createobject("wscript.shell")
   Set oExec = WshShell.Exec ("\\mch10\public\Projects\_ProjectFolderTools\robocopy.exe" & chr(32) & strParameters)
   set WshShell = nothing
Loop
   
MsgBox "ATTENTION:  Do not close console/command prompt window!  The window will close when the copy of " & strPackage & " is complete. On a VPN connection this can take up to 

2:30 minutes or longer (depending on your network/Internet connection and traffic at Redmond) to complete. Please click OK now."

Endif

Open in new window

0
Comment
Question by:NetManaged
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:Psy053
ID: 35743598
Is this the complete script?  What are r, w and x?



0
 
LVL 14

Expert Comment

by:Psy053
ID: 35743808
The snippet below uses Do Until Loops for input verification, then uses a Select Case statement to run the appropriate command.

Please have a look through it, and if you have any questions please let me know.


dim iBernieFolder
dim oExec
dim WshShell

Set WshShell = Wscript.CreateObject("Wscript.Shell")

Do Until iBernieFolder = "R" OR iBernieFolder = "W" OR iBernieFolder = "X"
	iBernieFolder = Ucase(InputBox("Enter Letter of Permissions to set for new folder" & VbCr & "(R, W or X)"))
Loop


Do until Len(strProjectName) > 0
	strProjectName = InputBox("Enter the Employee Resources Folder Name to Create (ie. 09069.0 Richmond, etc):")
Loop


Select Case iBernieFolder
	Case "R"
		strSource = "\\Mch10\public\Projects\_ProjectFolderTools\Employee Resources Folders\R_NTFS"
		strDest1 = "\\Mch10\public\Employee Resources\SHARED\_Engineering" & "\" & strProjectName
		strParameters = chr(34) & strSource & chr(34) & chr(32) & chr(34) & strDest1 & _ 
				chr(34) & chr(32) & "/E /COPYALL /V /LOG:copy.log /R:10 /W:30" & strDest1
		Set oExec = WshShell.Exec ("\\mch10\public\Projects\_ProjectFolderTools\robocopy.exe" & chr(32) & strParameters)
		Set WshShell = Nothing	


	Case "W"
		strSource = "\\Mch10\public\Projects\_ProjectFolderTools\Employee Resources Folders\W_NTFS"
		strDest1 = "\\Mch10\public\Employee Resources\SHARED\_Engineering" & "\" & strProjectName
		strParameters = chr(34) & strSource & chr(34) & chr(32) & chr(34) & strDest1 & _
				chr(34) & chr(32) & "/E /COPYALL /V /LOG:copy.log /R:10 /W:30"
		Set oExec = WshShell.Exec ("\\mch10\public\Projects\_ProjectFolderTools\robocopy.exe" & chr(32) & strParameters)
		Set WshShell = Nothing

	Case "X"
		strSource = "\\Mch10\public\Projects\_ProjectFolderTools\Employee Resources Folders\X_NTFS"
		strDest1 = "\\Mch10\public\Employee Resources\SHARED\_Engineering" & "\" & strProjectName
		strParameters = chr(34) & strSource & chr(34) & chr(32) & chr(34) & strDest1 & _
				chr(34) & chr(32) & "/E /COPYALL /V /LOG:copy.log /R:10 /W:30"
		Set oExec = WshShell.Exec ("\\mch10\public\Projects\_ProjectFolderTools\robocopy.exe" & chr(32) & strParameters)
		Set WshShell = nothing
End Select

MsgBox 	"ATTENTION:  Do not close console/command prompt window!  The window will close when the " & _ 
	"copy of " & strPackage & " is complete. On a VPN connection this can take up to 2:30 " & _
	"minutes or longer (depending on your network/Internet connection and traffic at Redmond) to " &_ 
	"complete. Please click OK now."

Open in new window

0
 

Author Comment

by:NetManaged
ID: 35770845
Hey Psy053!

   My apologies for the delay in responding to you! I tested your script, after a very slight modification, it worked a treat (see code attached).

For additional points, would you mind modifying to allow the following?
Ability to click Cancel on the Input boxes and the process end gracefully.

Thanks!
Greg
dim iBernieFolder 
dim oExec 
dim WshShell 
 
Set WshShell = Wscript.CreateObject("Wscript.Shell") 
 
Do Until iBernieFolder = "R" OR iBernieFolder = "W" OR iBernieFolder = "X" 
        iBernieFolder = Ucase(InputBox("Enter Letter of Permissions to set for new folder" & VbCr & "(R, W or X)")) 
Loop 
 
 
Do until Len(strProjectName) > 0 
        strProjectName = InputBox("Enter the Employee Resources Folder Name to Create (ie. 09069.0 Richmond, etc):") 
Loop 
 
 
Select Case iBernieFolder 
        Case "R" 
                strSource = "\\Mch10\public\Projects\_ProjectFolderTools\Employee Resources SHARED Engineering Folder Template\R_NTFS" 
                strDest1 = "\\Mch10\public\Employee Resources\SHARED\_Engineering" & "\" & strProjectName 
                strParameters = chr(34) & strSource & chr(34) & chr(32) & chr(34) & strDest1 & _  
                                chr(34) & chr(32) & "/E /COPYALL /V /LOG:copy.log /R:10 /W:30"
                Set oExec = WshShell.Exec ("\\mch10\public\Projects\_ProjectFolderTools\robocopy.exe" & chr(32) & strParameters) 
                Set WshShell = Nothing   
 
 
        Case "W" 
                strSource = "\\Mch10\public\Projects\_ProjectFolderTools\Employee Resources SHARED Engineering Folder Template\W_NTFS" 
                strDest1 = "\\Mch10\public\Employee Resources\SHARED\_Engineering" & "\" & strProjectName 
                strParameters = chr(34) & strSource & chr(34) & chr(32) & chr(34) & strDest1 & _ 
                                chr(34) & chr(32) & "/E /COPYALL /V /LOG:copy.log /R:10 /W:30" 
                Set oExec = WshShell.Exec ("\\mch10\public\Projects\_ProjectFolderTools\robocopy.exe" & chr(32) & strParameters) 
                Set WshShell = Nothing 
 
        Case "X" 
                strSource = "\\Mch10\public\Projects\_ProjectFolderTools\Employee Resources SHARED Engineering Folder Template\X_NTFS" 
                strDest1 = "\\Mch10\public\Employee Resources\SHARED\_Engineering" & "\" & strProjectName 
                strParameters = chr(34) & strSource & chr(34) & chr(32) & chr(34) & strDest1 & _ 
                                chr(34) & chr(32) & "/E /COPYALL /V /LOG:copy.log /R:10 /W:30" 
                Set oExec = WshShell.Exec ("\\mch10\public\Projects\_ProjectFolderTools\robocopy.exe" & chr(32) & strParameters) 
                Set WshShell = nothing 
End Select 
 
MsgBox  "ATTENTION:  Do not close console/command prompt window!  The window will close when the " & _  
        "copy of " & strPackage & " is complete. On a VPN connection this can take up to 2:30 " & _ 
        "minutes or longer (depending on your network/Internet connection and traffic at Redmond) to " &_  
        "complete. Please click OK now."

Open in new window

0
 
LVL 14

Accepted Solution

by:
Psy053 earned 2000 total points
ID: 35774138
Greg, please find the updated script.


With this script, I have included a sub prodcedure that checks whether the InputBox has returned a zero-length string, if it has, it displays a MsgBox asking them if they want to exit - if they select Yes the script ends, if they press No the script returns to the Do Loop.

The reason that I've done it this way is because with an InputBox dialog, pressing OK without entering any text, and pressing Cancel both return a zero length string.

dim iBernieFolder 
dim oExec 
dim WshShell 
 
Set WshShell = Wscript.CreateObject("Wscript.Shell") 
 
Do Until iBernieFolder = "R" OR iBernieFolder = "W" OR iBernieFolder = "X" 
        iBernieFolder = Ucase(InputBox("Enter Letter of Permissions to set for new folder" & VbCr & "(R, W or X)")) 
	ExitCheck(iBernieFolder)
Loop 
 
 
Do until Len(strProjectName) > 0 
        strProjectName = InputBox("Enter the Employee Resources Folder Name to Create (ie. 09069.0 Richmond, etc):") 
	ExitCheck(strProjectName)
Loop 
 
 
Select Case iBernieFolder 
        Case "R" 
                strSource = "\\Mch10\public\Projects\_ProjectFolderTools\Employee Resources SHARED Engineering Folder Template\R_NTFS" 
                strDest1 = "\\Mch10\public\Employee Resources\SHARED\_Engineering" & "\" & strProjectName 
                strParameters = chr(34) & strSource & chr(34) & chr(32) & chr(34) & strDest1 & _  
                                chr(34) & chr(32) & "/E /COPYALL /V /LOG:copy.log /R:10 /W:30"
                Set oExec = WshShell.Exec ("\\mch10\public\Projects\_ProjectFolderTools\robocopy.exe" & chr(32) & strParameters) 
                Set WshShell = Nothing   
 
 
        Case "W" 
                strSource = "\\Mch10\public\Projects\_ProjectFolderTools\Employee Resources SHARED Engineering Folder Template\W_NTFS" 
                strDest1 = "\\Mch10\public\Employee Resources\SHARED\_Engineering" & "\" & strProjectName 
                strParameters = chr(34) & strSource & chr(34) & chr(32) & chr(34) & strDest1 & _ 
                                chr(34) & chr(32) & "/E /COPYALL /V /LOG:copy.log /R:10 /W:30" 
                Set oExec = WshShell.Exec ("\\mch10\public\Projects\_ProjectFolderTools\robocopy.exe" & chr(32) & strParameters) 
                Set WshShell = Nothing 
 
        Case "X" 
                strSource = "\\Mch10\public\Projects\_ProjectFolderTools\Employee Resources SHARED Engineering Folder Template\X_NTFS" 
                strDest1 = "\\Mch10\public\Employee Resources\SHARED\_Engineering" & "\" & strProjectName 
                strParameters = chr(34) & strSource & chr(34) & chr(32) & chr(34) & strDest1 & _ 
                                chr(34) & chr(32) & "/E /COPYALL /V /LOG:copy.log /R:10 /W:30" 
                Set oExec = WshShell.Exec ("\\mch10\public\Projects\_ProjectFolderTools\robocopy.exe" & chr(32) & strParameters) 
                Set WshShell = nothing 
End Select 
 
MsgBox  "ATTENTION:  Do not close console/command prompt window!  The window will close when the " & _  
        "copy of " & strPackage & " is complete. On a VPN connection this can take up to 2:30 " & _ 
        "minutes or longer (depending on your network/Internet connection and traffic at Redmond) to " &_  
        "complete. Please click OK now."


Sub ExitCheck(strCheck)
	If strCheck = "" Then
		ret = MsgBox ("Text field was empty, or Cancel was pushed!" & VBCr & VBCr & "Do you wish to exit?", VbYesNo, "Exit?")
		If ret = VbYes Then 
			Wscript.Quit
		End If
	End If
End Sub

Open in new window

0
 

Author Comment

by:NetManaged
ID: 35796960
Great Job! Thanks! Points upgraded and awarded.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month10 days, 19 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question