Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBScript For Each isn't working

Tags:

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
like image 253
Jeremy Cron Avatar asked Dec 17 '22 08:12

Jeremy Cron


1 Answers

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.

like image 184
Saul Dolgin Avatar answered Feb 24 '23 13:02

Saul Dolgin