Re: Look for a certain file name

Discussion in 'ASP .Net' started by Gregory A. Beamer, Oct 9, 2009.

  1. "pvong" <vonger@*dot*com> wrote in
    news::

    > Doing this in VB.Net
    >
    > I asked a similar question last month, but I still can not get it to
    > work right. All I want to do is look for files that start with the
    > word "sharp" and do something if it sees a file starting with this
    > name. All the files are .pdf files. The folder is located on the
    > local server running the IIS and I've checked the directory over and
    > over again and it's right. I just keep getting There are No Files.
    > I've attached an image of the listed files inside for testing.
    >


    I have answered this recently (albeit in C# as the OP did not specify
    language). If that was you that posted, you have more/different
    information here than before.

    This is off the top of my head, but the only thing that might need
    tweaking is the Regex statement

    Dim dirInfo as New DirectoryInfo(DirectoryPath)

    'Assume files in question START with sharp
    'SUCCESS sharpFOOBAR.pdf
    'FAIL SomethingSharp.pdf
    Dim reg As New Regex("^[Ss]harp")

    ForEach(filInfo as FileInfo in dirInfo.GetFiles())

    If (reg.Match(filInfo.Name).Success) Then
    Response.Write("Has Sharp File")
    Else
    Response.Write("No Sharp File")
    End If

    Next

    If you want a single success or fail, encapsulate this in a method and
    return true when you have a single success, like so:

    Private Function HasSharpFile(ByVal directoryPath As String) As Boolean

    Dim dirInfo as New DirectoryInfo(DirectoryPath)

    'Assume files in question START with sharp
    'SUCCESS sharpFOOBAR.pdf
    'FAIL SomethingSharp.pdf
    Dim reg As New Regex("^[Ss]harp")

    ForEach(filInfo as FileInfo in dirInfo.GetFiles())

    If (reg.Match(filInfo.Name).Success) Then
    Return True
    End If

    Next

    'If no files found, this is false
    Return False

    End Function

    This will find a single file with sharp or Sharp at the beginning. Note
    that you can also set to a non-case sensitive comparison. I use the [Ss]
    to illustrate what the regex is doing more than it being a requirement
    for case insensitive searching.

    Peace and Grace,


    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    Twitter: @gbworld
    Blog: http://gregorybeamer.spaces.live.com

    *******************************************
    | Think outside the box! |
    *******************************************
    Gregory A. Beamer, Oct 9, 2009
    #1
    1. Advertising

  2. Gregory A. Beamer

    pvong Guest

    Gregory - Thanks for the reply. I know I'm asking some very simple
    questions, but I'm learning a little at a time with help from people like
    yourself. I would like to try the Private Function route. If it sees one,
    then it does not need to look any further. This is what I have for my
    Private Function, but I'm getting an error in the "For Each" line. I'm
    getting "filInfo not declared" error.

    Private Function HasSharpFile(ByVal directoryPath As String) As Boolean

    Dim dirInfo As New IO.DirectoryInfo(directoryPath)

    'Assume files in question START with sharp

    'SUCCESS sharpFOOBAR.pdf

    'FAIL SomethingSharp.pdf

    Dim reg As New Regex("^[Ss]harp")

    For Each(filInfo as FileInfo in dirInfo.GetFiles())

    If (reg.Match(filInfo.Name).Success) Then

    Return True

    End If

    Next

    'If no files found, this is false

    Return False

    End Function



    "Gregory A. Beamer" <> wrote in message
    news:Xns9C9F67838C84Agbworld@207.46.248.16...
    > "pvong" <vonger@*dot*com> wrote in
    > news::
    >
    >> Doing this in VB.Net
    >>
    >> I asked a similar question last month, but I still can not get it to
    >> work right. All I want to do is look for files that start with the
    >> word "sharp" and do something if it sees a file starting with this
    >> name. All the files are .pdf files. The folder is located on the
    >> local server running the IIS and I've checked the directory over and
    >> over again and it's right. I just keep getting There are No Files.
    >> I've attached an image of the listed files inside for testing.
    >>

    >
    > I have answered this recently (albeit in C# as the OP did not specify
    > language). If that was you that posted, you have more/different
    > information here than before.
    >
    > This is off the top of my head, but the only thing that might need
    > tweaking is the Regex statement
    >
    > Dim dirInfo as New DirectoryInfo(DirectoryPath)
    >
    > 'Assume files in question START with sharp
    > 'SUCCESS sharpFOOBAR.pdf
    > 'FAIL SomethingSharp.pdf
    > Dim reg As New Regex("^[Ss]harp")
    >
    > ForEach(filInfo as FileInfo in dirInfo.GetFiles())
    >
    > If (reg.Match(filInfo.Name).Success) Then
    > Response.Write("Has Sharp File")
    > Else
    > Response.Write("No Sharp File")
    > End If
    >
    > Next
    >
    > If you want a single success or fail, encapsulate this in a method and
    > return true when you have a single success, like so:
    >
    > Private Function HasSharpFile(ByVal directoryPath As String) As Boolean
    >
    > Dim dirInfo as New DirectoryInfo(DirectoryPath)
    >
    > 'Assume files in question START with sharp
    > 'SUCCESS sharpFOOBAR.pdf
    > 'FAIL SomethingSharp.pdf
    > Dim reg As New Regex("^[Ss]harp")
    >
    > ForEach(filInfo as FileInfo in dirInfo.GetFiles())
    >
    > If (reg.Match(filInfo.Name).Success) Then
    > Return True
    > End If
    >
    > Next
    >
    > 'If no files found, this is false
    > Return False
    >
    > End Function
    >
    > This will find a single file with sharp or Sharp at the beginning. Note
    > that you can also set to a non-case sensitive comparison. I use the [Ss]
    > to illustrate what the regex is doing more than it being a requirement
    > for case insensitive searching.
    >
    > Peace and Grace,
    >
    >
    > --
    > Gregory A. Beamer
    > MVP; MCP: +I, SE, SD, DBA
    >
    > Twitter: @gbworld
    > Blog: http://gregorybeamer.spaces.live.com
    >
    > *******************************************
    > | Think outside the box! |
    > *******************************************
    pvong, Oct 9, 2009
    #2
    1. Advertising

  3. On Oct 9, 10:20 pm, "pvong" <vonger@*dot*com> wrote:
    > Gregory - Thanks for the reply.  I know I'm asking some very simple
    > questions, but I'm learning a little at a time with help from people like
    > yourself.  I would like to try the Private Function route.  If it sees one,
    > then it does not need to look any further.  This is what I have for my
    > Private Function, but I'm getting an error in the "For Each" line.  I'm
    > getting "filInfo not declared" error.
    >


    Change it to

    For Each filInfo As System.IO.FileInfo In dirInfo.GetFiles()

    and try again.

    Hope this helps
    Alexey Smirnov, Oct 9, 2009
    #3
  4. Gregory A. Beamer

    pvong Guest

    EVERTHING WORKS!!!! Thanks guys!!! Learned something new today. Baby
    steps.


    "Alexey Smirnov" <> wrote in message
    news:...
    On Oct 9, 10:20 pm, "pvong" <vonger@*dot*com> wrote:
    > Gregory - Thanks for the reply. I know I'm asking some very simple
    > questions, but I'm learning a little at a time with help from people like
    > yourself. I would like to try the Private Function route. If it sees one,
    > then it does not need to look any further. This is what I have for my
    > Private Function, but I'm getting an error in the "For Each" line. I'm
    > getting "filInfo not declared" error.
    >


    Change it to

    For Each filInfo As System.IO.FileInfo In dirInfo.GetFiles()

    and try again.

    Hope this helps
    pvong, Oct 9, 2009
    #4
  5. On Oct 9, 10:49 pm, "pvong" <vonger@*dot*com> wrote:
    > EVERTHING WORKS!!!!  Thanks guys!!!  Learned something new today.  Baby
    > steps.
    >


    Great! it means that ( ) were wrong there
    Alexey Smirnov, Oct 9, 2009
    #5
  6. Alexey Smirnov <> wrote in news:95827338-9465-
    :

    > Change it to
    >
    > For Each filInfo As System.IO.FileInfo In dirInfo.GetFiles()
    >
    > and try again.
    >
    > Hope this helps


    Or add the following at the top:


    Imports System.IO.FileInfo

    Peace and Grace,


    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    Twitter: @gbworld
    Blog: http://gregorybeamer.spaces.live.com

    *******************************************
    | Think outside the box! |
    *******************************************
    Gregory A. Beamer, Oct 9, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    645
  2. Replies:
    2
    Views:
    517
    bruce barker
    Mar 25, 2008
  3. pvong
    Replies:
    2
    Views:
    357
    Gregory A. Beamer
    Oct 1, 2009
  4. kazaam
    Replies:
    3
    Views:
    136
    kazaam
    Aug 26, 2007
  5. SAN CAZIANO
    Replies:
    8
    Views:
    174
    Dr John Stockton
    Oct 15, 2004
Loading...

Share This Page