Solved

How to create a vbscript to copy a file based on User Input?

Posted on 2008-10-10
10
262 Views
Last Modified: 2010-04-21
How are you doing, I am extreemly new to creating scripts, I need to be able to create a script that will copy a file based on the user input. Reason, we have a tunnel to our parent company and when in the corporate office we need to use a Host file to access certian hosts, but when outside of the corporate office the Local Host file prevents accessing the same hosts that are also available over the Internet.

I would like to simply have the user double click on the Script, have it ask if you are in the Coporate Office or Remote?
If answer is "Corporate" - Copy C:\Windows\system32\Drivers\etc\Hosts.loc to C:\Windows\system32\Drivers\etc\Hosts  

If answer is "Remote" - Copy C:\Windows\system32\Drivers\etc\Hosts.rem to C:\Windows\system32\Drivers\etc\Hosts  

It would over write the Hosts File each time with the correct version.

Any help in point me to in the right direction would be appreciated, sorry for the lack of knowledge.

Thanks
0
Comment
Question by:dmking43
  • 4
  • 4
  • 2
10 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 22691239
strReply = InputBox("Are you in the Corporate or Remote Office", "Location")

or

strReply = Msgbox("Corporate office users click (Y)es" & vbNewLine & "Remote users click (N)o", vbYesNo,"Location)


0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22691283
For only two choices, I prefer the Msgbox approach.  You have less to account for (can also use vbYesNoCancel to give a 'bail out' option).

For the inputbox route, I'd setup a loop similar to:

Do
  strReply = InputBox("Are you in the Corporate or Remote Office", "Location")
While lcase(strReply) <> "remote" and lcase(strReply) <> "corporate"

This ensures that one of the two will be entered, but has the risk of an endless loop.

To copy the file, use this code:
Dim objFSO :Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell :Set objShell = CreateObject("Wscript.Shell")
WinDir = objShell.ExpandEnvironmentStrings("%windir%")
Set objShell = Nothing
 
CorpHosts =  WinDir & "\system32\Drivers\etc\Hosts.loc"
RemHosts = WinDir & "\system32\Drivers\etc\Hosts.rem"
Target = WinDir & "\system32\Drivers\etc\Hosts"
 
'Use the prompt choice from above.
'For the inputbox route:
 
If lCase(strReply) = "corporate" then SourceFile = CorpHosts
If lCase(strReply) = "remote" then SourceFile = RemHosts
 
'For the msgbox:
If strReply = vbYes Then
  SourceFile = CorpHosts
Else
  If strReply = vbNo Then SourceFile = RemHosts
End If
 
objFSO.CopyFile SourceFile, TargetFile, True

Open in new window

0
 
LVL 7

Expert Comment

by:Hubasan
ID: 22691312
Hi there,

Here is the script that I wrote for you. You can change the text to suit the needs of your users.
Dim oFS, oWS
Const cTitle = "Corporate/Remote Connection"
Set oFS = CreateObject("scripting.filesystemobject")
Set oWS = CreateObject("WScript.Shell")
 
sCorp = "C:\Windows\system32\Drivers\etc\Hosts.loc"
sRem = "C:\Windows\system32\Drivers\etc\Hosts.rem"
sDest = "C:\Windows\system32\Drivers\etc\Hosts"
 
Do
	sInput = InputBox("Please enter where are you connecting from?" & vbCrLf & vbCrLf &_
	"Corporate or Remote?",cTitle)
	If Len(sInput) = 0 Then
		sRes = oWS.Popup("Sorry, you must enter a response to continue. Do you wish to try again?", ,cTitle ,vbYesNo+vbexclamation)
		If sRes = vbNo Then
			oWS.Popup "User canceled, Script Exiting!", , cTitle, vbCritical
			WScript.Quit
		End If
	End If
Loop Until Len(strUser) <> 0
 
sInput = UCase(sInput)
 
If InStr(sInput) = "CORPORATE" Then
	oFS.CopyFile(sCorp, sDest, True)
	Err.Clear
	If Err.Number <> 0 Then
		oWS.Popup "File was not copied, please check User Access Rights.", , cTitle,vbCritical
	Else
		oWS.Popup "User chose CORPORATE, Host file copied!"
		WScript.Quit
	End If	
ElseIf InStr(sInput) = "REMOTE" Then
	oFS.CopyFile(sRem, sDest, True)
	Err.Clear
	If Err.Number <> 0 Then
		oWS.Popup "File was not copied, please check User Access Rights.", , cTitle,vbCritical
	Else
		oWS.Popup "User chose REMOTE, Host file copied!"
		WScript.Quit
	End If	
End If

Open in new window

0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 7

Expert Comment

by:Hubasan
ID: 22691316
Oh, sirbounty is here, then you will be taken care of.
0
 
LVL 7

Accepted Solution

by:
Hubasan earned 50 total points
ID: 22691373
Just in case you want to use my script for some unknown reason :-) here is the correction to my previous post, there was couple of mistakes.


Dim oFS, oWS
Const cTitle = "Corporate/Remote Connection"
Set oFS = CreateObject("scripting.filesystemobject")
Set oWS = CreateObject("WScript.Shell")
 
sCorp = "C:\Windows\system32\Drivers\etc\Hosts.loc"
sRem = "C:\Windows\system32\Drivers\etc\Hosts.rem"
sDest = "C:\Windows\system32\Drivers\etc\hosts"
 
Do
	sInput = InputBox("Please enter where are you connecting from?" & vbCrLf & vbCrLf &_
	"Corporate or Remote?",cTitle)
	If Len(sInput) = 0 Then
		sRes = oWS.Popup("Sorry, you must enter a response to continue. Do you wish to try again?", ,cTitle ,vbYesNo+vbexclamation)
		If sRes = vbNo Then
			oWS.Popup "User canceled, Script Exiting!", , cTitle, vbCritical
			WScript.Quit
		End If
	End If
Loop Until Len(sInput) <> 0
 
sInput = UCase(sInput)
 
If InStr(sInput, "CORPORATE") Then
	oFS.CopyFile sCorp, sDest, True
	Err.Clear
	If Err.Number <> 0 Then
		oWS.Popup "File was not copied, please check User Access Rights.", , cTitle,vbCritical
	Else
		oWS.Popup "User chose CORPORATE, Host file copied!", , cTitle, vbinformation
		WScript.Quit
	End If	
ElseIf InStr(sInput, "REMOTE") Then
	oFS.CopyFile sRem, sDest, True
	Err.Clear
	If Err.Number <> 0 Then
		oWS.Popup "File was not copied, please check User Access Rights.", , cTitle,vbCritical
	Else
		oWS.Popup "User chose REMOTE, Host file copied!", , cTitle, vbinformation
		WScript.Quit
	End If	
End If

Open in new window

0
 

Author Closing Comment

by:dmking43
ID: 31505203
Thanks so much for the help. I was trying for several hours on my own, not pretty, you nailed it, Greatly appreciated.

Thanks again
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22695094
Anything in particular wrong with my script?
Generally for such a low point question, the first expert would receive the points, unless there's something horribly wrong with it - and with no author feedback, I'm left guessing and feeling a bit ignored...???
0
 

Author Comment

by:dmking43
ID: 22695256
I can only appoligize, When oening the E-Mail notifications, I opened the last one sent first, as my InBox lists the latest E-Mail first, an MS Exchange default. When I saw that response and it worked fine, I accepted it. I am sure this was easy to a person who wriites scripts as you mentioned.

I appreciate your help, and may appoligies for you feeling ignored, That was not my intention.

Thanks  
0
 
LVL 7

Expert Comment

by:Hubasan
ID: 22695336
sirbounty,

As I said im my previous post, when I saw you were helping dmking43 I knew he will be taken care of. I was just finishing my script and did a refresh and saw you posted just seconds before me. I will gladly refund my points if you want, and if there is a way. I have nothing but respect your status, as I know that you would not be Sage if you didn't help a lot of people here.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22695712
No, that's not at all what I was getting at.
I saw benefits with both versions of scripts posted and wanted to know if there was something I left out that you included.
No biggie on the points - glad you found your solution. :^)
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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