VBScript中参数需要的对象?

时间:2011-12-13 16:02:02

标签: vbscript

我是VBScript的新手,我有一个允许我从首选项文件中提取同步首选项的函数,它看起来像这样:

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

但是当我尝试运行它时,只要它到达此功能,Windows就会向我抛出“Object required:SyncFolder”错误。为什么是这样? SyncFolder只是一个参数吗?

1 个答案:

答案 0 :(得分:3)

在VBScript中,每个变量都没有一些内置方法。如果变量具有属性或方法,则意味着它是一个Object。但是你的参数似乎不是一个对象,这就是错误发生的原因。
因此,对于VBScript中的字符串变量,没有内置方法,如 SubString 或其他方法。

  1. 使用Len函数获取字符串的长度而不是 .Length
  2. 如果您需要.SubString,请使用MidLeftRight个功能。
  3. 我猜你需要在这种情况下使用 - with order - Len Left Right 函数。

    考虑一下:

    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