Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2008-10-10
10
269 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Update Access FrontEnd by Version # 9 46
Excel 2016 loop through 6 43
Server 2012 management. 5 33
a slight change to the code written by Brad Yundt 2 33
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…
This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

840 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