Explore Courses Blog Tutorials Interview Questions
0 votes
in RPA by (12.7k points)

I have this string: "C:\Procesos\rrhh\CorteDocumentos\Cortados\10001662-1_20060301_29_1_20190301.pdf" and I'm trying to get this part: "20190301". The problem is the length is not always the same. It would be: "9001662-1_20060301_4_1_20190301".

I've tried this: item.ToString.Substring(66,8), but it doesn't work sometimes.

What can I do?.

1 Answer

0 votes
by (29.5k points)

what you can do is Get the filename without extension using Path.GetFileNameWithoutExtension Method then split the filename by '_' and get the last index of the array by using count - 1

I am mentioning example code below for your understanding :

Sub Main()
            Dim strFileName As String = ""
            Dim di As New DirectoryInfo("C:\Users\Prakhar\Desktop\test")
            Dim arrayFi As FileInfo() = di.GetFiles("*.pdf")
            Dim fi As FileInfo

            For Each fi In arrayFi

                Dim arrname() As String
                arrname = Split(Path.GetFileNameWithoutExtension(fi.Name), "_")
                strFileName = arrname(arrname.Count - 1)
       End Sub


Related questions

Browse Categories