List files in a directory, modify name.

  • Thread starter Thread starter Øyvind Isaksen
  • Start date Start date
Ø

Øyvind Isaksen

Hi,

I have made a code that list all files in a directory on the server.
It works fine, but I need to modify the filename (remove the 7 first char in
the name) that is displayed in the datagrid.

Example:
The file "123456_myfile.jpg" shoult be displayed like this: "myfile.jpg"

---------------------------
This is my code so far:
---------------------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dirInfo As New
DirectoryInfo(Server.MapPath("/uploads/isaksenmedia/"))
Me.dtgFilelist.DataSource = dirInfo.GetFiles("*.*")
Me.dtgFilelist.DataBind()
End Sub


<asp:DataGrid id="dtgFilelist" runat="server" autogeneratecolumns="False">
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name"
DataTextField="Name" HeaderText="Filename" />
<asp:BoundColumn DataField="LastWriteTime" HeaderText="Modyfied"
DataFormatString="{0:d}" />
<asp:BoundColumn DataField="Length" HeaderText="Size"
DataFormatString="{0:#,### bytes}" />
</Columns>
</asp:DataGrid>



Thanks!!!!!!
 
If i were you, I would dump the GetFiles into a collection first and
iterate through that, changing the names while it is still in a
collection, then bind this collection to the datagrid, it would be
easier then binding it to the datagrid and then changing it.
 
Ok, I sounds like a smart way to do it!
Could you please help me get started making this "collection", I'm not shure
how to do this... Guess this is an array...?

Thanks alot!!!
 
ok,

GetFiles returns a FileInfo[] collection

if you go to msdn.microsoft.com and look up FileInfo class, you'll see
all the different properties you can get about a file, it depends on
what you want out of FileInfo class. you can setup a foreach and
iterate through the FileInfo[] collection changing properties if you
want:

i havent tested this code, but you should point you in the right
direction

FileInfo[] files = dirInfo.GetFiles();

foreach (FileInfo f in files) {
f.Name = f.Name.substring(8, f.Name.Length - 8);
}

DataGrid myDataGrid.DataSource = files;
myDataGrid.DataBind();

that substring might throw an error because of the length of the
substring, but you get the idea.

hope this helps
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top