vb.net string help

CalmSoul
CalmSoul used Ask the Experts™
on
Hello All:

I have following the following string value

I just want to grab the file name and put in a different string variable

Thu 07/09/2015 11:35:45.83 - Missing file: "x:\TAD Image\Alldocs\utility\H155559ZK.TXT" 

Open in new window


output

H155559ZK.TXT

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
How about something like this?

        Dim str1 As String = "Thu 07/09/2015 11:35:45.83 - Missing file: ""x:\TAD Image\Alldocs\utility\H155559ZK.TXT"
        Dim pos1 As Integer = str1.LastIndexOf("\") + 1
        Dim str2 As String = str1.Remove(0, pos1)
Commented:
Probably something like this:
Imports System.IO
Imports System.Runtime.CompilerServices

Module Module1
	Sub Main()
		Dim errorMessage = "Thu 07/09/2015 11:35:45.83 - Missing file: ""x:\TAD Image\Alldocs\utility\H155559ZK.TXT"""
		Console.WriteLine(errorMessage.GetErrorMessageFileName("Missing file:"))
		Console.ReadLine()
	End Sub
End Module

Module Extensions
	<Extension()> _
	Public Function GetErrorMessageFileName(ByVal source As String, ByVal filter As String) As String
		Dim file As FileInfo
		Dim result As String = String.Empty
		Dim index = source.IndexOf(filter, StringComparison.OrdinalIgnoreCase) + filter.Length
		If index <> -1 Then
			Try
				file = New FileInfo(source.Substring(index, source.Length - index).Trim().Trim(""""))
				If file IsNot Nothing Then
					result = file.Name
				End If
			Catch ex As Exception
				result = String.Empty
			End Try
		End If
		Return result
	End Function
End Module

Open in new window

Which produces the following -Capture.JPG-saige-
You could also do it like this.

Module Module1
    Sub Main()
        Dim strMessage = "Thu 07/09/2015 11:35:45.83 - Missing file: ""x:\TAD Image\Alldocs\utility\H155559ZK.TXT"""
        Dim vPos1 As Integer = strMessage.LastIndexOf("\")
        If vPos1 > 0 Then
            Dim str2 As String = Replace(Mid(strMessage, vPos1 + 2), Chr(34), "")
            Console.WriteLine(str2)
        Else
            Console.WriteLine("ERROR Could not find \ character in: " & strMessage)
        End If
        Console.ReadLine()
    End Sub
End Module

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
...and yet another one:
Dim data As String = "Thu 07/09/2015 11:35:45.83 - Missing file: ""x:\TAD Image\Alldocs\utility\H155559ZK.TXT"""
Dim fileName As String = data.Split("\").Last.TrimEnd("""")
MessageBox.Show(fileName)

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial