Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 4th, 2008, 03:43 PM
Authorized User
 
Join Date: Sep 2007
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default GetFiles() Help

Greetings,
I need help with a GetFiles() to return multiple file extensions. I think I am close with the included bit of code. However, it is not filtering like I desire. It returns all files if any file has an extension of my If statement. I want it to return only those files with the desired extension.

I think I might need a table adapter but I'm not using a database.

Code:
Dim dirInfo2 As NewDirectoryInfo(Server.MapPath("///.\UploadFolder\"))
        Dim fileArray As FileInfo() = dirInfo2.GetFiles()
        Dim specificFile As FileInfo
        For Each specificFile In fileArray
            If specificFile.Extension = ".htm" Then
                'add to data source
                dgArticleList.DataSource = fileArray 'PROBLEM HERE
            End If
        Next specificFile
        dgArticleList.DataBind()
  #2 (permalink)  
Old April 5th, 2008, 01:51 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Adam,

There are a couple of ways to do this. First of all, you can use the static GetFiles method of the Directory class and pass it a search pattern. This only gives you the name of the file as a string though:
Code:
Dim basePath As String = Server.MapPath("~/")
Dim searchPattern As String = "*.aspx"

dgArticleList.DataSource = Directory.GetFiles(basePath, searchPattern)
dgArticleList.DataBind()
Alternatively, you can do the same with your code and pass the wild card pattern to GetFiles of your DirectoryInfo instance:
Code:
' Assumes you have basePath and searchPattern from example 1
Dim dirInfo As New DirectoryInfo(basePath)
dgArticleList.DataSource = dirInfo.GetFiles(searchPattern)
dgArticleList.DataBind()
Finally, and much cooler you can use LINQ (if you are using .NET 3.5). With LINQ you can express queries in a much more natural way and get files based on much more criteria than their extension alone. Just for fun, the following code gets all ASPX files that have been created in the past 24 hours and that have been accessed in the last 10 minutes:
Code:
Dim list = From f In New DirectoryInfo(basePath).GetFiles _
    Where f.CreationTime > DateTime.Now.AddHours(-24) _
    And f.LastAccessTime > DateTime.Now.AddMinutes(-10) _
    And f.Extension.EndsWith(".aspx", StringComparison.InvariantCultureIgnoreCase) _
    Select f

  dgArticleList.DataSource = list
  dgArticleList.DataBind()
  Hope this helps,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
  #3 (permalink)  
Old April 5th, 2008, 01:06 PM
Authorized User
 
Join Date: Sep 2007
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

As usual, your code is awesome! I am truly impressed with the LINQ example! However, I'm trying to pass multiple patterns to GetFiles().

searchPattern returns all file types which is stored in an array of FileInfo(). I then use two separate arrays to loop through everything then store the files with the desired extensions which becomes my DataSource.

I've modified my code to match your example, but I am at a complete loss inside the nested IF Statement. I cannot use .Add in there.

Code:
Dim basePath As String = Server.MapPath("~/UploadFolder/")
Dim searchPattern As String = "*.*"

Dim dirInfo As New DirectoryInfo(basePath)
Dim fileArray As FileInfo() = dirInfo.GetFiles(searchPattern)

Dim specificFile As FileInfo
Dim secondArray As FileInfo

     For Each specificFile In fileArray
          If specificFile.Extension = ".htm" Or specificFile.Extension = ".ppt" Then
          'collect files with desired extensions
          secondArray.Add(specificFile)
          End If
     Next specificFile
dgArticleList.DataSource = secondArray
dgArticleList.DataBind()
  #4 (permalink)  
Old April 5th, 2008, 01:58 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You're not declaring secondArray as an array (using ()) but even if you would, it wouldn't have an Add method. Instead of an array you can use a generics List (Of FileInfo) instance like this:
Code:
Imports System.Collections.Generic

Dim specificFile As FileInfo
Dim filteredList As New List(Of FileInfo)

For Each specificFile In fileArray
  If specificFile.Extension = ".htm" Or specificFile.Extension = ".ppt" Then
    'collect files with desired extensions
    filteredList.Add(specificFile)
  End If
Next specificFile
dgArticleList.DataSource = filteredList
dgArticleList.DataBind()
If you don't want GetFiles to do any filtering, there's no need to pass *.* as the search pattern. The parameterless version of GetFiles gets all files by default.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
  #5 (permalink)  
Old April 5th, 2008, 02:28 PM
Authorized User
 
Join Date: Sep 2007
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

You are a genius!! Your List(Of FileInfo) solution was exactly what I needed! It correctly filled my datagrid with the multiple-filters.

Thank you so much for your help!

Problem Solved.

  #6 (permalink)  
Old April 5th, 2008, 02:45 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You're welcome.....

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
  #7 (permalink)  
Old April 16th, 2008, 10:43 PM
Authorized User
 
Join Date: Sep 2007
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Again,

I am using the code posted above, it works perfectly (thank you Imar!). I am now attempting to change the file name just before it is collected in the "filteredList" array. I'm thinking I might need to convert from stream to string for this to work??...

The included code shows my feeble attempt change the file name. I would prefer to prefix the file name based on the first character of the file name.

Ideally my DataGrid row would display a custom custom file name like this:
Original File Name= 1myFile.txt
Modified File Name= CSTLRKmyFile.txt

Code:
For Each specificFile In fileArray
     If specificFile.Extension = ".txt" Or specificFile.Extension = ".doc" Then
          If specificFile.Name.StartsWith("1") Then
               specificFile.Replace(specificFile.FullName, "CSTLRK")
               'collect files with desired extensions and new name
               filteredList.Add(specificFile)
          End If
     End If
Next specificFile
dgArticleList.DataSource = filteredList
dgArticleList.DataBind()
  #8 (permalink)  
Old April 17th, 2008, 08:48 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

First of all, you're not really replacing the value. Or actually, it looks like you are replacing, but you don't assign the value back:

specificFile.Replace(specificFile.FullName, "CSTLRK")

The return value of the Replace operation is completely ignored.

Also, Replace doesn't replace the file name, it replaces the *underlying* file on disk: http://msdn2.microsoft.com/en-us/lib...o.replace.aspx

Finally, most of the properties of the FileInfo are read-only so you can't really reassign a new file name.

Instead, create an intermediate object, fill that with the name of the file and add that object to your list that you bind to your grid.

What that intermediate object is depends; ot could be as simple as a string of all you need is the file name. You could also create a simple custom class for this purpose.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
  #9 (permalink)  
Old April 17th, 2008, 08:54 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

BTW: as an alternative, you can change the file name in one of the data events of your DataGrid, like ItemDataBound. That way, you can still bind your FileInfo list, but only change the name when it;s displayed.

Finally, you could create a little helper method that does this. Something like this in your Grid:

... <%# ConvertFileName(Eval("FullName")) %> ...

With this code in the Code Behind:

Protected Function ConvertFileName(ByVal fileName As Object) As String
  Dim localFileName As String = CType(fileName, String)
  If localFileName.EndsWith(.....)
    Return "WhatEverYouWant"
  End If
  Return localFileName
End Function

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
  #10 (permalink)  
Old April 17th, 2008, 01:07 PM
Authorized User
 
Join Date: Sep 2007
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

Just as I was about to give up and beg you for more help, I re-read your instructions and got it to work successfully!!

Problem solved.

Again, I am grateful and inspired by your expert code examples, and clear advice.

I am curious about your suggestion to create a custom class, do you have an example I could follow?

Here is the final code, I was using the HyperLinkColumn unsuccessfully at first. Then, I tried a TemplateColumn with your helper method and code-behind:
Code:
ASPX PAGE
<Columns>
     <asp:TemplateColumn HeaderText="File Name">
          <ItemTemplate>
               <asp:HyperLink ID="hyprLnk1" runat="server" Target="_self" 
               Text='<%# ConvertFileName(Eval("Name")) %>'
               NavigateUrl="javascript:LoadIFrame('../../UploadFolder/{0}');">
               </asp:HyperLink>
          </ItemTemplate>
     </asp:TemplateColumn>
</Columns>

VB CODE-BEHIND
 Protected Function ConvertFileName(ByVal fileName As Object) As String
     Dim localFileName As String = CType(fileName, String)
          If localFileName.StartsWith("1") Then
               Return "CSTLRK"
          End If
     Return localFileName
End Function
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vista: Getfiles throws security error sigilaea Visual Basic 2008 Professionals 2 February 9th, 2009 06:00 PM
Directory.GetFiles got other not related files cmyeung_99 C# 2005 0 June 29th, 2006 09:16 PM
DirectoryInfo.GetFiles(pattern): search pattern fo arif_1947 VS.NET 2002/2003 1 October 19th, 2004 11:59 PM



All times are GMT -4. The time now is 01:57 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.