2012-04-19 53 views
2

尝试使用循环来检查图像是否存在,但它总是返回false。我相信我做一些简单和愚蠢的,但这里是代码:asp fileExists always returns false

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 

回答

5

你似乎给人的感觉是当前文件夹背景下,你对FileExists的通话将假设是包含ASP的物理文件夹下操作脚本正在执行。事实并非如此,它很可能是“C:\ windows \ system32 \ inetsrv”。您还正在使用URL路径元素分隔符/,其中FileExists需要Windows物理路径文件夹分隔符\

您需要使用Server.MapPath来解析路径。这可能工作:

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

然而,你可能在与父路径故障运行“..”,这可能不允许出于安全原因。这可能是一个更好的方法:

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  

其中“parentFolder”是从站点根目录的绝对路径。