Solved

Server.MapPath on a different server

Posted on 2004-10-28
600 Views
Last Modified: 2008-01-09
Hi Experts,

I have written a page that goes through a recordset of Photo IDs in the database and checks if the file of the same name exists in a physical folder. This works. However I dont know how to check a physical folder on a different server.

For example:
photoPath = "\\servername\images$"
strPhysicalPath = Server.MapPath(photoPath)

I thought the above would work but I get the error:

Server.MapPath() error 'ASP 0174 : 80004005'
Invalid Path Character(s)

How do I do this?
0
Question by:colin_palmer
    8 Comments
     
    LVL 11

    Accepted Solution

    by:
    Permission tend to be a problem here as the user under which IIS is running needs to be entitled to log on to the other machine.

    The easiest way to do this is to create a virtual directory in IIS that points to the relevent directory on the other machine. This will allow you enter the relevent user account and password then within your mappath you can specify the virtual directory eg

    If the remote server/directory  is \\server2\images and you set up a virtual directory called images2 that points to this the your code will look like this

    photoPath = "\images2"
    strPhysicalPath = Server.MapPath(photoPath)

    HTH
    Steve
    0
     
    LVL 1

    Author Comment

    by:colin_palmer
    Hi Steve

    I did what you said and created a new Virtual Directory and its permissions, say its called 'images2'  and its pointing to '\\server2\images':

    If I goto my browser and type the URL e.g. http://servername/images2/filename.jpg

    I can see the file so that part of it is working.

    So I tried

    photoPath = "\images2"
    strPhysicalPath = Server.MapPath(photoPath)

    However, I am getting the error:

    Microsoft VBScript runtime error '800a004c'
    Path not found

    Isnt the MapPath looking for the physcial folder instead of the Virtual Directory? Is there a way to get the Server.MapPath to see the Virtual Directory I created?
    0
     
    LVL 11

    Expert Comment

    by:mouatts
    Actually I've just twigged what you are trying to do and I don't think that you really need to use mappath.

    I presume you are using the FileSystemObject et al to check for the existence of file. In which case just use the full UNC path when you check the file. If permissions are a problem here then either grant the appropriate permissions on the remote server or set the directory up as a network drive and use the drive letter in the FSO stuff.

    There are some glitches when using UNC which may be the problem that you have had with the mappath. Certainly on a system I worked on recently when you did a password change on the local machine the remote path became inaccessible via IIS but ok via Explorer. The solution was to go to lunch and hope it worked when you got back!

    HTH
    Steve
    0
     
    LVL 1

    Author Comment

    by:colin_palmer
    This is the code im using:

    <!--#include file="connection.asp" -->
    <html>

    <body bgcolor="#FFFFFF" text="#000000">
    <%
    'Create a recordset object
    Set rstRange = Server.CreateObject("ADODB.Recordset")

          ' Find Names of people who have withheld their photos
          SQL = "SELECT ID AS ID, FIRST_NAME ++ ' ' ++ SUR_NAME AS Name "
          SQL = SQL & "FROM  tb_Staff_Member "
          SQL = SQL & "WHERE (PHOTO = N'with_held.jpg') "
          SQL = SQL & "ORDER BY SUR_NAME "
          rstRange.Open sql,ObjConn,adOpenKeyset
          %>
          
    <table  border="1" align="center">
      <tr>
        <td colspan="2">
          <div align="center"><b>Members of Staff Who have with held their Photos</b></div>
        </td>
      </tr>
      <tr>
        <td>
          <div align="center"><b>ID</b></div>
        </td>
        <td>
          <div align="center"><b>Name</b></div>
        </td>
      </tr>
      <%
      while NOT rstRange.EOF
      %>
      <tr>
        <td><%=rstRange("ID")%></td>
        <td><%=rstRange("Name")%></td>
      </tr>
      <% rstRange.MoveNext
      Wend
      rstRange.Close
      %>
    </table>

          
    <p align="center">Building Table........... This requires a search of the photo
      files Please wait .........</p>

    <table  border="1" align="center">
      <tr>
        <td colspan="2">
          <div align="center"><b>Members of Staff Who have No Photos</b></div>
        </td>
      </tr>
      <tr>
        <td>
          <div align="center"><b>ID</b></div>
        </td>
        <td>
          <div align="center"><b>Name</b></div>
        </td>
      </tr>
      <%
    dim strPathInfo, strPhysicalPath,strPath, objFolder, objSubFolders, objFSO, objFile, photoPath
    dim objFolderContents,objFileItem, MyFile
    photoPath = "StudentPhotos"
    strPhysicalPath = Server.MapPath(photoPath)

    Response.write "This file is: " & strPhysicalPath

    set objFSO = CreateObject("Scripting.FileSystemObject")

    set obJFolder = objFSO.GetFolder(strPhysicalPath)
    set objFolderContents = objFolder.Files

    sql = "SELECT ID AS ID, FIRST_NAME ++ ' ' ++ SUR_NAME AS Name  FROM tb_staff_Member ORDER BY SUR_NAME"
    rstRange.Open sql,ObjConn,adOpenKeyset
    rstRange.MoveFirst

          DO WHILE NOT rstRange.EOF
          checkID = rstRange("ID")
          
                For each objFileItem in objFolderContents
                      if checkID = Left(objFileItem.name,8) THEN
                      found = true
                      EXIT FOR
                      Else
                      found = false
                      end if
                next
                
                if found = false then
                Response.Write "<tr><td>" & checkID & "</td>"
                Response.Write "<td>" & rstRange("NAME")& "</td></tr>"
                count = count + 1
                end if
                
          rstRange.MoveNext
          LOOP
          Response.Write "<tr><td>Total</td><td>" & count & "</td></tr>"
          rstRange.Close
          
    %>
    </table>
    <p>
    </body>

    </html>
    <!--#include file="footer.asp" -->
    0
     
    LVL 11

    Expert Comment

    by:mouatts
    Try this

    strPhysicalPath = "\\server2\StudentPhotos"

    Response.write "This file is: " & strPhysicalPath
    set objFSO = CreateObject("Scripting.FileSystemObject")
    set obJFolder = objFSO.GetFolder(strPhysicalPath)
    set objFolderContents = objFolder.Files
    0
     
    LVL 1

    Author Comment

    by:colin_palmer
    Hi Steve

    your help is much appreciated, I tried the above and am still getting path not found error.

    If I type the URL in my browser: http://mars/hydraphotos/photoID.jpg
    this works

    So I also tried

    strPhysicalPath = "http://mars/hydraphotos"

    This does not work either. Very weird.
    0
     
    LVL 11

    Expert Comment

    by:mouatts
    No you can't use http with FSO you will need to use either the UNC or map the directory and use the drive letter eg

    strPhysicalPath="\\mars\hydraphotos"

    or assuming mapped to e:

    strPhysicalPath="e:\"

    Steve
    0
     
    LVL 1

    Author Comment

    by:colin_palmer
    Hi Steve

    I still cannot see the folder contents on another server so I decided to copy the contents of the folder into a folder on the same server as the webpage and then ill write a batch file to copy any new files added to the folder.

    Many thanks for you help, you deserve the points

    regards

    Colin
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
    Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now