0

I want to check for the existence of a file on a remote server -

Current code:

FUNCTION CheckFiles(targetServer, prefix)
    ON ERROR RESUME NEXT
    Dim xmlhttp
    fileURL = "<path omitted>"
    Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
    xmlhttp.open "GET", fileURL, False
    xmlhttp.send
    If(Err.Number<>0) then
        Response.Write "Could not connect to remote server"
    else
        Select Case Cint(xmlhttp.status)
            Case 200, 202, 302
                Set xmlhttp = Nothing
                DoesFileExist = True
            Case Else
                Set xmlhttp = Nothing
                DoesFileExist = False
        End Select
    end if
    ON ERROR GOTO 0
End FUNCTION

After reading some more examples with a sample similar to this it's always used with a web URL (should have been obvious to me by the ServerXMLHTTP but anyways...

Is it possible to perform something like this but using the Windows file sharing method? For example:

"\\"+targetServer+"\c$\path1\path2\path3\"+prefix+"-filename.txt"
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
dschwartz0815
  • 127
  • 2
  • 7
  • Have you tried creating a mapped drive to the remote server, then you can treat it as you would a local file – Dijkgraaf Jan 14 '21 at 19:48
  • @Dijkgraaf I did consider that but can it be done through the ASP page? Because this ASP page would be left on multiple web servers all needed to target their corresponding app server – dschwartz0815 Jan 14 '21 at 19:54
  • 3
    Your function is called `CheckFiles`, but the boolean value is being assigned to `DoesFileExist`, so the function won't return anything. Also, try using the `MSXML2.XMLHTTP.6.0` object rather than `MSXML2.ServerXMLHTTP` – Adam Jan 14 '21 at 21:42
  • If the target servers are all inside the same domain you could just use `Scripting.FileSystemObject` to talk to them via UNC path. I would, however, suggest specifying a public share rather than connecting to the administrative share though. – user692942 Jan 15 '21 at 12:13
  • @Adam Thank you for pointing out the syntax error, but even with MSXML2.XMLHTTP.6.0 I'm still getting "Could not connect to remote server" returned. Working on trying out user692942's suggestion now – dschwartz0815 Jan 15 '21 at 12:45
  • @Adam Becareful `MSXML2.XMLHTTP.6.0`, is not designed for a server environment unlike `MSXML2.ServerXMLHTTP`. – user692942 Jan 15 '21 at 13:21
  • @dschwartz0815 Are you connecting to a URL on the server using the XMLHTTP approach you aren't just passing a UNC path to it right? The target server will need to be running a web server with the correct permissions set or anonymous access. – user692942 Jan 15 '21 at 13:23
  • @user692942 That's exactly what I was doing, dumb mistake. I'd like to make use of the UNC path since I'm running this page on a web server and checking for a file(s) on an app server so there's no IIS or any web components – dschwartz0815 Jan 15 '21 at 13:28
  • @dschwartz0815 In that case, the `Scripting.FilesystemObject` is the correct approach, this code will never work as it's expecting to talk over HTTP, not SMB. – user692942 Jan 15 '21 at 13:38
  • @user692942 What if I set the URL like so: fileURL = "file://"+targetServer+"/c$/path/path/"+env+"/ServerConfigurations/"+prefix+".text"? It works locally through a browser but I'm still getting an error connecting. Is this not doable without FilesystemObject? – dschwartz0815 Jan 15 '21 at 13:40
  • 1
    @dschwartz0815 Honestly, I've never tried to use the `file:` uri scheme through an XMLHTTP request on a server. I can't see it working though, to be honest due to the security concerns of allowing access to server resources over HTTP but you are welcome to try. Though I'd go with the tried and tested `Scripting.FileSystemObject` method. – user692942 Jan 15 '21 at 13:47
  • Appreciate the feedback on this @user692942, I'll see where I can get with this! – dschwartz0815 Jan 15 '21 at 13:54

0 Answers0