Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Works without variables, but not with???

Status
Not open for further replies.

slcjr777

IS-IT--Management
Feb 1, 2002
11
US
Hello
I am using the following to copy a dir from a network drive to a local computer. It works fine without declaring variables. The problem is if I declare the variables (and replace Wscript.Arguments and Wscript.Arguments.Count with those variables) I get a "subscript out of range" message. Can Someone tell me what I am doing wrong.
Thanks in Advance

'Option Explicit

Const OverWriteFiles = True

'Dim sFolder
'Dim dFolder
'Dim args
'Dim objFSO

'sFolder = Wscript.Arguments(0)
'dFolder = Wscript.Arguments(1)
'args = Wscript.Arguments.Count

IF Wscript.Arguments.Count < 2 THEN
Wscript.Echo &quot;Syntax is: cscript CopySp3.vbs SourceDir DestinationDir&quot;
Wscript.Quit
End IF

Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
objFSO.CopyFolder Wscript.Arguments(0) , Wscript.Arguments(1) , OverWriteFiles
 
here is the version I have amended, you need to check for the arguments before trying to assign them.

'Option Explicit

Const OverWriteFiles = True

Dim sFolder
Dim dFolder
Dim args
Dim objFSO

IF Wscript.Arguments.Count < 2 THEN
Wscript.Echo &quot;Syntax is: cscript CopySp3.vbs SourceDir DestinationDir&quot;
Wscript.Quit
End IF

sFolder = Wscript.Arguments(0)
dFolder = Wscript.Arguments(1)
args = Wscript.Arguments.Count

Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
objFSO.CopyFolder Wscript.Arguments(0) , Wscript.Arguments(1) , OverWriteFiles


Regards
Steve Friday
 
Thanks Steve,
It is working with variables declared and placed. In order to use the 'args' variable I edited the script as below. One thing, if I map a drive to the source and run the command it does not copy the files at the root of the mapped drive.

Option Explicit
On Error Resume Next

Const OverWriteFiles = True

Dim sFolder
Dim dFolder
Dim args
Dim objFSO

args = Wscript.Arguments.Count

IF args < 2 THEN
Wscript.Echo &quot;Syntax is: cscript CopySp3.vbs SourceDir DestinationDir&quot;
Wscript.Quit
End IF

sFolder = Wscript.Arguments(0)
dFolder = Wscript.Arguments(1)

Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
objFSO.CopyFolder sFolder , dFolder , OverWriteFiles


Thanks again for your help.
Simpson
 
Simpson,

Try using UNC naming instead of a mapped drive.

Steve
Regards
Steve Friday
 
Steve
Actually I used

objFso.copyfiles

And that worked fine

Thanks again for your help

Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top