General discussion

Locked

VB File Copy script failing

By Matt H ·
Can anyone otut there tell me why this ain't working? The echo's are so I could see how far it was getting. I have a 'Expected If' error but have had all sorts. Need to be able to pass it the filename from the command line. It'll be something really stupid I know, but I can't see it. Not very good at VBS yet.

MAny thanks


Option Explicit


Dim strFilename
Dim fsoCopy
Dim strDestination


wscript.echo "...1"

strDestination = "\\192.168.xxx.xxx\Dropin\test\"
Set strFilename = WScript.Arguments(0)
Set fsoCopy = CreateObject("Scripting.FileSystemObject")

wscript.echo "...2"

If Instr(strFilename, "ian") > 0 Then

wscript.echo "...2.1"

fsoCopy.CopyFile strFilename, strDestination & "ian"

wscript.echo "...3"

Else
If Instr(strFilename, "fred") > 0 Then

wscript.echo "...3.1"


fsoCopy.CopyFile strFilename, strDestination & "fred"

wscript.echo "...3.2"


wscript.echo "...4"

Else
If Instr(strFilename, "malcolm") > 0 Then
fsoCopy.CopyFile strFilename, strDestination & "malcolm"

wscript.echo "...5"

ElseIf inStr(strFilename, "test.exe") > 0 Then
fsoCopy.CopyFile strFilename, strDestination & "test"

End If
wscript.echo "..." & strFilename

end

This conversation is currently closed to new comments.

4 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by alan williams In reply to VB File Copy script faili ...

where is it failing and what are the error messages?
Note:- if you are checking for names, you need to deo an exact match. Instr 'ian' will catch Brian as well as any other name with ian in the text. Also from memory, command line argument 0 used to be the program name, but they might have changed this - well if it uses the right value.

Collapse -

by dshaffer In reply to VB File Copy script faili ...

It's VERY helpful with VBScript (or any language) to indent If...Then statements, especially when they are nested. If you do, you'll see you're missing an 'End If', notated ***End If*** below:

If InStr(strFilename, "ian") > 0 Then
fsoCopy.CopyFile strFilename, strDestination & "ian"
Else
If InStr(strFilename, "fred") > 0 Then
fsoCopy.CopyFile strFilename, strDestination & "fred"
Else
If InStr(strFilename, "malcolm") > 0 Then
fsoCopy.CopyFile strFilename, strDestination & "malcolm"

ElseIf InStr(strFilename, "test.exe") > 0 Then
fsoCopy.CopyFile strFilename, strDestination & "test"
***End If***
End If

Hope that helps

Collapse -

by dshaffer In reply to VB File Copy script faili ...

It's VERY helpful with VBScript (or any language) to indent If...Then statements, especially when they are nested. If you do, you'll see you're missing an 'End If', notated ***End If*** below:

If InStr(strFilename, "ian") > 0 Then
fsoCopy.CopyFile strFilename, strDestination & "ian"
Else
If InStr(strFilename, "fred") > 0 Then
fsoCopy.CopyFile strFilename, strDestination & "fred"
Else
If InStr(strFilename, "malcolm") > 0 Then
fsoCopy.CopyFile strFilename, strDestination & "malcolm"

ElseIf InStr(strFilename, "test.exe") > 0 Then
fsoCopy.CopyFile strFilename, strDestination & "test"
***End If***
End If

Hope that helps

Collapse -

by dshaffer In reply to VB File Copy script faili ...

It's WAY too early to be thinking. I missed another End If, and copying and pasting didn't do the indents. Let's try this again and hope the indents (and my brain) work:

If InStr(strFilename, "ian") > 0 Then
fsoCopy.CopyFile strFilename, strDestination & "ian"
ElseIf InStr(strFilename, "fred") > 0 Then
fsoCopy.CopyFile strFilename, strDestination & "fred"
ElseIf InStr(strFilename, "malcolm") > 0 Then
fsoCopy.CopyFile strFilename, strDestination & "malcolm"
ElseIf InStr(strFilename, "test.exe") > 0 Then
fsoCopy.CopyFile strFilename, strDestination & "test"
End If

You could also do a Select...Case structure, which is a bit cleaner than ElseIf for multiple cases:

strName = InStr(strFilename)
Select Case strName
Case "ian"
fsoCopy.CopyFile strFilename, strDestination & "ian"
Case "fred"
fsoCopy.CopyFile strFilename, strDestination & "fred"
Case "malcolm"
fsoCopy.CopyFile strFilename, strDestination & "malcolm"
Case Else
fsoCopy.CopyFile strFilename, strDestination & "test"
End Select

Back to Web Development Forum
4 total posts (Page 1 of 1)  

Related Discussions

Related Forums