Link to home
Create AccountLog in
Avatar of cuadmin
cuadminFlag for Australia

asked on

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..
Avatar of RobSampson
RobSampson
Flag of Australia image

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.
Avatar of cuadmin

ASKER

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.
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.
Avatar of cuadmin

ASKER

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
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.
Avatar of cuadmin

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of cuadmin

ASKER

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
Excellent, no worries.

Have fun with it...

Rob.