{"id":1248,"date":"2017-05-18T07:48:48","date_gmt":"2017-05-18T13:48:48","guid":{"rendered":"http:\/\/agiltools.com\/blogsp\/?p=1248"},"modified":"2017-05-18T18:56:15","modified_gmt":"2017-05-19T00:56:15","slug":"macro-ftp","status":"publish","type":"post","link":"https:\/\/agiltools.com\/blogsp\/macro-ftp\/","title":{"rendered":"Macro para transferir archivos a un sitio FTP"},"content":{"rendered":"<div id=\"dslc-theme-content\"><div id=\"dslc-theme-content-inner\"><p>Recientemente tuve un caso donde luego de realizar unos c\u00e1lulos automatizados en Excel, el resultado deb\u00eda transferirse a un sitio FTP. \u00a0El desaf\u00edo entonces es si es posible crear una manera automatizada de transferir estos archivos al sitio remoto.<\/p>\n<p>Encontr\u00e9 varios recursos en l\u00ednea y gracias a ellos desarroll\u00e9 la siguiente soluci\u00f3n<\/p>\n<pre class=\"lang:vb decode:true\" title=\"publish\">Sub UploadFile()\r\nDim strDirectoryList As String\r\nDim lStr_Dir As String\r\nDim lInt_FreeFile01 As Integer\r\nDim lInt_FreeFile02 As Integer\r\nDim nn As String\r\n\r\nOn Error GoTo Err_Handler\r\n    lStr_Dir = ThisWorkbook.Path\r\n    lInt_FreeFile01 = FreeFile\r\n    lInt_FreeFile02 = FreeFile\r\n    nn = \"ftpcom\"\r\n    strDirectoryList = lStr_Dir &amp; \"\\\" &amp; nn\r\n\r\n    '' Borrar archivo de cierre\r\n    If Dir(strDirectoryList &amp; \".out\") &lt;&gt; \"\" Then Kill (strDirectoryList &amp; \".out\")\r\n\r\n    '' Crear archivo de texto con instrucciones FTP\r\n    Open strDirectoryList &amp; \".txt\" For Output As #lInt_FreeFile01\r\n    Print #lInt_FreeFile01, \"!REM upload files\"\r\n    Print #lInt_FreeFile01, \"open ftp.agiltools.com\"\r\n    Print #lInt_FreeFile01, \"USER suusuarioaqui supasswordaqui\"\r\n    Print #lInt_FreeFile01, \"binary\"\r\n    Print #lInt_FreeFile01, \"!REM turn off interactive mode\"\r\n    Print #lInt_FreeFile01, \"prompt\"\r\n    Print #lInt_FreeFile01, \"mput \"\"\" &amp; ThisWorkbook.Path &amp; \"\\project.XML\"\"\"\r\n    Print #lInt_FreeFile01, \"bye\"\r\n    Close #lInt_FreeFile01\r\n\r\n    '' Crear archivo Batch\r\n    Open strDirectoryList &amp; \".bat\" For Output As #lInt_FreeFile02\r\n    Print #lInt_FreeFile02, \"ftp -n -s:\"\"\" &amp; strDirectoryList &amp; \".txt\"\"\"\r\n\r\n    Print #lInt_FreeFile02, \"Echo \"\"Complete\"\" &gt; \"\"\" &amp; strDirectoryList &amp; \".out\"\"\"\r\n    Close #lInt_FreeFile02\r\n\r\n    PathCrnt = ActiveWorkbook.Path\r\n    Shell (\"\"\"\" &amp; PathCrnt &amp; \"\\\" &amp; nn &amp; \".bat\"\"\")\r\n    Application.Wait (Now + TimeValue(\"0:00:03\"))\r\n\r\n    '' Clean up files\r\n    If Dir(strDirectoryList &amp; \".bat\") &lt;&gt; \"\" Then Kill (strDirectoryList &amp; \".bat\")\r\n    If Dir(strDirectoryList &amp; \".out\") &lt;&gt; \"\" Then Kill (strDirectoryList &amp; \".out\")\r\n    If Dir(strDirectoryList &amp; \".txt\") &lt;&gt; \"\" Then Kill (strDirectoryList &amp; \".txt\")\r\n\r\nbye:\r\nExit Sub\r\n\r\nErr_Handler:\r\n    MsgBox \"Error : \" &amp; Err.Number &amp; vbCrLf &amp; \"Description : \" &amp; Err.Description, vbCritical\r\n    Resume bye\r\n\r\nEnd Sub<\/pre>\n<p>En s\u00edntesis es una macro que crea un archivo de texto con las instrucciones en sintaxis FTP, luego crea un archivo\u00a0<a href=\"https:\/\/es.wikipedia.org\/wiki\/Archivo_batch\">Batch <\/a>que transfiere instrucciones MS-DOS siguiendo cada una de las \u00a0l\u00edneas del archivo de texto. Al finalizar, crea un archivo con extensi\u00f3n <strong>.out<\/strong> donde se confirma la transferencia.<\/p>\n<p>N\u00f3tese que es necesario cambiar el c\u00f3digo para agregar el usuario y password correcto. En ocasiones el sitio FTP no requiere password pues la conexi\u00f3n es an\u00f3nima. Si este es el caso, simplemente remueva esas dos l\u00edneas del c\u00f3digo.<\/p>\n<p>El siguiente video muestra el programa en ejecuci\u00f3n.<\/p>\n<p style=\"text-align: center;\">\n<iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/nG8iSbDzAL8\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-1248\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/agiltools.com\/blogsp\/macro-ftp\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-1248\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/agiltools.com\/blogsp\/macro-ftp\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Recientemente tuve un caso donde luego de realizar unos c\u00e1lulos automatizados en Excel, el resultado deb\u00eda transferirse a un sitio FTP. \u00a0El desaf\u00edo entonces es si es posible crear una manera automatizada de transferir estos archivos al sitio remoto. Encontr\u00e9 varios recursos en l\u00ednea y gracias a ellos desarroll\u00e9 la siguiente soluci\u00f3n Sub UploadFile() Dim [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-1248\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/agiltools.com\/blogsp\/macro-ftp\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-1248\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/agiltools.com\/blogsp\/macro-ftp\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"spay_email":""},"categories":[21],"tags":[112,185,22],"jetpack_featured_media_url":"","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/agiltools.com\/blogsp\/wp-json\/wp\/v2\/posts\/1248"}],"collection":[{"href":"https:\/\/agiltools.com\/blogsp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/agiltools.com\/blogsp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/agiltools.com\/blogsp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/agiltools.com\/blogsp\/wp-json\/wp\/v2\/comments?post=1248"}],"version-history":[{"count":5,"href":"https:\/\/agiltools.com\/blogsp\/wp-json\/wp\/v2\/posts\/1248\/revisions"}],"predecessor-version":[{"id":1435,"href":"https:\/\/agiltools.com\/blogsp\/wp-json\/wp\/v2\/posts\/1248\/revisions\/1435"}],"wp:attachment":[{"href":"https:\/\/agiltools.com\/blogsp\/wp-json\/wp\/v2\/media?parent=1248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agiltools.com\/blogsp\/wp-json\/wp\/v2\/categories?post=1248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agiltools.com\/blogsp\/wp-json\/wp\/v2\/tags?post=1248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}