hi i would like to know how can I checked the name of a file and then retrieve only the first 6 characters.

the format of the file names is: dd1234_10x2.xsl

i need to get just the first 6 characters up to the underscore, but if the name of the file does not contain an underscore after the first 6 chars, i.e. dd1234.10x10.xsl

then i need to skip that file and continue to the next file on that folder.

i hope this is clear.

thank you.

pelusa.

just adding to my question,

my program receives a 6 character number

the function that i currently use looks like this:


Private Function SearchLabelTemplate(ByVal strItemNumber As String, ByRef m_strMessage As String) As Boolean

Dim blnSuccess As Boolean

' Make a reference to the directory
Dim di As New IO.DirectoryInfo(m_strDirectoryPath)
Dim diar As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo

blnSuccess = False

' List the names of all files in the directory
If Not blnSuccess Then
For Each dra In diar

If (dra.Name.Substring(0, 6) = strItemNumber) Then
m_strMessage = dra.Name
blnSuccess = True
Exit Try
Else
m_strMessage = "NO TEMPLATE"
End If
Next
End If

blnSuccess = True

Return blnSuccess

End Function


so if this part is true

If (dra.Name.Substring(0, 6) = strItemNumber) dd4556 = dd4556


the program exits fine but before this is done, i need to make sure that there is an underscore right after the 6th character on the item number. how can i test for this?

Try something like this:

Dim FileName As String = "12345_789.567"
        Dim Index As Integer = FileName.IndexOf("_") + 1
        If Index = 6 Then
            ' do whatever if the Index=6
        End If

i am doing it this way, but i think i am not using the .net fully potential and that my code looks more vb6

i like your suggestion, but would you be able to show me how to incorporated into my solution, and if it's worth to do it?

thank you much.

If Not blnSuccess Then
For Each dra In diar

If (Mid(dra.Name, 7, 1) = "_") Then
If (dra.Name.Substring(0, 6) = Trim(strItemNumber)) Then
m_strMessage = Trim(dra.Name)
blnSuccess = True
Exit Try
Else
blnSuccess = False
m_strMessage = m_strTemplateNotFound
End If
Else
' Keep a log of all the files that have an unvalid format
m_objLogger.WriteLog("The following file was created using an unvalid template format: " & Trim(dra.Name), LogLevel.Informational, "")
End If
Next
End If

try

Dim FileName As String = "12345_789.567"
If FileName.contains("_") Then
' do whatever 
End If

I don't know the structures of your variables but here is a guess.

If Not blnSuccess Then
            For Each dra In diar
                Dim Index As Integer = dra.name.IndexOf("_")
                If Index Then
                    If (dra.name.Substring(0, Index) = Trim(strItemNumber)) Then
                        m_strMessage = Trim(dra.name)
                        blnSuccess = True
                    Else
                        blnSuccess = False
                        m_strMessage = m_strTemplateNotFound
                    End If
                Else
                    ' Keep a log of all the files that have an unvalid format
                    m_objLogger.WriteLog("The following file was created using an unvalid template format: " & Trim(dra.Name), LogLevel.Informational, "")
                End If
            Next
        End If

for this line in your suggestion

Dim intIndex As Integer = dra.Name.IndexOf("_")

If intIndex Then

does it have to be

Dim intIndex As Integer = dra.Name.IndexOf("_")

If intIndex > 0 Then

not sure, but if there is no underscore then i get a -1 and thus an exception.

thank you,

pelusa.

You are right. If intIndex is false if 0 otherwise any number + or - is true and IndexOf returns a -1 if it is not there. So use If intIndex > 0.
My bad.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.