Solved

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

Posted on 2008-10-10
10
252 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

930 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now