{"id":30010,"date":"2023-03-21T21:11:19","date_gmt":"2023-03-21T20:11:19","guid":{"rendered":"https:\/\/help.qualiware.it\/qw-help\/?p=30010"},"modified":"2023-03-21T21:12:18","modified_gmt":"2023-03-21T20:12:18","slug":"lettura-ed-elaborazione-file-da-cartella-ftp","status":"publish","type":"post","link":"https:\/\/help.qualiware.it\/qw-help\/lettura-ed-elaborazione-file-da-cartella-ftp\/","title":{"rendered":"Lettura ed elaborazione file da cartella FTP"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">' Il seguente script \u00e8 un esempio di come si possono elaborare i file (Excel in questo caso) contenuti in una cartella FTP, con cancellazione dopo l'elaborazione\r\n\r\ndim ftpuser as string=&lt;utente&gt;\r\ndim ftppw as string=&lt;password&gt;\r\ndim folder as string=\"ftp:\/\/xx.xx.xx.xx\/cartella\"\r\n\r\n' Legge la directory\r\ndim ftpRequest as System.Net.FtpWebRequest = System.Net.WebRequest.Create(folder)\r\nftpRequest.Credentials =new System.Net.NetworkCredential(ftpuser,ftppw)\r\nftpRequest.Method = System.Net.WebRequestMethods.Ftp.ListDirectory\r\nDim response as System.Net.FtpWebResponse = ftpRequest.GetResponse()\r\ndim streamReader as System.Io.StreamReader = new System.Io.StreamReader(response.GetResponseStream())\r\n\r\n' scarica l'elenco dei file\r\ndim files as new DBArray()\r\n\r\ndim fn as string = streamReader.ReadLine()\r\nwhile not empty(fn)\r\n    files.add(fn)\r\n    fn = streamReader.ReadLine()\r\nEnd While\r\n\r\nstreamReader.Close()\r\n\r\n' esamina tutti i file\r\ndim i,j as integer\r\nfor i=1 to files.size\r\n    if lower(system.Io.Path.getextension(files(i)))=\".xlsx\"\r\n        ' \u00e8 un file Excel: lo scarica\r\n        dim FTP as System.Net.WebClient\r\n        FTP = new System.Net.WebClient\r\n        FTP.Credentials = new System.Net.NetworkCredential (ftpuser,ftppw)\r\n        Dim fileData As Byte() = FTP.DownloadData(folder+\"\/\"+files(i))\r\n        \r\n        fn=form.Getsession().GetLocalTempFileName(\".xlsx\")\r\n        Dim f As System.io.FileStream = System.IO.File.Create(fn)\r\n        f.Write(fileData, 0, fileData.Length)\r\n        f.Close()\r\n\r\n        Dim x as New qwexcel()\r\n        x.Open(fn)\r\n        x.SetActiveWorksheet(x.GetWorkbook.Worksheets(0).Name)\r\n        For j = 2 To 100000\r\n            dim idGestionale as string=x.ReadString(j,16)\r\n            dim esito as string=x.ReadString(j,6)\r\n            dim errore as string=x.ReadString(j,7)\r\n\r\n            if not empty(idGestionale)\r\n                form.FindControl(\"MultilineTextField1\").value+=idGestionale+\": \"+esito+if(not empty(errore),\" \"+errore,\"\")+vbCr\r\n            Else\r\n                Exit For\r\n            End If\r\n        Next\r\n        x.Close()\r\n\r\n        ' elimina il file\r\n        ftpRequest = System.Net.WebRequest.Create(folder+\"\/\"+files(i))\r\n        ftpRequest.Method = System.Net.WebRequestMethods.Ftp.DeleteFile\r\n        ftpRequest.Credentials = new System.Net.NetworkCredential (ftpuser,ftppw)\r\n        response = ftpRequest.GetResponse()\r\n        response.Close()\r\n    End If\r\nnext\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8216; Il seguente script \u00e8 un esempio di come si possono elaborare i file (Excel in questo caso) contenuti in una cartella FTP, con cancellazione dopo l&#8217;elaborazione dim ftpuser as string=&lt;utente&gt; dim ftppw as string=&lt;password&gt; dim folder as string=&#8221;ftp:\/\/xx.xx.xx.xx\/cartella&#8221; &#8216; Legge la directory dim ftpRequest as System.Net.FtpWebRequest = System.Net.WebRequest.Create(folder) ftpRequest.Credentials =new System.Net.NetworkCredential(ftpuser,ftppw) ftpRequest.Method = System.Net.WebRequestMethods.Ftp.ListDirectory&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"ngg_post_thumbnail":0,"footnotes":""},"categories":[65],"tags":[71,82],"acf":[],"_links":{"self":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/30010"}],"collection":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/comments?post=30010"}],"version-history":[{"count":2,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/30010\/revisions"}],"predecessor-version":[{"id":30012,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/30010\/revisions\/30012"}],"wp:attachment":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/media?parent=30010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/categories?post=30010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/tags?post=30010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}