Rename a file - New to renaming

I have files with the following names:
Doe, Jane 123 example.pdf
Doe, Jane a34 example.pdf

I would like the end result to be Doe, Jane.pdf (Drop everything after Jane and keep the .pdf ext)

Thanks in advance…mj

If the pattern always is Lastname, Firstname xxx XXXXX.ext then you can use the following regular expression to remove the unwanted part:

(?<=^\w+,\s\w+)\s.+(?=\.\w+$)

image

oldFile = "C:\Test\Doe, Jane 123 example.pdf"
newFile = System.Text.RegularExpressions.Regex.Replace(Path.GetFileName(oldFile), "(?<=^\w+,\s\w+)\s.+(?=\.\w+$)", "")
My.Computer.FileSystem.RenameFile(oldFile, newFile)
1 Like