Correct VBScript Syntax for a CopyFile Function

I have been able to generate a successful run of a script containing the below function.  But now it needs to retrieve the source files to be copied from a UNC path.  Can you help me plese with understanding the syntax ?  It would be something like  (\\system\share\arrLines(i), sDestinationPath)

Function CopyFiles(sSourceFile, sDestinationPath)
     Dim oFSO, oSourceFile
     Dim arrLines, i

     Set oFSO = CreateObject("scripting.FileSystemObject")

     Set oSourceFile = oFSO.OpenTextFile(sSourceFile, 1)

     arrLines = Split(oSourceFile.ReadAll(), vbCrLf)

     For i = LBound(arrLines) To UBound(arrLines)
        Call oFSO.CopyFile(arrLines(i), sDestinationPath)
     Next

     Call oSourceFile.Close()
     Set oSourceFile = Nothing

     Set oFSO = Nothing
End Function
danfiggolfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dentabCommented:
um... what does arrLines hold?
If its just the file name and not the path, it would be

Call oFSO.CopyFile("\\server\fileshare\" & arrLines(i), sDestinationPath)

otherwize, if arrlines was populated with UNC paths, I dont see why your orig code wouldnt work
0
danfiggolfAuthor Commented:
I'm sorry for the lack of info.  Here's what's happening:

There are two text files:
1) computers.txt - which holds the list of computers to receive a copy of the files read from
2) files2copy.txt  - the list of files are contained here

The launchcopy.vbs script reads the computer.txt file and spawns a run of the filecopy.vbs for each PC.  First a look at the launchcopy.vbs:
'============
Const INPUT_FILE_NAME = "Computers.txt"
Const OUTPUT_FILE_NAME = "Copy_Log.txt"
Const FOR_READING = 1
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)
strComputers = objFile.ReadAll
objFile.Close
' Create the empty log file
objShell.Run "cmd /c Echo """" > " & OUTPUT_FILE_NAME, 0, True
arrComputers = Split(strComputers, vbCrLf)
Set objShell = CreateObject("WScript.Shell")
For Each strComputer In arrComputers
      If Ping(strComputer) = True Then
            strCommand = "wscript ""E:\Install\filecopy.vbs"" " & strComputer
            objShell.Run strCommand, 1, False
      Else
            objShell.Run "cmd /c Echo """ & strComputer & " could not be pinged."" >> " & OUTPUT_FILE_NAME, 0, True
      End If
Next

Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function
'============

Now here is the complete list of the filecopy.vbs which now needs to be able to pull the source file from a remote server via a UNC \\server\share -

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
strComputer = WScript.Arguments.Item(0)
WScript.Echo "About to check " & strComputer
Dim sSourceFile : sSourceFile = "E:\Install\files2CopyList.txt"
Dim sDestinationPath : sDestinationPath = "\\" & strComputer & "\FVPickup\"
 MsgBox strComputer
 MsgBox sSourceFile
 MsgBox sDestinationPath
Call CopyFiles(sSourceFile, sDestinationPath)

Function CopyFiles(sSourceFile, sDestinationPath)
     Dim oFSO, oSourceFile
     Dim arrLines, i

     Set oFSO = CreateObject("scripting.FileSystemObject")

     Set oSourceFile = oFSO.OpenTextFile(sSourceFile, 1)

     arrLines = Split(oSourceFile.ReadAll(), vbCrLf)

     For i = LBound(arrLines) To UBound(arrLines)
        Call oFSO.CopyFile(arrLines(i), sDestinationPath)
     Next

     Call oSourceFile.Close()
     Set oSourceFile = Nothing

     Set oFSO = Nothing
End Function
0
RobSampsonCommented:
Hi, you need to make sure that this path exists on each remote machine:
"\\" & strComputer & "\FVPickup\"

as a network share.  You should be able to type into Start --> Run:
\\compname\FVPickup
and go to that share.  If it is NOT shared out, then try using the Admin share of the root drive to get to it:
sDestinationPath = "\\" & strComputer & "\C$\FVPickup\"

Also, as you have already done, make sure the FVPickup actually exists, and keep the trailing backslash on the end, so the script knows it's a folder, not a file.

Regards,

Rob.
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

danfiggolfAuthor Commented:
Thanks Rob. I understand your direction about the sDestinationPath.  Can you help me to understand if the sSourcePath is also on a remote system, how the syntax would be setup.  This scripting workings perfectly when the files to be copied live on the same system that the script is run from.  What if the user is running this script from his PC to copy files from a server to multiply PCs?
0
RobSampsonCommented:
OK, I'll try to explain what's happening....

Firstly, the LaunchFile.vbs is obviously on the local system (that it is being run from), and will also have the "computers.txt" and "filestocopy.txt" there in the same folder.

Then, because you have this line:
   strCommand = "wscript ""E:\Install\filecopy.vbs"" " & strComputer
that means the filecopy.vbs must also be on the same system that ran LaunchFile.vbs, in the E:\Install folder.

Then the FileCopy.vbs kicks in, copying each file listed in the "E:\Install\files2CopyList.txt" file to each computers' "FVPickup" shared folder.

Now, because all of the files paths are listed via "E:\....", that means that only the computer that has the E:\Install folder with those files, can actually run this script.

The sSourceFile path is the "E:\Install\files2CopyList.txt" which can only be on the local system where the script is run from.

So, if you want to be able to run the script from another PC, then I suggest you share out this PC's Install folder, and then replace all of the E:\Install bits in each file reference to \\SERVER\Install, so for example,
   strCommand = "wscript ""E:\Install\filecopy.vbs"" " & strComputer
becomes
   strCommand = "wscript ""\\SERVER\Install\filecopy.vbs"" " & strComputer

and then obviously, the user running the script needs read permissions on that shared folder.

And, after all that, the whole thing should work.

Regards,

Rob.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
danfiggolfAuthor Commented:
OK Rob, I understand, thanks.  So, it wouldn't to have the filecopy.vbs on the user's PC tasked with this copy job.  So that in the the filecopy.vbs scripting there is the action to copy \\server\file to \\remotePC\file?

Thanks again for your help.
Dan
0
RobSampsonCommented:
To run from the user's PC, you'd need to run LaunchCopy first, which would go through the computers.txt file and run FileCopy for each computer.

If, however, you just want one computer to copy the files from "E:\Install\files2CopyList.txt" to its own FVPickup folder, then in FileCopy.vbs, change this
   strComputer = WScript.Arguments.Item(0)

to this:
  Set objNetwork = CreateObject("WScript.Network")
  If WScript.Arguments.Count = 1 Then
      strComputer = WScript.Arguments.Item(0)
   Else
      strComputer = objNetwork.ComputerName
   End If


and change this:
   sSourceFile = "E:\Install\files2CopyList.txt"

to this
   sSourceFile = "\\SERVER\Install\files2CopyList.txt"

or another path where the script can find "files2CopyList.txt" locally.

Then you can run FileCopy.vbs on it's own from any computer.

Regards,

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.