how we write in text file, stored on remote computer through ASP

Posted on 2005-04-08
Last Modified: 2008-03-10
how we can write in a text file which is stored on a remote computer through ASP
Question by:imranlakhani
    LVL 8

    Expert Comment

    by remote computer, do you mean a computer which is NOT the one hosting the script?

    if its the computer hosting the script, then it is treat as a client computer, since IT is running the script..

    for another machine, i guess you would need to set something up especially..
    LVL 25

    Accepted Solution

    > how we can write in a text file which is stored on a remote computer through ASP
    You can't, unless you're giving shared access to this file; for obvious security reasons.
    LVL 7

    Expert Comment

    What about FTP? You could open an FTP connect, download, modify file to your liking, and upload it. Or you could setup a small server and have the sending ASP talk to the remote ASP (or PHP!) and have remote ASP make the changes.

    Both of these are entirely possible.

    Example ftp transfer program (keep in mind, I'm a linux/apache/php/mysql dork):

    <%@ Language=VBScript %>
    ' FTP via ASP without using 3rd-party components
    ' Ben Meghreblian 15th Jan 2002
    ' benmeg at benmeg dot com /
    ' This script assumes the file to be FTP'ed is in the same directory as this script.
    ' It should be obvious how to change this (*hint* change the lcd line).
    ' You may specify a wildcard in ftp_files_to_put (e.g. *.txt).

    ' NB: You need to have C:\winnt\system32\wshom.ocx registered to use the WSCRIPT.SHELL object.
    ' It is registered by default, but is sometimes removed for security reasons (no kidding!).
    ' You will also need cmd.exe in the path, which again is there, unless the box is locked down.
    ' Check with your web host/resident sysadmin if in doubt.

    Dim objFSO, objTextFile, oScript, oScriptNet, oFileSys, oFile, strCMD, strTempFile, strCommandResult
    Dim ftp_address, ftp_username, ftp_password, ftp_physical_path, ftp_files_to_put

    ' Edit these variables to match your specifications
    ftp_address          = ""
    ftp_username         = "username"
    ftp_password         = "password"
    ftp_remote_directory = "subdirectory" ' Leave blank if uploading to root directory
    ftp_files_to_put     = "file.txt"

    On Error Resume Next

    Set oScript = Server.CreateObject("WSCRIPT.SHELL")
    Set oFileSys = Server.CreateObject("Scripting.FileSystemObject")
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Build our ftp-commands file
    Set objTextFile = objFSO.CreateTextFile(Server.MapPath("test.ftp"))

    objTextFile.WriteLine "lcd " & Server.MapPath(".")
    objTextFile.WriteLine "open " & ftp_address
    objTextFile.WriteLine ftp_username
    objTextFile.WriteLine ftp_password

    ' Check to see if we need to issue a 'cd' command
    If ftp_remote_directory <> "" Then
       objTextFile.WriteLine "cd " & ftp_remote_directory
    End If

    objTextFile.WriteLine "prompt"

    ' If the file(s) is/are binary (i.e. .jpg, .mdb, etc..), uncomment the following line
    ' objTextFile.WriteLine "binary"

    ' If there are multiple files to put, we need to use the command 'mput', instead of 'put'
    If Instr(1, ftp_files_to_put, "*",1) Then
       objTextFile.WriteLine "mput " & ftp_files_to_put
       objTextFile.WriteLine "put " & ftp_files_to_put
    End If

    objTextFile.WriteLine "bye"

    Set objTextFile = Nothing

    ' Use cmd.exe to run ftp.exe, parsing our newly created command file
    strCMD = "ftp.exe -s:" & Server.MapPath("test.ftp")
    strTempFile = "C:\" & oFileSys.GetTempName( )

    ' Pipe output from cmd.exe to a temporary file (Not :| Steve)
    Call oScript.Run ("cmd.exe /c " & strCMD & " > " & strTempFile, 0, True)
    Set oFile = oFileSys.OpenTextFile (strTempFile, 1, False, 0)

    On Error Resume Next
    ' Grab output from temporary file
    strCommandResult = Server.HTMLEncode( oFile.ReadAll )

    ' Delete the temporary & ftp-command files
    Call oFileSys.DeleteFile( strTempFile, True )
    Call objFSO.DeleteFile( Server.MapPath("test.ftp"), True )
    Set oFileSys = Nothing
    Set objFSO = Nothing

    ' Print result of FTP session to screen
    Response.Write( Replace( strCommandResult, vbCrLf, "<br>", 1, -1, 1) )


    Author Comment

    i need to connect client computer to web server(host on site) and a gateway(which is a remote server):

    message send/received process:
    client computer---->web server----->gateway
    gateway---->web server----->client computer

    gateway has 3 text file
    1. Out File
    2. In File
    3. Message File

    client have destop application which connect to web server and through web server message send to gateway and viceversa.

    how i send this file in xml format/string?

    LVL 7

    Expert Comment

    For XML format, just write out XML. I do think this is best handled in FTP. However, you can do it with Web. Just create programs on each end to do the processing.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    728 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