Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

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

Posted
Using LastIndexOf to get the charCt should solve that problem. Or alternatively you could use the System.IO.Path.GetFileNameWithoutExtension() method.
Anybody looking for a graduate programmer (Midlands, England)?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...