Advertisement
ftkr

FTP VFP

Jul 22nd, 2019
2,735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. **********************************************************************************
  2. *... https://www.tek-tips.com/faqs.cfm?fid=3234 ...*
  3. *... FTPGet.PRG ...*
  4.  
  5. PROCEDURE ftpGet
  6.     PARAMETERS lcHost, lcUser, lcPwd, lcRemoteFile, lcNewFile, lnXFerType
  7.  
  8.     *.................................................................................
  9.     *:   Usage: DO ftpget WITH ;
  10.     *:         'ftp.host', 'name', 'password', 'source.file', 'target.file'[, 1 | 2]
  11.     *:
  12.     *:  Where:  lcHost       = Host computer IP address or name
  13.     *:          lcUser       = user name - anonymous may be used
  14.     *:          lcPwd        = password
  15.     *:          lcRemoteFile = source file name
  16.     *:          lcNewFile    = target file name
  17.     *:          lnXFerType   = 1 (default) for ascii, 2 for binary
  18.     *.................................................................................
  19.  
  20.     *...set up API calls
  21.     DECLARE INTEGER InternetOpen IN wininet;
  22.         STRING sAgent, INTEGER lAccessType, STRING sProxyName,;
  23.         STRING sProxyBypass, STRING  lFlags
  24.  
  25.     DECLARE INTEGER InternetCloseHandle IN wininet INTEGER hInet
  26.  
  27.     DECLARE INTEGER InternetConnect IN wininet.DLL;
  28.         INTEGER hInternetSession,;
  29.         STRING  lcHost,;
  30.         INTEGER nServerPort,;
  31.         STRING  lcUser,;
  32.         STRING  lcPassword,;
  33.         INTEGER lService,;
  34.         INTEGER lFlags,;
  35.         INTEGER lContext
  36.  
  37.     DECLARE INTEGER FtpGetFile IN wininet;
  38.         INTEGER hftpSession, ;
  39.         STRING  lcRemoteFile,;
  40.         STRING  lcNewFile, ;
  41.         INTEGER fFailIfExists,;
  42.         INTEGER dwFlagsAndAttributes,;
  43.         INTEGER dwFlags, ;
  44.         INTEGER dwContext
  45.  
  46.     lcHost       = ALLTRIM(lcHost)
  47.     lcUser       = ALLTRIM(lcUser)
  48.     lcPwd        = ALLTRIM(lcPwd)
  49.     lcRemoteFile = ALLTRIM(lcRemoteFile)
  50.     lcNewFile    = ALLTRIM(lcNewFile)
  51.  
  52.     sAgent = "vfp"
  53.  
  54.     sProxyName = CHR(0)     &&... no proxy
  55.     sProxyBypass = CHR(0)   &&... nothing to bypass
  56.     lFlags = 0              &&... no flags used
  57.  
  58.     *... initialize access to Inet functions
  59.     hOpen = InternetOpen (sAgent, 1,;
  60.         sProxyName, sProxyBypass, lFlags)
  61.  
  62.     IF hOpen = 0
  63.         WAIT WINDOW  "Unable to get access to WinInet.Dll" TIMEOUT 2
  64.         RETURN
  65.     ENDIF
  66.  
  67.     *... The first '0' says use the default port, usually 21.
  68.     hftpSession = InternetConnect (hOpen, lcHost,;
  69.         0, lcUser, lcPwd, 1, 0, 0)   &&... 1 = ftp protocol
  70.  
  71.     IF hftpSession = 0
  72.         *... close access to Inet functions and exit
  73.         = InternetCloseHandle (hOpen)
  74.         WAIT WINDOW "Unable to connect to " + lcHost + '.' TIMEOUT 2
  75.         RETURN
  76.     ELSE
  77.         WAIT WINDOW "Connected to " + lcHost + " as: [" + lcUser + "]"  TIMEOUT 1
  78.     ENDIF
  79.  
  80.     *... 0 to automatically overwrite file
  81.     *... 1 to fail if file already exists
  82.     fFailIfExists  = 0
  83.     dwContext      = 0  &&... used for callback
  84.  
  85.     WAIT WINDOW 'Transferring ' + lcRemoteFile + ' to ' + lcNewFile + '...' NOWAIT
  86.     lnResult = FtpGetFile (hftpSession, lcRemoteFile, lcNewFile,;
  87.         fFailIfExists, 128, lnXFerType,;
  88.         dwContext)
  89.  
  90.     *... 128 = #define FILE_ATTRIBUTE_NORMAL     0x00000080
  91.     *... See CreateFile for other attributes
  92.  
  93.     * close handles
  94.     = InternetCloseHandle (hftpSession)
  95.     = InternetCloseHandle (hOpen)
  96.  
  97.     IF lnResult = 1
  98.         *... successful download, do what you want here
  99.         WAIT WINDOW 'Completed.' TIMEOUT 1
  100.         **MODI FILE (lcNewFile)
  101.     ELSE
  102.         WAIT WINDOW  "Unable to download selected file" TIMEOUT 2
  103.     ENDIF
  104.  
  105.     RETURN
  106.     *** End of ftpGet.PRG *************************************************************
  107. ENDPROC
  108.  
  109. **********************************************************************************
  110. *... FTPPut.PRG ...*
  111.  
  112. FUNCTION ftpPut
  113.     PARAMETERS lcHost, lcUser, lcPassword, lcSource, lcTarget, lnXFerType
  114.  
  115.     *.................................................................................
  116.     *:   Usage: DO ftpput WITH ;
  117.     *:         'ftp.host', 'name', 'password', 'source.file', 'target.file'[, 1 | 2]
  118.     *:
  119.     *:  Where:  lcHost     = Host computer IP address or name
  120.     *:          lcUser     = user name - anonymous may be used
  121.     *:          lcPassword = password
  122.     *:          lcSource   = source file name (remote)
  123.     *:          lcTarget   = target file name (local)
  124.     *:          lnXFerType = 1 (default) for ascii, 2 for binary
  125.     *.................................................................................
  126.  
  127.     DECLARE INTEGER InternetOpen IN wininet.DLL;
  128.         STRING  sAgent,;
  129.         INTEGER lAccessType,;
  130.         STRING  sProxyName,;
  131.         STRING  sProxyBypass,;
  132.         STRING  lFlags
  133.  
  134.     DECLARE INTEGER InternetCloseHandle IN wininet.DLL INTEGER hInet
  135.  
  136.     DECLARE INTEGER InternetConnect IN wininet.DLL;
  137.         INTEGER hInternetSession,;
  138.         STRING  lcHost,;
  139.         INTEGER nServerPort,;
  140.         STRING  lcUser,;
  141.         STRING  lcPassword,;
  142.         INTEGER lService,;
  143.         INTEGER lFlags,;
  144.         INTEGER lContext
  145.  
  146.     DECLARE INTEGER FtpPutFile IN wininet.DLL;
  147.         INTEGER hConnect,;
  148.         STRING  lpszLocalFile,;
  149.         STRING  lpszNewRemoteFile,;
  150.         INTEGER dwFlags,;
  151.         INTEGER dwContext
  152.  
  153.     PUBLIC hOpen, hftpSession
  154.  
  155.     lcHost     = ALLTRIM(lcHost)
  156.     lcUser     = ALLTRIM(lcUser)
  157.     lcPassword = ALLTRIM(lcPassword)
  158.     lcSource   = ALLTRIM(lcSource)
  159.     lcTarget   = ALLTRIM(lcTarget)
  160.  
  161.     LOCAL lSukses
  162.     lSukses = .F.
  163.     IF connect2ftp (lcHost, lcUser, lcPassword)
  164.         WAIT WINDOW 'Transferring....' NOWAIT
  165.         IF FtpPutFile(hftpSession, lcSource,;
  166.                 lcTarget, lnXFerType, 0) = 1
  167.             WAIT WINDOW lcSource + ' transferred.' TIMEOUT 2
  168.             lSukses = .T.
  169.         ENDIF
  170.  
  171.         = InternetCloseHandle (hftpSession)
  172.         = InternetCloseHandle (hOpen)
  173.     ENDIF
  174.     RETURN lSukses
  175. ENDPROC
  176. *..................... connect2ftp .........................................
  177. *...  Makes sure there is actually a valid connection to the host
  178. FUNCTION  connect2ftp (lcHost, lcUser, lcPassword)
  179.     * open access to Inet functions
  180.     hOpen = InternetOpen ("vfp", 1, 0, 0, 0)
  181.  
  182.     IF hOpen = 0
  183.         ? "Unable to get access to WinInet.Dll"
  184.         RETURN .F.
  185.     ENDIF
  186.  
  187.     *... The first '0' says use the default port, usually 21.
  188.     hftpSession = InternetConnect (hOpen, lcHost,;
  189.         0, lcUser, lcPassword, 1, 0, 0)   &&... 1 = ftp protocol
  190.  
  191.     IF hftpSession = 0
  192.         * close access to Inet functions and exit
  193.         = InternetCloseHandle (hOpen)
  194.         ? "ftp " + lcHost + " is not available"
  195.         RETURN .F.
  196.     ELSE
  197.         ? "Connected to " + lcHost
  198.     ENDIF
  199.     RETURN .T.
  200.     RETURN
  201.     *** End of ftpPut.PRG *************************************************************
  202.  
  203.  
  204.     *!* Now if you want to delete the remote file after downloading it,
  205.     *!* you can use FtpDelete.PRG:
  206.  
  207.     **********************************************************************************
  208.     *... FtpDelete.PRG ...*
  209.  
  210. PROCEDURE ftpDelete
  211.     PARAMETERS lcHost, lcUser, lcPwd, lcRemoteFile
  212.  
  213.     *.................................................................................
  214.     *:   Usage: DO ftpdelete WITH ;
  215.     *:         'ftpserver.host', 'name', 'password', 'delete.file'
  216.     *:
  217.     *:  Where:  lcHost       = Host computer IP address or name
  218.     *:          lcUser       = user name - anonymous may be used
  219.     *:          lcPwd        = password
  220.     *:          lcRemoteFile = file to delete
  221.     *.................................................................................
  222.  
  223.     *...set up API calls
  224.     PUBLIC hOpen, hftpSession
  225.     DECLARE INTEGER InternetOpen IN wininet.DLL;
  226.         STRING  sAgent,;
  227.         INTEGER lAccessType,;
  228.         STRING  sProxyName,;
  229.         STRING  sProxyBypass,;
  230.         STRING  lFlags
  231.  
  232.     DECLARE INTEGER InternetCloseHandle IN wininet.DLL INTEGER hInet
  233.  
  234.     DECLARE INTEGER InternetConnect IN wininet.DLL;
  235.         INTEGER hInternetSession,;
  236.         STRING  sServerName,;
  237.         INTEGER nServerPort,;
  238.         STRING  sUsername,;
  239.         STRING  sPassword,;
  240.         INTEGER lService,;
  241.         INTEGER lFlags,;
  242.         INTEGER lContext
  243.  
  244.     DECLARE INTEGER FtpDeleteFile IN wininet.DLL;
  245.         INTEGER hConnect,;
  246.         STRING  lpszFileName
  247.  
  248.     *... open access to Inet functions
  249.     hOpen = InternetOpen ("vfp", 1, 0, 0, 0)
  250.  
  251.     IF hOpen = 0
  252.         ? "Unable to get access to WinInet.Dll"
  253.         RETURN .F.
  254.     ENDIF
  255.  
  256.     *... connect to ftp host
  257.     hftpSession = InternetConnect (hOpen, lcHost, 0,;
  258.         lcUser, lcPwd, 1, 0, 0)
  259.  
  260.     IF hftpSession = 0
  261.         * close access to Inet functions and exit
  262.         = InternetCloseHandle (hOpen)
  263.         WAIT WINDOW "ftp " + strHost + " is not available"  TIMEOUT 2
  264.     ELSE
  265.         WAIT WINDOW "Connected to " + lcHost + " as: [" + lcUser + "]"  NOWAIT
  266.         IF FtpDeleteFile(hftpSession, lcRemoteFile) = 1
  267.             WAIT WINDOW lcRemoteFile + ' deleted.' TIMEOUT 1
  268.         ELSE
  269.             WAIT WINDOW 'Error deleting ' + lcRemoteFile + "." TIMEOUT 1
  270.         ENDIF
  271.     ENDIF
  272.     = InternetCloseHandle (hftpSession)
  273.     = InternetCloseHandle (hOpen)
  274.     *** End of ftpDelete.PRG *************************************************************
  275. ENDPR
  276. *... https://www.tek-tips.com/faqs.cfm?fid=3234 ...*
  277. *... FTPGet.PRG ...*
  278.  
  279. PROCEDURE ftpGet
  280.     PARAMETERS lcHost, lcUser, lcPwd, lcRemoteFile, lcNewFile, lnXFerType
  281.  
  282.     *.................................................................................
  283.     *:   Usage: DO ftpget WITH ;
  284.     *:         'ftp.host', 'name', 'password', 'source.file', 'target.file'[, 1 | 2]
  285.     *:
  286.     *:  Where:  lcHost       = Host computer IP address or name
  287.     *:          lcUser       = user name - anonymous may be used
  288.     *:          lcPwd        = password
  289.     *:          lcRemoteFile = source file name
  290.     *:          lcNewFile    = target file name
  291.     *:          lnXFerType   = 1 (default) for ascii, 2 for binary
  292.     *.................................................................................
  293.  
  294.     *...set up API calls
  295.     DECLARE INTEGER InternetOpen IN wininet;
  296.         STRING sAgent, INTEGER lAccessType, STRING sProxyName,;
  297.         STRING sProxyBypass, STRING  lFlags
  298.  
  299.     DECLARE INTEGER InternetCloseHandle IN wininet INTEGER hInet
  300.  
  301.     DECLARE INTEGER InternetConnect IN wininet.DLL;
  302.         INTEGER hInternetSession,;
  303.         STRING  lcHost,;
  304.         INTEGER nServerPort,;
  305.         STRING  lcUser,;
  306.         STRING  lcPassword,;
  307.         INTEGER lService,;
  308.         INTEGER lFlags,;
  309.         INTEGER lContext
  310.  
  311.     DECLARE INTEGER FtpGetFile IN wininet;
  312.         INTEGER hftpSession, ;
  313.         STRING  lcRemoteFile,;
  314.         STRING  lcNewFile, ;
  315.         INTEGER fFailIfExists,;
  316.         INTEGER dwFlagsAndAttributes,;
  317.         INTEGER dwFlags, ;
  318.         INTEGER dwContext
  319.  
  320.     lcHost       = ALLTRIM(lcHost)
  321.     lcUser       = ALLTRIM(lcUser)
  322.     lcPwd        = ALLTRIM(lcPwd)
  323.     lcRemoteFile = ALLTRIM(lcRemoteFile)
  324.     lcNewFile    = ALLTRIM(lcNewFile)
  325.  
  326.     sAgent = "vfp"
  327.  
  328.     sProxyName = CHR(0)     &&... no proxy
  329.     sProxyBypass = CHR(0)   &&... nothing to bypass
  330.     lFlags = 0              &&... no flags used
  331.  
  332.     *... initialize access to Inet functions
  333.     hOpen = InternetOpen (sAgent, 1,;
  334.         sProxyName, sProxyBypass, lFlags)
  335.  
  336.     IF hOpen = 0
  337.         WAIT WINDOW  "Unable to get access to WinInet.Dll" TIMEOUT 2
  338.         RETURN
  339.     ENDIF
  340.  
  341.     *... The first '0' says use the default port, usually 21.
  342.     hftpSession = InternetConnect (hOpen, lcHost,;
  343.         0, lcUser, lcPwd, 1, 0, 0)   &&... 1 = ftp protocol
  344.  
  345.     IF hftpSession = 0
  346.         *... close access to Inet functions and exit
  347.         = InternetCloseHandle (hOpen)
  348.         WAIT WINDOW "Unable to connect to " + lcHost + '.' TIMEOUT 2
  349.         RETURN
  350.     ELSE
  351.         WAIT WINDOW "Connected to " + lcHost + " as: [" + lcUser + "]"  TIMEOUT 1
  352.     ENDIF
  353.  
  354.     *... 0 to automatically overwrite file
  355.     *... 1 to fail if file already exists
  356.     fFailIfExists  = 0
  357.     dwContext      = 0  &&... used for callback
  358.  
  359.     WAIT WINDOW 'Transferring ' + lcRemoteFile + ' to ' + lcNewFile + '...' NOWAIT
  360.     lnResult = FtpGetFile (hftpSession, lcRemoteFile, lcNewFile,;
  361.         fFailIfExists, 128, lnXFerType,;
  362.         dwContext)
  363.  
  364.     *... 128 = #define FILE_ATTRIBUTE_NORMAL     0x00000080
  365.     *... See CreateFile for other attributes
  366.  
  367.     * close handles
  368.     = InternetCloseHandle (hftpSession)
  369.     = InternetCloseHandle (hOpen)
  370.  
  371.     IF lnResult = 1
  372.         *... successful download, do what you want here
  373.         WAIT WINDOW 'Completed.' TIMEOUT 1
  374.         **MODI FILE (lcNewFile)
  375.     ELSE
  376.         WAIT WINDOW  "Unable to download selected file" TIMEOUT 2
  377.     ENDIF
  378.  
  379.     RETURN
  380.     *** End of ftpGet.PRG *************************************************************
  381. ENDPROC
  382. **********************************************************************************
  383. *... FTPPut.PRG ...*
  384.  
  385. FUNCTION ftpPut
  386.     PARAMETERS lcHost, lcUser, lcPassword, lcSource, lcTarget, lnXFerType
  387.  
  388.     *.................................................................................
  389.     *:   Usage: DO ftpput WITH ;
  390.     *:         'ftp.host', 'name', 'password', 'source.file', 'target.file'[, 1 | 2]
  391.     *:
  392.     *:  Where:  lcHost     = Host computer IP address or name
  393.     *:          lcUser     = user name - anonymous may be used
  394.     *:          lcPassword = password
  395.     *:          lcSource   = source file name (remote)
  396.     *:          lcTarget   = target file name (local)
  397.     *:          lnXFerType = 1 (default) for ascii, 2 for binary
  398.     *.................................................................................
  399.  
  400.     DECLARE INTEGER InternetOpen IN wininet.DLL;
  401.         STRING  sAgent,;
  402.         INTEGER lAccessType,;
  403.         STRING  sProxyName,;
  404.         STRING  sProxyBypass,;
  405.         STRING  lFlags
  406.  
  407.     DECLARE INTEGER InternetCloseHandle IN wininet.DLL INTEGER hInet
  408.  
  409.     DECLARE INTEGER InternetConnect IN wininet.DLL;
  410.         INTEGER hInternetSession,;
  411.         STRING  lcHost,;
  412.         INTEGER nServerPort,;
  413.         STRING  lcUser,;
  414.         STRING  lcPassword,;
  415.         INTEGER lService,;
  416.         INTEGER lFlags,;
  417.         INTEGER lContext
  418.  
  419.     DECLARE INTEGER FtpPutFile IN wininet.DLL;
  420.         INTEGER hConnect,;
  421.         STRING  lpszLocalFile,;
  422.         STRING  lpszNewRemoteFile,;
  423.         INTEGER dwFlags,;
  424.         INTEGER dwContext
  425.  
  426.     PUBLIC hOpen, hftpSession
  427.  
  428.     lcHost     = ALLTRIM(lcHost)
  429.     lcUser     = ALLTRIM(lcUser)
  430.     lcPassword = ALLTRIM(lcPassword)
  431.     lcSource   = ALLTRIM(lcSource)
  432.     lcTarget   = ALLTRIM(lcTarget)
  433.  
  434.     LOCAL lSukses
  435.     lSukses = .F.
  436.     IF connect2ftp (lcHost, lcUser, lcPassword)
  437.         WAIT WINDOW 'Transferring....' NOWAIT
  438.         IF FtpPutFile(hftpSession, lcSource,;
  439.                 lcTarget, lnXFerType, 0) = 1
  440.             WAIT WINDOW lcSource + ' transferred.' TIMEOUT 2
  441.             lSukses = .T.
  442.         ENDIF
  443.  
  444.         = InternetCloseHandle (hftpSession)
  445.         = InternetCloseHandle (hOpen)
  446.     ENDIF
  447.     RETURN lSukses
  448. ENDPROC
  449. *..................... connect2ftp .........................................
  450. *...  Makes sure there is actually a valid connection to the host
  451. FUNCTION  connect2ftp (lcHost, lcUser, lcPassword)
  452.     * open access to Inet functions
  453.     hOpen = InternetOpen ("vfp", 1, 0, 0, 0)
  454.  
  455.     IF hOpen = 0
  456.         ? "Unable to get access to WinInet.Dll"
  457.         RETURN .F.
  458.     ENDIF
  459.  
  460.     *... The first '0' says use the default port, usually 21.
  461.     hftpSession = InternetConnect (hOpen, lcHost,;
  462.         0, lcUser, lcPassword, 1, 0, 0)   &&... 1 = ftp protocol
  463.  
  464.     IF hftpSession = 0
  465.         * close access to Inet functions and exit
  466.         = InternetCloseHandle (hOpen)
  467.         ? "ftp " + lcHost + " is not available"
  468.         RETURN .F.
  469.     ELSE
  470.         ? "Connected to " + lcHost
  471.     ENDIF
  472.     RETURN .T.
  473.     RETURN
  474.     *** End of ftpPut.PRG *************************************************************
  475.  
  476.  
  477.     *!* Now if you want to delete the remote file after downloading it,
  478.     *!* you can use FtpDelete.PRG:
  479.  
  480.     **********************************************************************************
  481.     *... FtpDelete.PRG ...*
  482.  
  483. PROCEDURE ftpDelete
  484.     PARAMETERS lcHost, lcUser, lcPwd, lcRemoteFile
  485.  
  486.     *.................................................................................
  487.     *:   Usage: DO ftpdelete WITH ;
  488.     *:         'ftpserver.host', 'name', 'password', 'delete.file'
  489.     *:
  490.     *:  Where:  lcHost       = Host computer IP address or name
  491.     *:          lcUser       = user name - anonymous may be used
  492.     *:          lcPwd        = password
  493.     *:          lcRemoteFile = file to delete
  494.     *.................................................................................
  495.  
  496.     *...set up API calls
  497.     PUBLIC hOpen, hftpSession
  498.     DECLARE INTEGER InternetOpen IN wininet.DLL;
  499.         STRING  sAgent,;
  500.         INTEGER lAccessType,;
  501.         STRING  sProxyName,;
  502.         STRING  sProxyBypass,;
  503.         STRING  lFlags
  504.  
  505.     DECLARE INTEGER InternetCloseHandle IN wininet.DLL INTEGER hInet
  506.  
  507.     DECLARE INTEGER InternetConnect IN wininet.DLL;
  508.         INTEGER hInternetSession,;
  509.         STRING  sServerName,;
  510.         INTEGER nServerPort,;
  511.         STRING  sUsername,;
  512.         STRING  sPassword,;
  513.         INTEGER lService,;
  514.         INTEGER lFlags,;
  515.         INTEGER lContext
  516.  
  517.     DECLARE INTEGER FtpDeleteFile IN wininet.DLL;
  518.         INTEGER hConnect,;
  519.         STRING  lpszFileName
  520.  
  521.     *... open access to Inet functions
  522.     hOpen = InternetOpen ("vfp", 1, 0, 0, 0)
  523.  
  524.     IF hOpen = 0
  525.         ? "Unable to get access to WinInet.Dll"
  526.         RETURN .F.
  527.     ENDIF
  528.  
  529.     *... connect to ftp host
  530.     hftpSession = InternetConnect (hOpen, lcHost, 0,;
  531.         lcUser, lcPwd, 1, 0, 0)
  532.  
  533.     IF hftpSession = 0
  534.         * close access to Inet functions and exit
  535.         = InternetCloseHandle (hOpen)
  536.         WAIT WINDOW "ftp " + strHost + " is not available"  TIMEOUT 2
  537.     ELSE
  538.         WAIT WINDOW "Connected to " + lcHost + " as: [" + lcUser + "]"  NOWAIT
  539.         IF FtpDeleteFile(hftpSession, lcRemoteFile) = 1
  540.             WAIT WINDOW lcRemoteFile + ' deleted.' TIMEOUT 1
  541.         ELSE
  542.             WAIT WINDOW 'Error deleting ' + lcRemoteFile + "." TIMEOUT 1
  543.         ENDIF
  544.     ENDIF
  545.     = InternetCloseHandle (hftpSession)
  546.     = InternetCloseHandle (hOpen)
  547.     *** End of ftpDelete.PRG *************************************************************
  548. ENDPR
  549. **********************************************************************************
  550. *... FTPPut.PRG ...*
  551.  
  552. FUNCTION ftpPut
  553.     PARAMETERS lcHost, lcUser, lcPassword, lcSource, lcTarget, lnXFerType
  554.  
  555.     *.................................................................................
  556.     *:   Usage: DO ftpput WITH ;
  557.     *:         'ftp.host', 'name', 'password', 'source.file', 'target.file'[, 1 | 2]
  558.     *:
  559.     *:  Where:  lcHost     = Host computer IP address or name
  560.     *:          lcUser     = user name - anonymous may be used
  561.     *:          lcPassword = password
  562.     *:          lcSource   = source file name (remote)
  563.     *:          lcTarget   = target file name (local)
  564.     *:          lnXFerType = 1 (default) for ascii, 2 for binary
  565.     *.................................................................................
  566.  
  567.     DECLARE INTEGER InternetOpen IN wininet.DLL;
  568.         STRING  sAgent,;
  569.         INTEGER lAccessType,;
  570.         STRING  sProxyName,;
  571.         STRING  sProxyBypass,;
  572.         STRING  lFlags
  573.  
  574.     DECLARE INTEGER InternetCloseHandle IN wininet.DLL INTEGER hInet
  575.  
  576.     DECLARE INTEGER InternetConnect IN wininet.DLL;
  577.         INTEGER hInternetSession,;
  578.         STRING  lcHost,;
  579.         INTEGER nServerPort,;
  580.         STRING  lcUser,;
  581.         STRING  lcPassword,;
  582.         INTEGER lService,;
  583.         INTEGER lFlags,;
  584.         INTEGER lContext
  585.  
  586.     DECLARE INTEGER FtpPutFile IN wininet.DLL;
  587.         INTEGER hConnect,;
  588.         STRING  lpszLocalFile,;
  589.         STRING  lpszNewRemoteFile,;
  590.         INTEGER dwFlags,;
  591.         INTEGER dwContext
  592.  
  593.     PUBLIC hOpen, hftpSession
  594.  
  595.     lcHost     = ALLTRIM(lcHost)
  596.     lcUser     = ALLTRIM(lcUser)
  597.     lcPassword = ALLTRIM(lcPassword)
  598.     lcSource   = ALLTRIM(lcSource)
  599.     lcTarget   = ALLTRIM(lcTarget)
  600.  
  601.     LOCAL lSukses
  602.     lSukses = .F.
  603.     IF connect2ftp (lcHost, lcUser, lcPassword)
  604.         WAIT WINDOW 'Transferring....' NOWAIT
  605.         IF FtpPutFile(hftpSession, lcSource,;
  606.                 lcTarget, lnXFerType, 0) = 1
  607.             WAIT WINDOW lcSource + ' transferred.' TIMEOUT 2
  608.             lSukses = .T.
  609.         ENDIF
  610.  
  611.         = InternetCloseHandle (hftpSession)
  612.         = InternetCloseHandle (hOpen)
  613.     ENDIF
  614.     RETURN lSukses
  615. ENDPROC
  616. *..................... connect2ftp .........................................
  617. *...  Makes sure there is actually a valid connection to the host
  618. FUNCTION  connect2ftp (lcHost, lcUser, lcPassword)
  619.     * open access to Inet functions
  620.     hOpen = InternetOpen ("vfp", 1, 0, 0, 0)
  621.  
  622.     IF hOpen = 0
  623.         ? "Unable to get access to WinInet.Dll"
  624.         RETURN .F.
  625.     ENDIF
  626.  
  627.     *... The first '0' says use the default port, usually 21.
  628.     hftpSession = InternetConnect (hOpen, lcHost,;
  629.         0, lcUser, lcPassword, 1, 0, 0)   &&... 1 = ftp protocol
  630.  
  631.     IF hftpSession = 0
  632.         * close access to Inet functions and exit
  633.         = InternetCloseHandle (hOpen)
  634.         ? "ftp " + lcHost + " is not available"
  635.         RETURN .F.
  636.     ELSE
  637.         ? "Connected to " + lcHost
  638.     ENDIF
  639.     RETURN .T.
  640.     RETURN
  641.     *** End of ftpPut.PRG *************************************************************
  642.  
  643.  
  644.     *!* Now if you want to delete the remote file after downloading it,
  645.     *!* you can use FtpDelete.PRG:
  646.  
  647.     **********************************************************************************
  648.     *... FtpDelete.PRG ...*
  649.  
  650. PROCEDURE ftpDelete
  651.     PARAMETERS lcHost, lcUser, lcPwd, lcRemoteFile
  652.  
  653.     *.................................................................................
  654.     *:   Usage: DO ftpdelete WITH ;
  655.     *:         'ftpserver.host', 'name', 'password', 'delete.file'
  656.     *:
  657.     *:  Where:  lcHost       = Host computer IP address or name
  658.     *:          lcUser       = user name - anonymous may be used
  659.     *:          lcPwd        = password
  660.     *:          lcRemoteFile = file to delete
  661.     *.................................................................................
  662.  
  663.     *...set up API calls
  664.     PUBLIC hOpen, hftpSession
  665.     DECLARE INTEGER InternetOpen IN wininet.DLL;
  666.         STRING  sAgent,;
  667.         INTEGER lAccessType,;
  668.         STRING  sProxyName,;
  669.         STRING  sProxyBypass,;
  670.         STRING  lFlags
  671.  
  672.     DECLARE INTEGER InternetCloseHandle IN wininet.DLL INTEGER hInet
  673.  
  674.     DECLARE INTEGER InternetConnect IN wininet.DLL;
  675.         INTEGER hInternetSession,;
  676.         STRING  sServerName,;
  677.         INTEGER nServerPort,;
  678.         STRING  sUsername,;
  679.         STRING  sPassword,;
  680.         INTEGER lService,;
  681.         INTEGER lFlags,;
  682.         INTEGER lContext
  683.  
  684.     DECLARE INTEGER FtpDeleteFile IN wininet.DLL;
  685.         INTEGER hConnect,;
  686.         STRING  lpszFileName
  687.  
  688.     *... open access to Inet functions
  689.     hOpen = InternetOpen ("vfp", 1, 0, 0, 0)
  690.  
  691.     IF hOpen = 0
  692.         ? "Unable to get access to WinInet.Dll"
  693.         RETURN .F.
  694.     ENDIF
  695.  
  696.     *... connect to ftp host
  697.     hftpSession = InternetConnect (hOpen, lcHost, 0,;
  698.         lcUser, lcPwd, 1, 0, 0)
  699.  
  700.     IF hftpSession = 0
  701.         * close access to Inet functions and exit
  702.         = InternetCloseHandle (hOpen)
  703.         WAIT WINDOW "ftp " + strHost + " is not available"  TIMEOUT 2
  704.     ELSE
  705.         WAIT WINDOW "Connected to " + lcHost + " as: [" + lcUser + "]"  NOWAIT
  706.         IF FtpDeleteFile(hftpSession, lcRemoteFile) = 1
  707.             WAIT WINDOW lcRemoteFile + ' deleted.' TIMEOUT 1
  708.         ELSE
  709.             WAIT WINDOW 'Error deleting ' + lcRemoteFile + "." TIMEOUT 1
  710.         ENDIF
  711.     ENDIF
  712.     = InternetCloseHandle (hftpSession)
  713.     = InternetCloseHandle (hOpen)
  714.     *** End of ftpDelete.PRG *************************************************************
  715. ENDPR
  716. *... FTPPut.PRG ...*
  717.  
  718. FUNCTION ftpPut
  719.     PARAMETERS lcHost, lcUser, lcPassword, lcSource, lcTarget, lnXFerType
  720.  
  721.     *.................................................................................
  722.     *:   Usage: DO ftpput WITH ;
  723.     *:         'ftp.host', 'name', 'password', 'source.file', 'target.file'[, 1 | 2]
  724.     *:
  725.     *:  Where:  lcHost     = Host computer IP address or name
  726.     *:          lcUser     = user name - anonymous may be used
  727.     *:          lcPassword = password
  728.     *:          lcSource   = source file name (remote)
  729.     *:          lcTarget   = target file name (local)
  730.     *:          lnXFerType = 1 (default) for ascii, 2 for binary
  731.     *.................................................................................
  732.  
  733.     DECLARE INTEGER InternetOpen IN wininet.DLL;
  734.         STRING  sAgent,;
  735.         INTEGER lAccessType,;
  736.         STRING  sProxyName,;
  737.         STRING  sProxyBypass,;
  738.         STRING  lFlags
  739.  
  740.     DECLARE INTEGER InternetCloseHandle IN wininet.DLL INTEGER hInet
  741.  
  742.     DECLARE INTEGER InternetConnect IN wininet.DLL;
  743.         INTEGER hInternetSession,;
  744.         STRING  lcHost,;
  745.         INTEGER nServerPort,;
  746.         STRING  lcUser,;
  747.         STRING  lcPassword,;
  748.         INTEGER lService,;
  749.         INTEGER lFlags,;
  750.         INTEGER lContext
  751.  
  752.     DECLARE INTEGER FtpPutFile IN wininet.DLL;
  753.         INTEGER hConnect,;
  754.         STRING  lpszLocalFile,;
  755.         STRING  lpszNewRemoteFile,;
  756.         INTEGER dwFlags,;
  757.         INTEGER dwContext
  758.  
  759.     PUBLIC hOpen, hftpSession
  760.  
  761.     lcHost     = ALLTRIM(lcHost)
  762.     lcUser     = ALLTRIM(lcUser)
  763.     lcPassword = ALLTRIM(lcPassword)
  764.     lcSource   = ALLTRIM(lcSource)
  765.     lcTarget   = ALLTRIM(lcTarget)
  766.  
  767.     LOCAL lSukses
  768.     lSukses = .F.
  769.     IF connect2ftp (lcHost, lcUser, lcPassword)
  770.         WAIT WINDOW 'Transferring....' NOWAIT
  771.         IF FtpPutFile(hftpSession, lcSource,;
  772.                 lcTarget, lnXFerType, 0) = 1
  773.             WAIT WINDOW lcSource + ' transferred.' TIMEOUT 2
  774.             lSukses = .T.
  775.         ENDIF
  776.  
  777.         = InternetCloseHandle (hftpSession)
  778.         = InternetCloseHandle (hOpen)
  779.     ENDIF
  780.     RETURN lSukses
  781. ENDPROC
  782. *..................... connect2ftp .........................................
  783. *...  Makes sure there is actually a valid connection to the host
  784. FUNCTION  connect2ftp (lcHost, lcUser, lcPassword)
  785.     * open access to Inet functions
  786.     hOpen = InternetOpen ("vfp", 1, 0, 0, 0)
  787.  
  788.     IF hOpen = 0
  789.         ? "Unable to get access to WinInet.Dll"
  790.         RETURN .F.
  791.     ENDIF
  792.  
  793.     *... The first '0' says use the default port, usually 21.
  794.     hftpSession = InternetConnect (hOpen, lcHost,;
  795.         0, lcUser, lcPassword, 1, 0, 0)   &&... 1 = ftp protocol
  796.  
  797.     IF hftpSession = 0
  798.         * close access to Inet functions and exit
  799.         = InternetCloseHandle (hOpen)
  800.         ? "ftp " + lcHost + " is not available"
  801.         RETURN .F.
  802.     ELSE
  803.         ? "Connected to " + lcHost
  804.     ENDIF
  805.     RETURN .T.
  806.     RETURN
  807.     *** End of ftpPut.PRG *************************************************************
  808.  
  809.  
  810.     *!* Now if you want to delete the remote file after downloading it,
  811.     *!* you can use FtpDelete.PRG:
  812.  
  813.     **********************************************************************************
  814.     *... FtpDelete.PRG ...*
  815.  
  816. PROCEDURE ftpDelete
  817.     PARAMETERS lcHost, lcUser, lcPwd, lcRemoteFile
  818.  
  819.     *.................................................................................
  820.     *:   Usage: DO ftpdelete WITH ;
  821.     *:         'ftpserver.host', 'name', 'password', 'delete.file'
  822.     *:
  823.     *:  Where:  lcHost       = Host computer IP address or name
  824.     *:          lcUser       = user name - anonymous may be used
  825.     *:          lcPwd        = password
  826.     *:          lcRemoteFile = file to delete
  827.     *.................................................................................
  828.  
  829.     *...set up API calls
  830.     PUBLIC hOpen, hftpSession
  831.     DECLARE INTEGER InternetOpen IN wininet.DLL;
  832.         STRING  sAgent,;
  833.         INTEGER lAccessType,;
  834.         STRING  sProxyName,;
  835.         STRING  sProxyBypass,;
  836.         STRING  lFlags
  837.  
  838.     DECLARE INTEGER InternetCloseHandle IN wininet.DLL INTEGER hInet
  839.  
  840.     DECLARE INTEGER InternetConnect IN wininet.DLL;
  841.         INTEGER hInternetSession,;
  842.         STRING  sServerName,;
  843.         INTEGER nServerPort,;
  844.         STRING  sUsername,;
  845.         STRING  sPassword,;
  846.         INTEGER lService,;
  847.         INTEGER lFlags,;
  848.         INTEGER lContext
  849.  
  850.     DECLARE INTEGER FtpDeleteFile IN wininet.DLL;
  851.         INTEGER hConnect,;
  852.         STRING  lpszFileName
  853.  
  854.     *... open access to Inet functions
  855.     hOpen = InternetOpen ("vfp", 1, 0, 0, 0)
  856.  
  857.     IF hOpen = 0
  858.         ? "Unable to get access to WinInet.Dll"
  859.         RETURN .F.
  860.     ENDIF
  861.  
  862.     *... connect to ftp host
  863.     hftpSession = InternetConnect (hOpen, lcHost, 0,;
  864.         lcUser, lcPwd, 1, 0, 0)
  865.  
  866.     IF hftpSession = 0
  867.         * close access to Inet functions and exit
  868.         = InternetCloseHandle (hOpen)
  869.         WAIT WINDOW "ftp " + strHost + " is not available"  TIMEOUT 2
  870.     ELSE
  871.         WAIT WINDOW "Connected to " + lcHost + " as: [" + lcUser + "]"  NOWAIT
  872.         IF FtpDeleteFile(hftpSession, lcRemoteFile) = 1
  873.             WAIT WINDOW lcRemoteFile + ' deleted.' TIMEOUT 1
  874.         ELSE
  875.             WAIT WINDOW 'Error deleting ' + lcRemoteFile + "." TIMEOUT 1
  876.         ENDIF
  877.     ENDIF
  878.     = InternetCloseHandle (hftpSession)
  879.     = InternetCloseHandle (hOpen)
  880.     *** End of ftpDelete.PRG *************************************************************
  881. ENDPR
  882. *..................... connect2ftp .........................................
  883. *...  Makes sure there is actually a valid connection to the host
  884. FUNCTION  connect2ftp (lcHost, lcUser, lcPassword)
  885.     * open access to Inet functions
  886.     hOpen = InternetOpen ("vfp", 1, 0, 0, 0)
  887.  
  888.     IF hOpen = 0
  889.         ? "Unable to get access to WinInet.Dll"
  890.         RETURN .F.
  891.     ENDIF
  892.  
  893.     *... The first '0' says use the default port, usually 21.
  894.     hftpSession = InternetConnect (hOpen, lcHost,;
  895.         0, lcUser, lcPassword, 1, 0, 0)   &&... 1 = ftp protocol
  896.  
  897.     IF hftpSession = 0
  898.         * close access to Inet functions and exit
  899.         = InternetCloseHandle (hOpen)
  900.         ? "ftp " + lcHost + " is not available"
  901.         RETURN .F.
  902.     ELSE
  903.         ? "Connected to " + lcHost
  904.     ENDIF
  905.     RETURN .T.
  906.     RETURN
  907.     *** End of ftpPut.PRG *************************************************************
  908.  
  909.  
  910.     *!* Now if you want to delete the remote file after downloading it,
  911.     *!* you can use FtpDelete.PRG:
  912.  
  913.     **********************************************************************************
  914.     *... FtpDelete.PRG ...*
  915.  
  916. PROCEDURE ftpDelete
  917.     PARAMETERS lcHost, lcUser, lcPwd, lcRemoteFile
  918.  
  919.     *.................................................................................
  920.     *:   Usage: DO ftpdelete WITH ;
  921.     *:         'ftpserver.host', 'name', 'password', 'delete.file'
  922.     *:
  923.     *:  Where:  lcHost       = Host computer IP address or name
  924.     *:          lcUser       = user name - anonymous may be used
  925.     *:          lcPwd        = password
  926.     *:          lcRemoteFile = file to delete
  927.     *.................................................................................
  928.  
  929.     *...set up API calls
  930.     PUBLIC hOpen, hftpSession
  931.     DECLARE INTEGER InternetOpen IN wininet.DLL;
  932.         STRING  sAgent,;
  933.         INTEGER lAccessType,;
  934.         STRING  sProxyName,;
  935.         STRING  sProxyBypass,;
  936.         STRING  lFlags
  937.  
  938.     DECLARE INTEGER InternetCloseHandle IN wininet.DLL INTEGER hInet
  939.  
  940.     DECLARE INTEGER InternetConnect IN wininet.DLL;
  941.         INTEGER hInternetSession,;
  942.         STRING  sServerName,;
  943.         INTEGER nServerPort,;
  944.         STRING  sUsername,;
  945.         STRING  sPassword,;
  946.         INTEGER lService,;
  947.         INTEGER lFlags,;
  948.         INTEGER lContext
  949.  
  950.     DECLARE INTEGER FtpDeleteFile IN wininet.DLL;
  951.         INTEGER hConnect,;
  952.         STRING  lpszFileName
  953.  
  954.     *... open access to Inet functions
  955.     hOpen = InternetOpen ("vfp", 1, 0, 0, 0)
  956.  
  957.     IF hOpen = 0
  958.         ? "Unable to get access to WinInet.Dll"
  959.         RETURN .F.
  960.     ENDIF
  961.  
  962.     *... connect to ftp host
  963.     hftpSession = InternetConnect (hOpen, lcHost, 0,;
  964.         lcUser, lcPwd, 1, 0, 0)
  965.  
  966.     IF hftpSession = 0
  967.         * close access to Inet functions and exit
  968.         = InternetCloseHandle (hOpen)
  969.         WAIT WINDOW "ftp " + strHost + " is not available"  TIMEOUT 2
  970.     ELSE
  971.         WAIT WINDOW "Connected to " + lcHost + " as: [" + lcUser + "]"  NOWAIT
  972.         IF FtpDeleteFile(hftpSession, lcRemoteFile) = 1
  973.             WAIT WINDOW lcRemoteFile + ' deleted.' TIMEOUT 1
  974.         ELSE
  975.             WAIT WINDOW 'Error deleting ' + lcRemoteFile + "." TIMEOUT 1
  976.         ENDIF
  977.     ENDIF
  978.     = InternetCloseHandle (hftpSession)
  979.     = InternetCloseHandle (hOpen)
  980.     *** End of ftpDelete.PRG *************************************************************
  981. ENDPR
  982. *...  Makes sure there is actually a valid connection to the host
  983. FUNCTION  connect2ftp (lcHost, lcUser, lcPassword)
  984.     * open access to Inet functions
  985.     hOpen = InternetOpen ("vfp", 1, 0, 0, 0)
  986.  
  987.     IF hOpen = 0
  988.         ? "Unable to get access to WinInet.Dll"
  989.         RETURN .F.
  990.     ENDIF
  991.  
  992.     *... The first '0' says use the default port, usually 21.
  993.     hftpSession = InternetConnect (hOpen, lcHost,;
  994.         0, lcUser, lcPassword, 1, 0, 0)   &&... 1 = ftp protocol
  995.  
  996.     IF hftpSession = 0
  997.         * close access to Inet functions and exit
  998.         = InternetCloseHandle (hOpen)
  999.         ? "ftp " + lcHost + " is not available"
  1000.         RETURN .F.
  1001.     ELSE
  1002.         ? "Connected to " + lcHost
  1003.     ENDIF
  1004.     RETURN .T.
  1005.     RETURN
  1006.     *** End of ftpPut.PRG *************************************************************
  1007.  
  1008.  
  1009.     *!* Now if you want to delete the remote file after downloading it,
  1010.     *!* you can use FtpDelete.PRG:
  1011.  
  1012.     **********************************************************************************
  1013.     *... FtpDelete.PRG ...*
  1014.  
  1015. PROCEDURE ftpDelete
  1016.     PARAMETERS lcHost, lcUser, lcPwd, lcRemoteFile
  1017.  
  1018.     *.................................................................................
  1019.     *:   Usage: DO ftpdelete WITH ;
  1020.     *:         'ftpserver.host', 'name', 'password', 'delete.file'
  1021.     *:
  1022.     *:  Where:  lcHost       = Host computer IP address or name
  1023.     *:          lcUser       = user name - anonymous may be used
  1024.     *:          lcPwd        = password
  1025.     *:          lcRemoteFile = file to delete
  1026.     *.................................................................................
  1027.  
  1028.     *...set up API calls
  1029.     PUBLIC hOpen, hftpSession
  1030.     DECLARE INTEGER InternetOpen IN wininet.DLL;
  1031.         STRING  sAgent,;
  1032.         INTEGER lAccessType,;
  1033.         STRING  sProxyName,;
  1034.         STRING  sProxyBypass,;
  1035.         STRING  lFlags
  1036.  
  1037.     DECLARE INTEGER InternetCloseHandle IN wininet.DLL INTEGER hInet
  1038.  
  1039.     DECLARE INTEGER InternetConnect IN wininet.DLL;
  1040.         INTEGER hInternetSession,;
  1041.         STRING  sServerName,;
  1042.         INTEGER nServerPort,;
  1043.         STRING  sUsername,;
  1044.         STRING  sPassword,;
  1045.         INTEGER lService,;
  1046.         INTEGER lFlags,;
  1047.         INTEGER lContext
  1048.  
  1049.     DECLARE INTEGER FtpDeleteFile IN wininet.DLL;
  1050.         INTEGER hConnect,;
  1051.         STRING  lpszFileName
  1052.  
  1053.     *... open access to Inet functions
  1054.     hOpen = InternetOpen ("vfp", 1, 0, 0, 0)
  1055.  
  1056.     IF hOpen = 0
  1057.         ? "Unable to get access to WinInet.Dll"
  1058.         RETURN .F.
  1059.     ENDIF
  1060.  
  1061.     *... connect to ftp host
  1062.     hftpSession = InternetConnect (hOpen, lcHost, 0,;
  1063.         lcUser, lcPwd, 1, 0, 0)
  1064.  
  1065.     IF hftpSession = 0
  1066.         * close access to Inet functions and exit
  1067.         = InternetCloseHandle (hOpen)
  1068.         WAIT WINDOW "ftp " + strHost + " is not available"  TIMEOUT 2
  1069.     ELSE
  1070.         WAIT WINDOW "Connected to " + lcHost + " as: [" + lcUser + "]"  NOWAIT
  1071.         IF FtpDeleteFile(hftpSession, lcRemoteFile) = 1
  1072.             WAIT WINDOW lcRemoteFile + ' deleted.' TIMEOUT 1
  1073.         ELSE
  1074.             WAIT WINDOW 'Error deleting ' + lcRemoteFile + "." TIMEOUT 1
  1075.         ENDIF
  1076.     ENDIF
  1077.     = InternetCloseHandle (hftpSession)
  1078.     = InternetCloseHandle (hOpen)
  1079.     *** End of ftpDelete.PRG *************************************************************
  1080. ENDPROC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement