views:

347

answers:

1

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
+10  A: 

Change this line:

Set tempHighNum = "000"

to the following:

tempHighNum = "000"

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.

Saul Dolgin
beat me to it.. I confirm.
madcolor
That fixed it. Thank you.
Jeremy Cron