VBScript For Each 不起作用 [英] VBScript For Each isn't working
问题描述
我无法在 VBScript 中使用以下函数.我正在尝试获取文件夹中的所有文件并遍历它们以获取编号最高的文件.(文件名格式为 log_XXX.txt)我遇到的问题是代码永远不会进入我的 For Each
循环.我是 VBScript 的新手,但我似乎不明白为什么这行不通.
I can't get the following function in VBScript to work. I am trying to get all of the files in a folder and loop through them to get the highest numbered file. (file name format is log_XXX.txt) The problem that I am having is that the code never enters my For Each
loop. I am new to VBScript, but I don't seem to understand why this won't work.
Function GetFileNumber(folderspec)
Dim fso, f, f1, fc, s, tempHighNum
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
WScript.Echo f.Files.Count : rem prints 3
Set fc = f.Files
WScript.Echo fc.Count : rem prints 3
Set tempHighNum = "000"
For Each f1 in fc
WScript.Echo f1.Size : rem does not print
WScript.Echo f1.Type : rem does not print
WScript.Echo f1.Name : rem does not print
s = Right(f1.name,3)
IF NOT(ISNULL(s)) THEN
IF (s > tempHighNum) THEN
tempHighNum = s
END IF
END IF
Next
GetFileNumber = tempHighNum
End Function
推荐答案
更改这一行:
Set tempHighNum = "000"
以下内容:
tempHighNum = "000"
您正试图将 tempHighNum 变量设置为字符串类型.因此,您不应使用 Set 关键字.Set 仅在将对象类型分配给变量时才需要.
You are attempting to set the tempHighNum variable to a string type. Therefore, you should not use the Set keyword. Set is only needed when assigning object types to variables.
这篇关于VBScript For Each 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!