We help IT Professionals succeed at work.

How to trim a string in vb6.0 to get filename

RIAS
RIAS asked
on
Hi,
I have a string :
str1 = Documents_1\dx1\40699948_Box01_Batch000001_00000001.pdf

How do I trim the string to
str1=40699948_Box01_Batch000001_00000001.pdf

i.e just get filename and not the filepath

Cheers
Comment
Watch Question

Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
If you JUST want to remove the slases then do

str1 = Replace(str1, "\", "")

To get rid of the documents part do a second replace

str1 = Replace(str1, "Documents_", "")
Two different ways of doing this here: http://inovacreations.com/project_home/vb_code_bank/files_folders/get-filename-from-path/

Another method here: http://www.freevbcode.com/ShowCode.asp?ID=10

If you are certain that there will be two '\' then you could use:
str1=mid(path,instr(instr(1,path,"\")+1,path,"\")+1)
use the InStrRev function to find position of the the "\" from end

& use the Split function

Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
In my haste to answer you question I misread it.

Dim intPos As Integer

intPos = InStrRev(str1, "\")
str1 = Mid$(str1, intPos + 1)

Author

Commented:
Brilliant mate!!!works like charm...Cheers