Solved

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

Posted on 2008-10-10
10
243 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Oh, sirbounty is here, then you will be taken care of.
0
 
LVL 7

Accepted Solution

by:
Hubasan earned 50 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Closing Comment

by:dmking43
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

15 Experts available now in Live!

Get 1:1 Help Now