We help IT Professionals succeed at work.

Script needed to copy a file

219 Views
Last Modified: 2010-04-16
Hi Scripting people

I was wondering if a script will do the following; and if so, could someone advise the script i would use to accomplish this.

User logs on to a Terminal Server. They get a msgbox asking which Office they are working at. (We have users from 2 separate locations that log on to the same server)
Depending on their answer, i then need to copy a small file from a network share location on the same server & have it overwrite the same named file that exists in their own personal profile directory.

There will only be 2 files in the network share to choose from, so i would like it to copy either File1 if they select Office1 as the location or copy file2 if they select office2.
 E.g 'I am working at office1'
I then need to copy \\servername\office1 file and put it into \\servername\profiles\username\office1 directory.

Hope that makes sense. Not sure if it's possible, but it would make my life much easier if i could.

1 other small question. In batch file language i can use %username% at the end of a script to have it look for that user's directory. What is the equivalent in VB scripting?

Thankyou all and have a great day..
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi, please try this:

'=============
strPrompt = "Please select your location by the number below:" & VbCrLf & _
            "1: Office 1" & VbCrLf & _
            "2: Office 2"
strOption = InputBox(strPrompt, "Select your Location")

While strOption <> "1" Or strOption <> "2"
      strOption = InputBox(strPrompt, "Select your Location")
Wend

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")

strUserName = objNetwork.UserName

Select Case strOption
      Case "1"
            objFSO.CopyFile "\\server1\share\file1.txt", "\\servername\profiles\" & strUserName & "\office1\", True
      Case "2"
            objFSO.CopyFile "\\server1\share\file2.txt", "\\servername\profiles\" & strUserName & "\office2\", True
End Select

Set objFSO = Nothing
Set objNetwork = Nothing
'=============


You can change the paths throughout, and note the place where we get the username is through the objNetwork object.

Regards,

Rob.

Author

Commented:
Hi
I have tried this but perhaps i am doing something wrong. The input box will load, but won't accept my answer of 1 or 2. (I'm assuming i just use 1 or 2 instead of office1 or office 2)
I don't receive an error message, when i click 'ok' nothing happens and i can only close the box using the 'end task' option in Task Manager.

The string i've used is below:

'============= (Do i need this in the file?)
strPrompt = "Please select your location by the number below:" & VbCrLf & _
            "1: Office 1" & VbCrLf & _
            "2: Office 2"
strOption = InputBox(strPrompt, "Select your Location")

While strOption <> "1" Or strOption <> "2"
      strOption = InputBox(strPrompt, "Select your Location")
Wend

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")

strUserName = objNetwork.UserName

Select Case strOption
      Case "1"
            objFSO.CopyFile "\\naawitsimitsvr\profiles\123.ini", "\\naawitsimitsvr\profiles\" & strUserName & "\office1\", True
      Case "2"
            objFSO.CopyFile "\\naawitsimitsvr\profiles\bds2.ini", "\\naawitsimitsvr\profiles\" & strUserName & "\office2\", True
End Select

Set objFSO = Nothing
Set objNetwork = Nothing
'=============
The files are located at \\naawitsimitsvr\profiles\123.ini and the destination folder is:
\\naawitsimitsvr\profiles\witsimadmin\office1

Both are shared correctly.

Thanks for your help.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Oh, oops....I think you need to change:
While strOption <> "1" Or strOption <> "2"

to
While strOption <> "1" And strOption <> "2"

just from Or to And....try that....

Regards,

Rob.

Author

Commented:
Hi Rob

I must say, you are a legend! That worked perfectly.

So to map a network drive to the naawitsimitsvr\profiles\username directory would be something like:

on error resume next
set objnetwork = wscript.createobject("Wscript.Network")

objnetwork.mapnetworkdrive "u:","\\naawitsimitsvr\profiles\& strUserName & (or something like that)

Happy to up the points to answer this one.

Thanks
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Almost, but you need to close the string and open it again before and after the ampersands...

objNetwork.MapNetworkDrive "u:", "\\naawitsimitsvr\profiles\" & strUserName, True

then the file copy would be:
objFSO.CopyFile "\\naawitsimitsvr\profiles\123.ini", "U:\office1\", True

That should work.  But technically, you could just use the UNC paths and not map the network drive....unless they don't have it mapped, and need it....

Regards,

Rob.

Author

Commented:
Sorry Rob, can't seem to crack this one.

All i need for this one is the script to map the drive to the users personal profile directory.
What i have used is:

set objnetwork = wscript.createobject("Wscript.Network")
objnetwork.mapnetworkdrive "u:","\\naawitsimitsvr\profiles\"& strUserName, True

Is this correct? When i run the file i get 'No network provider accepted the given network path', so if the scripts ok, i'll need to look at the directories and shares.

Thanks
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Congrats Rob, you are spot on the money this time. It's all good.

Thanks for all your help with this. It sure will save me some time.

Cheers
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Excellent, no worries.

Have fun with it...

Rob.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.