Script needed to copy a file

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..
LVL 2
cuadminAsked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
Hi, this is the code I have run for Case "1", and it works fine......can you see if it works for you?

'=========
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" And 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"
                  objNetwork.MapNetworkDrive "u:","\\ntfp\netdata\temp\"& strUserName, True
            objFSO.CopyFile "\\ntfp\netdata\temp\files.txt", "u:\office1\", True
      Case "2"
            objFSO.CopyFile "\\naawitsimitsvr\profiles\bds2.ini", "\\naawitsimitsvr\profiles\" & strUserName & "\office2\", True
End Select

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

And BTW, no, the '========  lines aren't required, I just use them to separate my code blocks.

Regards,

Rob.
0
 
RobSampsonCommented:
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.
0
 
cuadminAuthor 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.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RobSampsonCommented:
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.
0
 
cuadminAuthor 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
0
 
RobSampsonCommented:
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.
0
 
cuadminAuthor 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
0
 
cuadminAuthor 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
0
 
RobSampsonCommented:
Excellent, no worries.

Have fun with it...

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.