jayjyli
jayjyli

Reputation: 821

Object required of a parameter in VBScript?

I'm new to VBScript, and I have a function that allows me to pull synchronizing preferences from a preferences file, and it looks like this:

Function IsSync(SyncFolder)
    If FS.FileExists(PrefFilePath) Then
        Set objFile = FS.OpenTextFile(PrefFilePath, 1)
        PrefLine = "start"
        Do Until Prefline.Substring(0, SyncFolder.Length) = SyncFolder
            PrefLine = objFile.Readline
        Loop

        If PrefLine.Substring(PrefLine.Length - 6) = "nosync" Then
            IsSync = False
        Else
            IsSync = True
        End If
    Else
        IsSync = True
    End If
End Function

But when I try to run it, Windows throws me an error of "Object required: SyncFolder" whenever it gets to this function. Why is this? SyncFolder is just a parameter?

Upvotes: 0

Views: 2570

Answers (1)

Kul-Tigin
Kul-Tigin

Reputation: 16950

In VBScript, every variable has not some built-in methods. And if a variable has a property or method this means it's an Object. But your parameter does not seems like an object, this is why the error occurred.
So, there is no built-in methods such as SubString or another for the string variables in the VBScript.

  1. Use Len function to get length of string instead of .Length.
  2. Use Mid, Left or Right functions which one if you need to istead of .SubString.

I guess you need to use -with order- Len, Left and Right functions in this case.

Consider this :

Function IsSync(SyncFolder)
    If FS.FileExists(PrefFilePath) Then
        Set objFile = FS.OpenTextFile(PrefFilePath, 1)
        PrefLine = "start"
        Do Until Left(Prefline, Len(SyncFolder)) = SyncFolder 'starts with SyncFolder
            PrefLine = objFile.Readline
        Loop

        If Right(PrefLine, 5) = "nosync" Then 'ends with "nosync"
            IsSync = False
        Else
            IsSync = True
        End If
    Else
        IsSync = True
    End If
End Function

Upvotes: 3

Related Questions