Solved

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

Posted on 2008-10-10
10
275 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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