?
Solved

Script needed to copy a file

Posted on 2007-10-04
9
Medium Priority
?
202 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..
0
Comment
Question by:cuadmin
  • 5
  • 4
9 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 20019042
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
 
LVL 2

Author Comment

by:cuadmin
ID: 20019156
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 20019165
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Author Comment

by:cuadmin
ID: 20019220
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 20019242
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
 
LVL 2

Author Comment

by:cuadmin
ID: 20019302
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
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 20019455
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
 
LVL 2

Author Comment

by:cuadmin
ID: 20019531
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 20019544
Excellent, no worries.

Have fun with it...

Rob.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

807 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