Solved

Exclude Extensions from file array

Posted on 2009-04-02
12
290 Views
Last Modified: 2013-11-07
I have an .aspx page that links to all the .pdf files in a directory and does not display the extension.
Now they would like to have all the files linked and still not display the file extension.
I'm thinking there would be an easier way to do this using "GetFileNameWithoutExtension" but I'm not exactly sure how to do it.
Thanks for any help
Dim dirInfo as New DirectoryInfo(Server.MapPath("/p1/q/csr rpt"))		

Dim arrFileInfo  As Array

Dim filesInfo   As FileInfo

Dim filesTable   As New DataTable

Dim drFiles    As DataRow

Dim dvFiles   As DataView

Dim ofn,ext As String

Dim charCt as Integer

filesTable.Columns.Add("Name", Type.GetType("System.String"))

filesTable.Columns.Add("LastWriteTime", Type.GetType("System.DateTime"))

' Get File Info

arrFileInfo = dirInfo.GetFiles("*.pdf")

For Each filesInfo In arrFileInfo

  drFiles = filesTable.NewRow()					

  ofn = filesInfo.Name

  ext = Right(ofn, 4)

  charCt = Instr(ofn, ".") -1

  ofn = Mid(ofn, 1, charCt)

  drFiles("Name") = ofn

  drFiles("LastWriteTime") = filesInfo.LastWriteTime					

  filesTable.Rows.Add(drFiles)

Next filesInfo

				

dvFiles = filesTable.DefaultView

dvFiles.Sort = "LastWriteTime DESC"
 

dg1.DataSource = dvFiles

dg1.DataBind()

Open in new window

0
Comment
Question by:lakhi
  • 7
  • 5
12 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 400 total points
ID: 24052133
Change:

    drFiles("Name") = ofn

To:

    drFiles("Name") = System.IO.Path.GetFileNameWithoutExtension(filesInfo.Name)

The whole loop would become:

    For Each filesInfo In arrFileInfo
        drFiles = filesTable.NewRow()                                
        drFiles("Name") = System.IO.Path.GetFileNameWithoutExtension(filesInfo.Name)
        drFiles("LastWriteTime") = filesInfo.LastWriteTime                                    
        filesTable.Rows.Add(drFiles)
    Next filesInfo
0
 

Author Comment

by:lakhi
ID: 24052527
I tried that and when I preview the page, I get this error:
BC30456: 'GetFileNameWithoutExtension' is not a member of 'System.IO.FileInfo'
0
 

Author Comment

by:lakhi
ID: 24052555
Wait a sec - that might have been something I did. BRB
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24052564
Look closely at the line..  =)

    drFiles("Name") = System.IO.Path.GetFileNameWithoutExtension(filesInfo.Name)
0
 

Author Comment

by:lakhi
ID: 24052733
I did get it to work.
Now, the issue is to add the extension back in when I set up the "DataNavigateUrlFormatString" in the datagrid where the links are listed.
When they were all pdf files, I could just use {0}.pdf but now I'm not sure. How can I do that?
The issue is a little trickier for me because the links are in a virtual directory on the intranet server. So I do a server.mappath to the virtual directory but then the "DataNavigateUrlFormatString" will be:
"http://myintranet.com/virtualDir/qty/csr/2008/" and I used to just add the "/{0}.pdf " but of course that doesn't work now.
help?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24053132
Add the actual FileInfo instance to your Table:

    filesTable.Columns.Add("FileInfo", Type.GetType("System.IO.FileInfo"))

Then in the loop:

    drFiles("FileInfo") = filesInfo

Now you can get it back out with a cast:

    Dim fi As FileInfo = CType(YourDataRowHere("FileInfo"), FileInfo)
    Dim FullName As String = fi.FullName
    ' ...do something with "FullName"...


0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:lakhi
ID: 24053731
Sorry - I'm still a bit confused. I understand how to get the filename with and without the extension but when I set up the datagrid link, I get lost.
If I have:
    Dim fi As FileInfo = CType(YourDataRowHere("FileInfo"), FileInfo)
    Dim FullName As String = fi.FullName

Then how do I reference it in the datagrid hyperlink column. Here is what I mean:
<asp:HyperLinkColumn DataNavigateUrlField="Name" DataNavigateUrlFormatString="http://myintranet.com/virtualDir/qty/csr/2008/" **** DataTextField="Name" Target="_blank">
**** Can I just put "FullName" at this point or how do I do that?
Sorry to be a pest...
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24053888
(you're not being a pest)

*** I'm NOT a web programmer though ***         =(

Couldn't you just add an extra column to your datatable that has the filename with the extension on it?

    filesTable.Columns.Add("FileName", Type.GetType("System.String"))
    ...
    drFiles("FileName") = filesInfo.Name ' This WILL have the extension on it

Then you could use your previous code that worked for just PDFs, but with "FileName" instead of "Name".

Sorry if that makes no sense.  Like I said, I'm not a web programmer.  =\

0
 

Author Comment

by:lakhi
ID: 24053958
I did post my question in the "Programming for ASP.Net"
If you aren't a web programmer, who else do I ask?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24054053
You also posted in the GENERIC .Net Topic areas with a code snippet dealing almost exclusively with non web specific code.  I looked at your question and it seemed like I could answer just the part you needed w/o knowing any web programming...and I did.  I showed how to use System.IO.GetFileNameWithoutExtension().

I'll hit the "Request Attention" button and they can get more attention to your question.

Apologies...
0
 

Author Comment

by:lakhi
ID: 24054070
Oh! That makes sense.
Obviously, I'm new at this.
I think I will post it as a separate question because you were so helpful with my main question.
Thank you very much.
0
 

Author Closing Comment

by:lakhi
ID: 31565889
Very helpful. I really needed a web programmer but I didn't post my question in the right place and the Expert who helped me did a great job! Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Allow space in this pattern 2 47
Runtime Exceptions when trying to submit data 28 36
Replace &lt; with < 14 55
Where to download and how to install sqldmo.dll 5 33
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now