Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does GetFileName fail with first database object? 1

Status
Not open for further replies.

jdnewton

Technical User
Jan 17, 2003
17
0
0
GB
Here is a curious one.

I have an ASP page which uses a search string to pull records from a database.

For a variety of reasons I then use a GetFileName function to cull the filename and extension, this is then used to populate a Javascript window.open routine to provide a link to the appropriate file.

It works fine - except for the first record retrieved on every search occasion which returns a null string instead of the filename.

The GetFileName function is this:

Code:
<%
Function FileNameGet(strPath)
	
Dim fngFSO
set fngFSO = Server.CreateObject("Scripting.FileSystemObject")
nameoffile = fngFSO.getfilename(strPath)
Set fngFSO = Nothing
End Function
%>

and the body of the ASP page opening the database and pulling the records is this:

(sorry, lots of added bits, have seperated lines to make clearer)

Code:
rs.Open
		
If rs.RecordCount >= 1 then

Response.Write "<table cellpadding='5' border='0'><tr>"

Response.Write "<td style='color:black;text-decoration:underline'><strong><a class='sort' href='resort.asp?S=T&term=" & searchterm & "' title='Sort by title'>Title</a></strong></td>"

Response.Write "<td style='color:black;text-decoration:underline'><strong><a class='sort' href='resort.asp?S=C&term=" & searchterm & "' title='Sort by category'>Category</a></strong></td>"

Response.Write "<td style='color:black;text-decoration:underline'><strong><a class='sort' href='resort.asp?S=S&term=" & searchterm & "' title='Sort by subject'>Subject</a></strong></td>"

Response.Write "<td style='color:black;text-decoration:underline'><strong><a class='sort' href='resort.asp?S=A&term=" & searchterm & "' title='Sort by author'>Author</a></strong></td><td style='color:black;text-decoration:underline'><strong><a class='sort' href='resort.asp?S=J&term=" & searchterm & "' title='Sort by Journal'>Journal</a></strong></td>"

Response.Write "<td style='color:black;text-decoration:underline'><strong>File</strong></td>"
Response.Write "<td style='color:black;text-decoration:underline'><strong>Delete</strong></td>"
Response.Write "<td style='color:black;text-decoration:underline'><strong>Edit</strong></td></tr>"

Do Until rs.EOF
		
databasename=(rs("location"))    

FileNameGet databasename

nametoopen="Files/" & nameoffile
	 	
Response.Write "<tr><td><a target='_blank' title='" & Replace(rs("comment"),"''","&#39;") &"' href='" & nametoopen & "''" &">" & Replace(rs("title"),"''","&#39;") & "</a></td>"

Response.Write "<td style='color:black'>" & Replace(rs("category"),"''","&#39;") & "</td><td style='color:black'>" & Replace(rs("subject"),"''","&#39;") & "</td>"

Response.Write "<td title='" & Replace(rs("forename"),"''","&#39;") & "&nbsp;" & Replace(rs("surname"),"''","&#39;") & "' style='color:black'>" & Replace(rs("surname"),"''","&#39;") & "</td><td title='" & Replace(rs("journal"),"''","&#39;") & ", " & Replace(rs("formalref"),"''","&#39;") & "' style='color:black'>" & Replace(rs("publication"),"''","&#39;") & "</td>"

Response.Write "<td style='text-transform:uppercase' style='color:black'>" & fileext & "</td>"

Response.Write "<td><a style='color:red' href='delete.asp?N=" & rs("ID") & "'>delete</a></td>"

Response.Write "<td><a style='color:blue' href='edit.asp?N=" & rs("ID") & "'>edit</a></td>"

Response.Write "<td><a href='#top'><img border='0' src='triangle.jpg' height='10' width='10' title='Back to top' /></a></td></tr>"

' added bits to display what the variables are if GetFileName is wrong
		
If nametoopen="Files/" then 

Response.Write "<tr><td>database location=" & (databasename) & "</tr></td>"

Response.Write "<tr><td>Constructed target=" & nametoopen & "</td></tr>"

Response.Write "<tr><td>returned name from routine=" & nameoffile & "</td></tr>"

end if

rs.MoveNext

Loop

Response.Write "<tr><td><form method='post' action='print.asp' target='_blank'><input type='hidden' name='List' value='" & searchterm & "'><input type='submit' value='Print list' /></form></td></tr></table>"

Else	    	 

Session("badsearch")="true"

Response.redirect "search.asp"		

End If

rs.Close

set rs = Nothing

Conn.close

set Conn = Nothing
%>

Can anyone fathom why the first returned entry in the table always returns a null string and hence an invalid link?

Cheers all.
 
[1]
>nameoffile = fngFSO.getfilename(strPath)
[tt]FileNameGet = fngFSO.getfilename(strPath)[/tt]
[2]
>FileNameGet databasename
>nametoopen="Files/" & nameoffile
[tt]nametoopen="Files/" & FileNameGet(databasename)[/tt]
 
Genius!

Worked a treat.

Care to explain why?

Cheers muchly.
 
[a] The vbs function can return value via the assigning the function's name. (It can return value by out-parameter, global variable etc... as well, but that is the basic receipt of the use of function.)
Call the function can capture the return value as shown in [2].
[c] The plausible reason of what described in the title is due to an accident. An accident, or a side effect, of not properly delimiting the scope of variable nameoffile. (You should, after the modification, dim nameoffile inside the function to limit its scope.) As such, it would be global and can be accessed everywhere. It is a way to pass "return" mentioned in [a]. But it is more accurately described as a "side-effect". It would be terribly confusing and less than good practice unless no other better way available. (The concrete reason the first time failure is probably nametofile is not yet well-defined. But I have not looked into it.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top