我是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只是一个参数吗?
答案 0 :(得分:3)
在VBScript中,每个变量都没有一些内置方法。如果变量具有属性或方法,则意味着它是一个Object。但是你的参数似乎不是一个对象,这就是错误发生的原因。
因此,对于VBScript中的字符串变量,没有内置方法,如 SubString 或其他方法。
.Length
。.SubString
,请使用Mid,Left或Right个功能。我猜你需要在这种情况下使用 - 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