Count Files In a directory

lorena

Well-known member
Joined
Oct 23, 2003
Messages
134
Location
Phoenix, Arizona
I have several pages that are supposed to list all the files that are .pdf files in a certain directory.
For some reason, the page displays one less file than is actually in the directory.
What am I doing wrong?
Here is my code:
Code:
Sub BindGrid()
				Dim dirInfo As New System.IO.DirectoryInfo(Server.MapPath("/p1/Docs/JobOrders/6500 - 6599"))
				Dim arrFileInfo As Array
				Dim filesInfo As System.IO.FileInfo

				Dim filesTable As New DataTable()
				Dim drFiles As DataRow
				Dim dvFiles As DataView
				Dim ofn, ext As String
				Dim charCt, fileCt As Integer

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

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

				fileCt = 0
				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)
					fileCt += 1
				Next filesInfo

				dvFiles = filesTable.DefaultView
				If SortAscending then
					dvFiles.Sort = SortExpression & " ASC" 
				Else
					dvFiles.Sort = SortExpression & " DESC" 
				End If

				dgFiles.DataSource = dvFiles
				
				dgFiles.DataBind()
				If fileCt < 51 Then
					GridPagerVisibility(False)
				Else
					GridPagerVisibility(True)
				End If
				lblyourmom.text = fileCt
			End Sub
 
Using LastIndexOf to get the charCt should solve that problem. Or alternatively you could use the System.IO.Path.GetFileNameWithoutExtension() method.
 
Back
Top