Hello again, I am trying to replace a string which is actually different from line to line in a text file. I want to end up with a filename but without the path. For example:

My text file contains:
C:\test\testingmore\filename.mpg
C:\test\testfiles1\testfile4.mpg
W:\testinglocation\testingmore\testfiles9.mpg

I would like to replace the path in each line with nothing so it reads as follows:

filename.mpg
testfile4.mpg
testfiles9.mpg

I have the following code written but I have something wrong as I get the error below:
parsing "*\" - Quantifier {x,y} following nothing.

Imports System.Text
Imports System

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fso1, inputFile1, outputFile1
        Dim str0 As String
        Dim str10 As String
        Dim str20 As String
        Dim str30 As String
        fso1 = CreateObject("Scripting.FileSystemObject")
        inputFile1 = fso1.OpenTextFile("c:\WDrive.txt", 1)
        str0 = inputFile1.ReadAll
        str10 = System.Text.RegularExpressions.Regex.Replace(str0, "*\", "")
        str20 = str10.Replace(".mpg", "")
        str30 = str20.Replace(".ts", "")
        inputFile1.close()
        outputFile1 = fso1.CreateTextFile("c:\WDriveReplaced.txt", True)
        outputFile1.Write(str30)
        outputFile1.close()
    End Sub
End Class

You could try the following (untested) console application

Imports System.IO
Imports System
Module Module1

    Sub Main()

        Dim stw As New StreamWriter("c:\WDriveReplaced.txt", False)
        Dim str As New StreamReader("c\WDrive.txt")

        While (Not str.EndOfStream)
            stw.WriteLine(Path.GetFileName(str.ReadLine()))
        End While
        stw.Flush()
        stw.close()
        str.close()
    End Sub


End Module

That doesn't seem to be what I am looking for, but thanks for the effort!

I'm more familiar with regular expressions in perl, but I think a backslash is an escape character. Try using "*\\".

No luck. same error. Still looking for a solution.
Thanks for your input.

Ok I just tested this out and it works. Try:

System.Text.RegularExpressions.Regex.Replace(str, "^.*\\", "")

You put a * in but didn't add the . to let it know what to look for zero or more of. I would also add the caret to let it know to start at the beginning of the line, just in case.

That does work for one line. Is there a reason it doesn't replace this in all the lines?

Ok I just reproduced the whole code you have there. Try:

"[A-Z]:.*\\" for your regular expression

That will match a capital letter followed by a colon, zero or more characters and a backslash. You may have to do something different if your drive letters also come in lowercase, replace [A-Z] with [A-Z|a-z]

I just tested it, that should do the trick.

That worked perfectly. Thank you so much for the help. I've learned a lot about regex. Very useful, but very complicated for a newbie.

Thanks again

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.