Question

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
Was it helpful?

Solution

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.

OTHER TIPS

I am not sure how your script works so I put this HTML application together for you. It uses a batch file called Dir.Bat located in C:\Batch which makes a file called Data.Txt located in c:\Temp. Then the script takes over. The script reads the file Data.Txt line by line. As each line is read two split statements are used to separate out the string in the text file's name. After that I collect those strings containing numbers into the variable ListCol as I test for larger and larger numbers. I finally wind up with the largest number which I place in your original variable tempHighNum. I will post the HTA file and the Dir.Bat file. I know I did not write the script as a function using a parameter so if you really need to use a parameter, I will try to help you by changing the HTA file to make it possible to enter the path and file name in a TextBox. That should make it easy to change and use. I added and changed a thing or two to make it run smoother. I am not sure how your script works so I put this HTML application together for you. It uses a batch file called Dir.Bat located in C:\Batch which makes a file called Data.Txt located in c:\Temp. Then the script takes over. The script reads the file Data.Txt line by line. As each line is read two split statements are used to separate out the string in the text file's name. After that I collect those strings containing numbers into the variable ListCol as I test for larger and larger numbers. I finally wind up with the largest number which I place in your original variable tempHighNum. I will post the HTA file and the Dir.Bat file. I know I did not write the script as a function using a parameter so if you really need to use a parameter, I will try to help you by changing the HTA file to make it possible to enter the path and file name in a TextBox. That should make it easy to change and use. I added and changed a thing or two to make it run smoother.

<HTML><!-- C:\HTML_and_HTA_CODE_EXAMPLES\ATest.Hta -->
    <HEAD>
        <TITLE>ATest.Hta</TITLE>
        <HTA:APPLICATION ID="HTA MyApp" 
          APPLICATIONNAME="Help4Saul Dolgin"
          BORDER         ="thick"
          BORDERSTYLE    ="complex"
          CAPTION        ="yes"
          CONTEXTMENU    ="no"
          ICON           ="http://Your URL/your icon.ico"
          INNERBORDER    ="yes"
          MAXIMIZEBUTTON ="yes"
          MINIMIZEBUTTON ="yes"
          NAVIGABLE      ="no"
          SCROLL         ="no"
          SHOWINTASKBAR  ="yes"
          SINGLEINSTANCE ="yes"
          SYSMENU        ="yes"
          VERSION        ="1.0"
          WINDOWSTATE    ="Normal"/>
    </HEAD>
    <style>
        .ExBt21    {background:"#E0E0E0";Color:"red";}/* For Exit Button */
        .Spn4      {font-family:"arial";font-weight:"bold";Color:"blue"}
        .Spn2      {Color:"red"}
        .tAr1      {font-family:"arial";font-weight:"bold";Color:"blue"}
    </style>
    <SCRIPT Language="VBScript">
        Sub GetFileNumber
            Dim FSO, f, fc, tempHighNum, strLine, objSHO, line
            Dim DataArr, Data1Arr, Data2Arr, ListCol
            fc=""
            ListCol=""
            tempHighNum=000
            Set objSHO=CreateObject("WScript.Shell")
            objSHO.run "C:\Batch\Dir.bat"
            Set FSO = CreateObject( "Scripting.FileSystemObject" )
            Set f = FSO.OpenTextFile("c:\Temp\Data.Txt", "1")
            Do Until f.AtEndOfStream
            fc = fc & f.ReadLine & vbLf
            Loop
            tArea1.innerHTML=fc
            Data1Arr = Split(fc,vbLf)
            Count=UBound(Data1Arr)
            For x=0 To Count
            DataArr = Split(Data1Arr(x),".")
            If x <= Count-1 Then
                Data2Arr = Split(DataArr(0),"_")
                ListCol = ListCol & Data2Arr(1) & vbLf
                If Data2Arr(1) > tempHighNum Then
                    tempHighNum = Data2Arr(1)
                End If
            End If
            Next 
            Span2.innerHTML=tempHighNum
        End Sub
        Sub ExtBtn:Window.close:End Sub' Exit Script For Window
    </SCRIPT>
    <BODY bgcolor="#D0D0D0">
        <button OnClick="GetFileNumber">Button To Press</button><br/>
        <span id="Span1" class="Spn1">The biggest No. is:&nbsp;</span><span id="Span2" class="Spn2"></span><br/><br/>
        <span Id="Span3" Class="Spn3">Dir.Bat looks in:&nbsp;</span><span Id="Span4" Class="Spn4">C:\Temp\Log_???.Txt</span><br/>
        <textarea Id="tArea1" class="tAr1" rows="10"></textarea><br/><br/>
        <input id="ExBtn21" class="ExBt21"  type="Button" name="ExitBtn21" OnClick="ExtBtn" value="&nbsp-Exit-&nbsp"/><!-- Exit Button -->
    </BODY>
</HTML>

The Dir.Bat file is next:

@Echo Off
Dir/B c:\Temp\Log_???.Txt>c:\Temp\Data.Txt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top