Look to see if a certain file exist in from button click

  • Thread starter Thread starter pvong
  • Start date Start date
P

pvong

I'm a newbie. Trying to do this in VB.NET

On my server, I have a directory called c:\db\files\ and I have a whole
bunch of pdf files in here. I want to do an onclick where the server checks
to see if any of the files starts with the word "sharp". So I will have
lots of files, but some files will look like this:

sharp123.pdf
sharp321.pdf
sharp987.pdf

I just want to look for the beginning of the word "sharp". If it finds a
file starting with that, then I want it to do whatever. The directory is on
the same server as my IIS and runing the site.

Thanks!
 
Visual Studio 2005 or 2008 has a code snippet for the first part of your
problem. Looping through the results after that is easy:

Dim files As ReadOnlyCollection(Of String)
files = My.Computer.FileSystem.FindInFiles("c:\db\files", "sharp", True,
FileIO.SearchOption.SearchTopLevelOnly, "*.pdf")

If files.Count > 1 Then
For Each file In files
'Work with file that contains "sharp"
Next
End If

Scott M.
 
I'm a newbie. Trying to do this in VB.NET

On my server, I have a directory called c:\db\files\ and I have a
whole bunch of pdf files in here. I want to do an onclick where the
server checks to see if any of the files starts with the word "sharp".
So I will have lots of files, but some files will look like this:

sharp123.pdf
sharp321.pdf
sharp987.pdf

I just want to look for the beginning of the word "sharp". If it
finds a file starting with that, then I want it to do whatever. The
directory is on the same server as my IIS and runing the site.

One hurdle to overcome here is the files are not in the web directories.
you can do this by creating a virtual directory to search the files
from. Otherwise, you will have to reduce some security setting to reach
the "outside" folder.

As for figuring out the files, you can loop through:

DirectoryInfo dir = new DirectoryInfo(filePath);
Regex regex = new Regex("^sharp");

foreach(FileInfo file in dir.GetFiles())
{
if(Regex.Match(file.Name).Success)
{
//file begins with sharp
}
}

You may have to vary the code a bit to get exactly what you want.
Outputting a PDF is a matter of streaming the bytes with a MIME type of
application/pdf.

Peace and Grace,

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

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

*******************************************
| Think outside the box! |
*******************************************
 
Back
Top