OffSight
Website and Graphic Design
01785 43 1000
Home : Tutorials

Loop Through The Files In A Folder On Your Asp Net Server


Added by David Coleman on 18 March 2011

You may sometimes want to loop through all the files in a particular folder on your server. You might want to show files available for download or go through and delete images older than a year, for example. Just use the following code:

At the top of your page make sure you include the system.io namespace:

Imports System.IO


The following code should be placed wherever you want to loop through your files, e.g. on pageLoad or in a function.
'Tell your code where to find the folder you want to look inside. 
'The ~ character simply means "the root of your website". 
'Below, for example, "~/your-folder/" tells your code to look 
'inside www.yourwebsite.co.uk/your-folder/.

Dim myFolder As New DirectoryInfo(Server.MapPath("~/your-folder/"))

'Loop through each file in the folder

For Each myFile As FileInfo In myFolder.GetFiles

	'You can now access information about each file. Here are a few examples.

	'Get the file's name

	dim fileName as string = myFile.Name

	'Get the file's extension, e.g. "jpg"

	Dim fileExtension as string = myFile.Extension

	'Get the date the file was created on

	Dim creationTime as datetime = myFile.CreationTime

	'Find out when the file was last changed

	dim lastWriteTime as datetime = myFile.LastWriteTime
'Using one of the above examples, we can delete all files that are older than a year... If myFile.CreationTime < DateTime.Now.AddYears(-1) Then myFile.Delete() End If 'Similarly, This extremely handy code deletes all files with 'banana' in the filename If myFile.Name.ToLower.Contains("banana") Then myFile.Delete() End If Next 'This takes us back to the start of the loop

Add a comment

Your name:

Your comment:



There are no comments yet

Share This Page


What Do Our Customers Think?
"I was fed up with looking for website providers – I wanted a new site that didn’t look the same as all the others – and I wanted a site that I could update but still looked like a professional web guy was running it … I find it hard to say how much I love my new site – I’ll tell anyone who asks how good it is and how helpful you guys have been."

Marcelo Viegas, Manager, The Best Western Charnwood Hotel
Latest OffSight News
new website cookie law comes into effect on 26th may New website 'cookie law' comes into effect on 26th May What is the new law? The new law states that (to paraphrase), “all websites in the EU must seek consent to store cookies that are not essential ... offsight moves to new larger stafford office OffSight moves to new, larger Stafford office We're excited to tell you that OffSight has moved to Stafford into the Paul Reynolds Centre, above the Ashiana curry house opposite Halfords. Our new office is much larger than ... offsight delivers client a huge growth in online sales OffSight Delivers a Huge Growth in Online Sales to iWood Timber iWood, a company that Offsight works very closely with, has been featured on the front page of the business section in the Express and Star Newspaper! ... offsight invited to present at staffordshire university Matthew Nash and myself, David Coleman, were recently asked by Staffordshire University to give a presentation on Search Engine Optimisation to their business scheme members. The aim of the presentation was to introduce the business ...
Signup for our newsletter
Share this page