AlexInAustralia
asked on
WSH Object required running a VB script
I have a script to copy a file. It runs fine on my PC, but on another person's PC it gives a WSH (Windows Script Host) error with the error "Object Required". Both PCs are running Windows XP SP3.
Here is the script:
Option Explicit
Dim oArgs, fileToCopy, destination, objShell, objFolder
Set oArgs = WScript.Arguments.Unnamed
fileToCopy = oArgs.Item(0)
destination = oArgs.Item(1)
Set objShell = CreateObject("Shell.Applic ation")
Set objFolder = objShell.NameSpace(destina tion)
objFolder.CopyHere fileToCopy
If I comment out the last line of the script, I don't get any errors.
I presume the other PC has got VB Script installed, as it can run the following simple script:
WScript.Echo "Hello World"
Here is the script:
Option Explicit
Dim oArgs, fileToCopy, destination, objShell, objFolder
Set oArgs = WScript.Arguments.Unnamed
fileToCopy = oArgs.Item(0)
destination = oArgs.Item(1)
Set objShell = CreateObject("Shell.Applic
Set objFolder = objShell.NameSpace(destina
objFolder.CopyHere fileToCopy
If I comment out the last line of the script, I don't get any errors.
I presume the other PC has got VB Script installed, as it can run the following simple script:
WScript.Echo "Hello World"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Does it give a line number of the error and what are the two arguments you are passing to the script?
ASKER
The following were tried on another PC:
CopyFile C:\MyFolder\aaa.txt C:\
CopyFile C:\MyFolder\aaa.txt \\135.145.35.183\c$
CopyFile C:\MyFolder\aaa.txt \\135.145.35.183\MySharedD irectory
All 3 lines worked on my PC.
On the other PC, the first line worked, but the other two gave the error.
Based on my new script, the error reported was a WSH on line 27, char 3, error 'Object Required', code 800A01A8, source 'Microsoft VB Script runtime error'.
Copying the file manually to that box worked fine.
Here is my complete new script:
' This script copies a file to a destination folder
' Author: (enter author here)
' (add additional comments here)
'To use it, pass the following parameters (in the following order):
' 1: the path and name to the file that will be copied (eg, c:\temp\SomeFile.txt)
' 2: the destination path (eg, c:\temp\ or \\135.145.35.183\c$\)
Option Explicit
Dim oArgs, fileToCopy, destination, objShell, objFolder
Set oArgs = WScript.Arguments.Unnamed
fileToCopy = oArgs.Item(0)
destination = oArgs.Item(1)
Const FOF_YESTOALL = &H10&
Set objShell = CreateObject("Shell.Applic ation")
Set objFolder = objShell.NameSpace(destina tion)
If (Not objShell Is Nothing) Then
If (objFolder Is Nothing) Then
WScript.Echo "Could not find destination folder " + objFolder.Title
Else
objFolder.CopyHere fileToCopy, FOF_YESTOALL
End If
Else
WScript.Echo "Could not create the shell application object"
End If
Set objFolder = Nothing
Set objShell = Nothing
CopyFile C:\MyFolder\aaa.txt C:\
CopyFile C:\MyFolder\aaa.txt \\135.145.35.183\c$
CopyFile C:\MyFolder\aaa.txt \\135.145.35.183\MySharedD
All 3 lines worked on my PC.
On the other PC, the first line worked, but the other two gave the error.
Based on my new script, the error reported was a WSH on line 27, char 3, error 'Object Required', code 800A01A8, source 'Microsoft VB Script runtime error'.
Copying the file manually to that box worked fine.
Here is my complete new script:
' This script copies a file to a destination folder
' Author: (enter author here)
' (add additional comments here)
'To use it, pass the following parameters (in the following order):
' 1: the path and name to the file that will be copied (eg, c:\temp\SomeFile.txt)
' 2: the destination path (eg, c:\temp\ or \\135.145.35.183\c$\)
Option Explicit
Dim oArgs, fileToCopy, destination, objShell, objFolder
Set oArgs = WScript.Arguments.Unnamed
fileToCopy = oArgs.Item(0)
destination = oArgs.Item(1)
Const FOF_YESTOALL = &H10&
Set objShell = CreateObject("Shell.Applic
Set objFolder = objShell.NameSpace(destina
If (Not objShell Is Nothing) Then
If (objFolder Is Nothing) Then
WScript.Echo "Could not find destination folder " + objFolder.Title
Else
objFolder.CopyHere fileToCopy, FOF_YESTOALL
End If
Else
WScript.Echo "Could not create the shell application object"
End If
Set objFolder = Nothing
Set objShell = Nothing
ASKER
Oops.
I've now changed the line:
WScript.Echo "Could not find destination folder " + objFolder.Title
to:
WScript.Echo "Could not find destination folder"
And now it doesn't throw the WSH error any more.
But the question now becomes, why is it giving me "Could not find destination folder" on the other PC.
As I said, I can copy the file manually across to that box (on both PCs).
I've now changed the line:
WScript.Echo "Could not find destination folder " + objFolder.Title
to:
WScript.Echo "Could not find destination folder"
And now it doesn't throw the WSH error any more.
But the question now becomes, why is it giving me "Could not find destination folder" on the other PC.
As I said, I can copy the file manually across to that box (on both PCs).
Does the logged in user who is running the script have admin access on the machine @ 135.145.35.183? AFAIK, only admins are allowed to access admin shares (C$).
ASKER
You've answered my initial question. And now my code displays the relevant error handling if it can't do the copy. The reason why it's not copying is a different issue all together which the system administrator will have to look into.
ASKER
You've answered my initial question. And now my code displays the relevant error handling if it can't do the copy. The reason why it's not copying is a different issue all together which the system/network administrator will have to look into.