file.exists ?

punker

New member
Joined
Dec 3, 2009
Messages
3
Programming Experience
Beginner
hi i'm new to programming, i'm trying to check whats the highest rev number in a folder (file names are 123456_rev01.*, 123456_rev02.*).

here is my code

For j = 1 To File.Exists("c:\test\123456" & "*" & j & ".*") = False

MsgBox("looking at rev" & j)
j = j + 1
Next
MsgBox("last heighest rev is" & j)

when i run it it just goes through the loop once even though I have the following files in that folder;
123456_rev01.dwf
123456_rev02.pdf
123456_rev03.pdf

All i want is the number 3 (highest rev).
 
What does a For loop do? It loops over some code while a counter goes from a start value to and end value. Those values have to be numbers. What numbers are you using?

start = 1
end = (File.Exists("c:\test\123456" & "*" & j & ".*") = False)

Exactly what do you expect that end value to evaluate to? File.Exists doesn't support wildcards so that will always be False, and False is always equal to False so the whole expression will evaluate to True. The end value MUST be a number so that's being implicitly converted to 1. As such, your For loop counter is going from 1 to 1, i.e. a single iteration.

You're going to have to use a different approach to do what you want to do. You're going to have to use Directory.GetFiles if you want to use a wildcard, then you'll need to extract out the parts of the file names that you're interested in. If the file names are really that uniform then you really only need to look at the last one in the array returned by GetFiles because it will have the highest number.
 
jmcilhinney

thanks for your response. I understand now.

All i'm trying to do is look in a folder (specified earlier in the code) for a file starting with a number (specified earlier in the code) eg 123456 with the highest last number in filename eg. 123456_rev01.xxx = 1. if there is a file 123456_rev03.xxx then = 3 etc. regardless of extension.
 
Do yourself a favor and Turn Option Strict On.

To find the highest number, something like this should work pretty well:

VB.NET:
Imports System.Convert
Imports System.IO

' ...your function...

        Dim allFiles() As String = Directory.GetFiles("yourDirHere")
        Array.Sort(Of String)(allFiles, AddressOf YourFileComparer)

        Dim newstRev As Integer = ToInt32(allFiles(allFiles.Length - 1).Substring(9, 2))

' ...your function ends...

    Friend Shared Function YourFileComparer(ByVal x As String, ByVal y As String) As Integer
        If ToInt32(x.Substring(9, 2)) = ToInt32(y.Substring(9, 2)) Then
            YourFileComparer = 0
        Else
            If ToInt32(x.Substring(9, 2)) > ToInt32(y.Substring(9, 2)) Then
                YourFileComparer = 1
            Else
                YourFileComparer = -1
            End If
        End If
    End Function
 
Back
Top