DaleHutch
DaleHutch

Reputation: 1

VB script to locate file

I am requesting some help with a simple VB script I need to deploy to catch a few computers who only connect to my domain a few times a month and check for a file.

What I basically need is to see if a file is there. If it is.. Stop.

If not... Then look for a folder. If there, do this. Then stop. If not there, create folder and then do this.

This is what I have. I get and error on line 9 when the file is there and all I want is to stop the script.

Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")    
If fso.FileExists("C:\output.log") Then
Else
    fso.CreateFolder("C:\temp") 
     fso.CopyFolder "\\DOMAIN\FOLDER", "c:\temp\FOLDER"
End If
If fso.FolderExists( "C:\temp") 
    fso.CopyFolder "\\DOMAIN\FOLDER", "c:\temp\FOLDER" 
Else
    fso.CreateFolder("C:\temp")
    fso.CopyFolder "\\DOMAIN\FOLDER", "c:\temp\FOLDER"
End If

Upvotes: 0

Views: 55

Answers (1)

JNevill
JNevill

Reputation: 50034

You can stick your script into a function or subroutine and then call that subroutine. Subroutines and functions can be exited:

Option Explicit

main

Sub main
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")    
    If fso.FileExists("C:\output.log") Then
        Exit Sub
    Else
        fso.CreateFolder("C:\temp") 
        fso.CopyFolder "\\DOMAIN\FOLDER", "c:\temp\FOLDER"
    End If
    If fso.FolderExists( "C:\temp") 
        fso.CopyFolder "\\DOMAIN\FOLDER", "c:\temp\FOLDER" 
    Else
        fso.CreateFolder("C:\temp")
        fso.CopyFolder "\\DOMAIN\FOLDER", "c:\temp\FOLDER"
    End If
End Sub

Your script will execute and the first thing you do is call the main sub. Which then runs and if the condition is right, exits. Since there are no more commands after we call main the script ends.

Upvotes: 1

Related Questions