2

Trying to use a loop to check if images exists however it is always returning false. I am sure I am doing something simple and stupid but here is the code:

dim fs, sql_except
set fs=Server.CreateObject("Scripting.FileSystemObject")
if Not rs.eof then
    arrRS = rs.GetRows(30,0)
    set rs = nothing
    If IsArray(arrRS) Then
        For i = LBound(arrRS, 2) to UBound(arrRS, 2)
            sku = arrRS(0, i)
            if (fs.FileExists("../i/"&sku&".gif")=false) Then
                response.write sku&"does not exist<br>"
            end if
        next
    end if
    erase arrRS
end if
set fs=nothing
4

1 回答 1

6

You appear to be operating under the impression that the current folder context the your call to FileExists will assume is the physical folder containing the ASP script being executed. This is not so, it most likely will be "C:\windows\system32\inetsrv". You are also using URL path element separator / where FileExists is expecting windows physical path folder separator \.

You need to use Server.MapPath to resolve the path. This may work:

if Not fs.FileExists(Server.MapPath("../i/"&sku&".gif")) then

However you may run in to trouble with the parent path "..", this may not be allowed for security reasons. This might be a better approach:

Dim path : path = Server.MapPath("/parentFolder/i") & "\"
For i = LBound(arrRS, 2) to UBound(arrRS, 2)        
    sku = arrRS(0, i)        
    if Not fs.FileExists(path & sku & ".gif") Then        
        response.write Server.HTMLEncode(sku) & " does not exist<br>"        
    end if        
next    

Where "parentFolder" is the absolute path from the site root.

于 2012-04-19T21:43:36.270 回答