Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to catch certain word from sentence..

Status
Not open for further replies.

zaq888

MIS
Jun 2, 2003
46
0
0
MY
hi, i'm newbie in vb... i have a sentence in a txtbox...

e.g:

C:\temp\newfolder\ex030303.log

so, how can i catch up the word starting ex030303 and diplays the result at label1.text?

the txtbox is consider as the file location. i just want to displays the filename.. n not the exaccally location of the file.... can anybody help me... and for ur info the file location cam be changed... means... it can be in

c:\temp\ex030303.log

the main point is the file name is started with word ex-mm-dd-yy.

I'm Keep Studying.... please show the way...
Not Good in English
 
Well, since the length of the name is fixed ( it's always 8 characters and has .log attached )

Assumeing the control text1 has the text that you want to get the name from:

Label1.Caption = Right$(Text1.Text,12)

Hope this helped,

Robert
 

Hi:

If the filename is always the same length, then one could use:
Code:
strFilename = Right(text1.text, 12)

Cassie
 
Hi all

I agree that if the filename is guaranteed to be in the format as stated, then the
Code:
right
command is a good method to adopt. If there is a chance of this not being the case, then a more general approach might be to use the file system object:

Code:
    Dim FSO
    Dim FileName As String
    Dim BaseName As String
    Dim FolderName As String
    Dim ExtenName As String
    Set FSO = CreateObject("Scripting.FileSystemObject")
    FileName = FSO.getfilename("C:\temp\newfolder\ex030303.log")
    BaseName = FSO.getbasename("C:\temp\newfolder\ex030303.log")
    FolderName = FSO.getparentfoldername("C:\temp\newfolder\ex030303.log")
    ExtenName = FSO.getextensionname("C:\temp\newfolder\ex030303.log")

Results:
FileName = ex030303.log
BaseName = ex030303
ExtenName = log
FolderName = c:\temp\newfolder

This will work with variable length filenames, and filenames / foldernames with embedded spaces as well.

There is also probably a solution to this using regular expressions (but I suspect that there are many out there who are better at RegExp than I am).

Hope this helps.
 
Or simply...

Dim ptr As Long

ptr = InStrRev(Text1.Text, "\")
Label1.Caption = Right(Text1.Text, Len(Text1.Text) - ptr)
 
the instr function will give you the position of the first occurence of a string inside another string

eg:

MsgBox InStr(1, "can i come to this", "come")

gives the result 7

but, for you specific problem, if you are not sure of the name of the file why don't you find the position of the last "\" and take all the characters after that position which will give the name of the file alone.

msgbox Mid$(fullfilename, InStrRev(fullfilename, "\") + 1)

it will return the file name alone
 
I always use Instr and for some reason the InStrRev instruction nevers enters my head when I need it. With the possiblity of double extensions you can't assume the file name is simple - the path structure does not appear to have alternative options though so looking for the solidus is foolproof - unless I am mistaken.
 
or an alternative solution maybe use the split command? i.e.

Dim strFilename As String
Dim arrFilename As Variant

strFilename = "C:\temp\newfolder\ex030303.log"
arrFilename = Split(strFilename, "\")
MsgBox arrFilename(UBound(arrFilename))
 
Thanks for the help guys..... this all stuff made me proud being as a member of this forums...... thanx a lot!!!!

I'm Keep Studying.... please show the way...
Not Good in English
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top