Home All Groups Group Topic Archive Search About

Determine if: > .dat < is in a string

Author
26 Feb 2007 2:13 PM
Randy Gardner
I need to determine file type from the file path and name which I get from
the CommonDialog control.

What is the best way?

--
Thank you,

Randy

Author
26 Feb 2007 2:57 PM
Rick Rothstein (MVP - VB)
> I need to determine file type from the file path and name
> which I get from  the CommonDialog control.

As long as the cdlOFNNoValidate flag is NOT set (I can't think of any reason
it should be), you can use this code to get the file's name and extension...

  Dim FilesName As String
  Dim FilesExtension As String
  Dim NameParts() As String
  '  Set Common Dialog Box flags here
  CommonDialog1.ShowOpen
  NameParts = Split(CommonDialog1.FileTitle & ".", ".")
  FilesName = NameParts(0)
  FilesExtension = NameParts(1)

Rick
Author
26 Feb 2007 3:52 PM
Jan Hyde
"Rick Rothstein \(MVP - VB\)"
<rickNOSPAMnews@NOSPAMcomcast.net>'s wild thoughts were
released on Mon, 26 Feb 2007 09:57:40 -0500 bearing the
following fruit:

Show quoteHide quote
>> I need to determine file type from the file path and name
>> which I get from  the CommonDialog control.
>
>As long as the cdlOFNNoValidate flag is NOT set (I can't think of any reason
>it should be), you can use this code to get the file's name and extension...
>
>  Dim FilesName As String
>  Dim FilesExtension As String
>  Dim NameParts() As String
>  '  Set Common Dialog Box flags here
>  CommonDialog1.ShowOpen
>  NameParts = Split(CommonDialog1.FileTitle & ".", ".")
>  FilesName = NameParts(0)
>  FilesExtension = NameParts(1)

Hmm..

Jan.Hyde.txt

I've come across a lot of documents like that
Author
26 Feb 2007 4:37 PM
Rick Rothstein (MVP - VB)
> Hmm..
>
> Jan.Hyde.txt
>
> I've come across a lot of documents like that

Picky...picky...picky  <g>

Okay, so much for off the top of the head code. Well, let's try one more off
the top of my head solution<g>...

  Dim FilesName As String
  Dim FilesExtension As String
  Dim NameParts() As String
  CommonDialog1.ShowOpen
  NameParts = Split(CommonDialog1.FileTitle, ".")
  If UBound(NameParts) Then
    FilesExtension = NameParts(UBound(NameParts))
    ReDim Preserve NameParts(UBound(NameParts) - 1)
  End If
  FilesName = Trim$(Join(NameParts, "."))

I think this code works.

Rick
Author
26 Feb 2007 5:08 PM
Randy Gardner
Thanks!

I'll try it, if no worky, I'll repost
--
Thank you,

Randy


Show quoteHide quote
"Rick Rothstein (MVP - VB)" wrote:

> > Hmm..
> >
> > Jan.Hyde.txt
> >
> > I've come across a lot of documents like that
>
> Picky...picky...picky  <g>
>
> Okay, so much for off the top of the head code. Well, let's try one more off
> the top of my head solution<g>...
>
>   Dim FilesName As String
>   Dim FilesExtension As String
>   Dim NameParts() As String
>   CommonDialog1.ShowOpen
>   NameParts = Split(CommonDialog1.FileTitle, ".")
>   If UBound(NameParts) Then
>     FilesExtension = NameParts(UBound(NameParts))
>     ReDim Preserve NameParts(UBound(NameParts) - 1)
>   End If
>   FilesName = Trim$(Join(NameParts, "."))
>
> I think this code works.
>
> Rick
>
>
>
Author
26 Feb 2007 10:34 PM
DanS
Show quote Hide quote
"Rick Rothstein \(MVP - VB\)" <rickNOSPAMnews@NOSPAMcomcast.net> wrote
in news:OsX8vRcWHHA.388@TK2MSFTNGP04.phx.gbl:

>> Hmm..
>>
>> Jan.Hyde.txt
>>
>> I've come across a lot of documents like that
>
> Picky...picky...picky  <g>
>
> Okay, so much for off the top of the head code. Well, let's try one
> more off the top of my head solution<g>...
>
>   Dim FilesName As String
>   Dim FilesExtension As String
>   Dim NameParts() As String
>   CommonDialog1.ShowOpen
>   NameParts = Split(CommonDialog1.FileTitle, ".")
>   If UBound(NameParts) Then
>     FilesExtension = NameParts(UBound(NameParts))
>     ReDim Preserve NameParts(UBound(NameParts) - 1)
>   End If
>   FilesName = Trim$(Join(NameParts, "."))
>
> I think this code works.
>
> Rick
>
>

What's wrong with a one-liner ?!?!?!!??.....

FileName = mid$(CommonDialog1.FileTitle, _
                InStrRev (CommonDialog1.FileTitle,"\") + 1)
Author
26 Feb 2007 10:50 PM
Rick Rothstein (MVP - VB)
> What's wrong with a one-liner ?!?!?!!??.....
>
> FileName = mid$(CommonDialog1.FileTitle, _
>        InStrRev (CommonDialog1.FileTitle,"\") + 1)

Nothing is wrong with a one-liner, but do you realize that all of the code
you have there produces the same result as this?

     FileName = CommonDialog1.FileTitle

By the way, the question asked how to get the extension (I threw in the file
name part as a bonus<g>).

Rick
Author
26 Feb 2007 11:27 PM
DanS
Show quote Hide quote
"Rick Rothstein \(MVP - VB\)" <rickNOSPAMnews@NOSPAMcomcast.net> wrote
in news:OpNjBlfWHHA.996@TK2MSFTNGP02.phx.gbl:

>> What's wrong with a one-liner ?!?!?!!??.....
>>
>> FileName = mid$(CommonDialog1.FileTitle, _
>>        InStrRev (CommonDialog1.FileTitle,"\") + 1)
>
> Nothing is wrong with a one-liner, but do you realize that all of the
> code you have there produces the same result as this?
>
>      FileName = CommonDialog1.FileTitle
>


DOH !!!!!!! I didn't notice you were using FileTitle from the comDlg
control, and just blindly typed away. (I rarely use the comDlg.)

> By the way, the question asked how to get the extension

Then just change the "\" to "." in the code above and you have the
extension only then.

The (still a) one-liner would be....

Extension = mid$(CommonDialog1.FileTitle, _
        InStrRev (CommonDialog1.FileTitle,".") + 1)

Show quoteHide quote
> (I threw in the file name part as a bonus<g>).
> Rick
Author
27 Feb 2007 9:05 AM
Jan Hyde
"Rick Rothstein \(MVP - VB\)"
<rickNOSPAMnews@NOSPAMcomcast.net>'s wild thoughts were
released on Mon, 26 Feb 2007 11:37:41 -0500 bearing the
following fruit:

>> Hmm..
>>
>> Jan.Hyde.txt
>>
>> I've come across a lot of documents like that
>
>Picky...picky...picky  <g>

I could have included my middle name

Jan.Pedantic.Hyde.txt

;-)

J

Show quoteHide quote
>Okay, so much for off the top of the head code. Well, let's try one more off
>the top of my head solution<g>...
>
>  Dim FilesName As String
>  Dim FilesExtension As String
>  Dim NameParts() As String
>  CommonDialog1.ShowOpen
>  NameParts = Split(CommonDialog1.FileTitle, ".")
>  If UBound(NameParts) Then
>    FilesExtension = NameParts(UBound(NameParts))
>    ReDim Preserve NameParts(UBound(NameParts) - 1)
>  End If
>  FilesName = Trim$(Join(NameParts, "."))
>
>I think this code works.
>
>Rick
>