locked
Moving Multiple excel spreadsheets to FTP RRS feed

  • Question

  • User-1320770751 posted

    Hi,

    I want to move multiple Excel spreadsheets from a folder on my server to an FTP site.

    I tried doing it with a .bat file - which works... but ONLY copies the last excel spreadsheet, and not all.

     

    Any help would be greatly appreciated.

    tnx 

    Thursday, September 3, 2009 6:03 AM

Answers

  • User-1320770751 posted

    Hi,

     

    Thanks for the reply.

    I got it sorted thanks. When I said Automatically - I meant for the code being placed in the Page_Load event.

    I used Chillkat. Code below :

     

    '  The mailman object is used for receiving (POP3)

            '  and sending (SMTP) email.

            Dim mailman As New Chilkat.MailMan()

     

            '  Any string argument automatically begins the 30-day trial.

            Dim success As Boolean

            success = mailman.UnlockComponent("")

            If (success <> True) Then

                Response.Write("Component unlock failed")

                Exit Sub

            End If

     

     

            '  Set the POP3 server's hostname

            mailman.MailHost = "xxx"

     

            '  Set the POP3 login/password.

            mailman.PopUsername = "xxx"

            mailman.PopPassword = "xxx"

     

            '  We're keeping a list of already-seen UIDLs in a text file:

            '  If this is the first time you're running this example,

            '  create an empty text file named "seenUidls.txt"

            Dim saSeenUidls As New Chilkat.StringArray()

            success = saSeenUidls.LoadFromFile(Server.MapPath("\FolderName\seenUidls.txt"))

            If (success <> True) Then

                Response.Write("failed to load seenUidls.txt - Check if File Exists")

                Exit Sub

            End If

     

     

            '  Get the complete list of UIDLs on the mail server.

            Dim saUidls As Chilkat.StringArray

            saUidls = mailman.GetUidls()

     

            If (saUidls Is Nothing) Then

                Response.Write(mailman.LastErrorText)

                Exit Sub

            End If

     

     

            '  We don't have set operators (yet) on the StringArray object,

            '  so create a new string array object (it's an object, not an actual array)

            '  and add the UIDLs from saUidls that aren't already seen.

            Dim saUnseenUidls As New Chilkat.StringArray()

     

            Dim i As Long

            Dim n As Long

            Dim emailA As Chilkat.Email

            Dim bundleA As Chilkat.EmailBundle

            Dim bundleA2 As New Chilkat.EmailBundle()

     

            'bundle = mailman.CopyMail()

            n = saUidls.Count

            For i = 0 To n - 1

                If (saSeenUidls.Contains(saUidls.GetString(i)) <> True) Then

                    saUnseenUidls.Append(saUidls.GetString(i))

                    bundleA = mailman.CopyMail()

                    If (Not (bundleA Is Nothing)) Then

                        Dim k As Long

                       

                        For k = 0 To bundleA.MessageCount - 1

                            emailA = bundleA.GetEmail(i)

                            emailA.SaveAllAttachments(Server.MapPath("\FolderName"))

                            emailA.DropAttachments()

                            ' Add the email (without attachments) to bundle2.

                            bundleA2.AddEmail(emailA)

                        Next

                    End If

                   

                   

                   

                End If

     

            Next

     

     

            If (saUnseenUidls.Count = 0) Then

                Response.Write("No unseen emails!")

                Exit Sub

            End If

     

     

            '  Download in full the unseen emails:

            Dim bundle As Chilkat.EmailBundle

     

            bundle = mailman.FetchMultiple(saUnseenUidls)

            If (bundle Is Nothing) Then

                Response.Write(mailman.LastErrorText)

     

                Exit Sub

            End If

     

            Dim email As Chilkat.Email

            For i = 0 To bundle.MessageCount - 1

                email = bundle.GetEmail(i)

                TextBox1.Text = TextBox1.Text & email.From & vbCrLf

                TextBox1.Text = ""

                TextBox1.Text = TextBox1.Text & email.Subject & vbCrLf

                TextBox1.Text = ""

                TextBox1.Text = TextBox1.Text & "----" & vbCrLf

                TextBox1.Text = ""

     

            Next

     

            '  We've seen all the emails, save saUidls to "seenUidls.txt"

            saUidls.SaveToFile(Server.MapPath("\Foldername\seenUidls.txt"))

     

    What this does is the following : Reads all the emails and get their UIDL's, and places them into a text file, so and saves all the attachments. the next time the application is run, it collects the UIDL's and if they match, it does not save the attachments again, but if they dont match, it saves those attachments and places those UIDL's in the same text file(this allows the application to see which emails have been "read" already and those which havnt)

     

    Hope this helps anyone else.

    Please Note that you need to download the Chilkat software

     

    I then used a Batch file to move the files from my folder on my server to my FTP site

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 8, 2009 4:45 AM

All replies

  • User-1696077569 posted

    Place all excel files in a folder and Try below code

     foreach (FileInfo file in Directory.GetFiles("fodlerpath"))
            {
    
    
                string FTPAddress = "";
                string filePath = "";
                FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" + Path.GetFileName(filePath));
    
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(username, password);
                request.UsePassive = true;
                request.UseBinary = true;
                request.KeepAlive = false;
    
                FileStream stream = file.OpenRead();
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length); stream.Close();
                Stream reqStream = request.GetRequestStream(); 
                reqStream.Write(buffer, 0, buffer.Length); 
                reqStream.Close();
            }


     

    Thursday, September 3, 2009 7:59 AM
  • User-1320770751 posted

    Thanks for the reply - but I got it sorted out.

     

    I used a batch file which pulled the files from the folder on my server to the FTP site.

    What I would also like to find out - Is it possible for an ASP.net application to be opened up scheduely, my code run(in the page load) and then closed again ? 

    Thursday, September 3, 2009 9:59 AM
  • User-1267218547 posted

     Hi AxleWack,

    Based on my experience, that's impossible for opening the page to process the work automatically.

    By the way, I think you can use the Windows Services for moving your content.
    http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx

    I hope it is helpful to you.

    Tuesday, September 8, 2009 4:22 AM
  • User-1320770751 posted

    Hi,

     

    Thanks for the reply.

    I got it sorted thanks. When I said Automatically - I meant for the code being placed in the Page_Load event.

    I used Chillkat. Code below :

     

    '  The mailman object is used for receiving (POP3)

            '  and sending (SMTP) email.

            Dim mailman As New Chilkat.MailMan()

     

            '  Any string argument automatically begins the 30-day trial.

            Dim success As Boolean

            success = mailman.UnlockComponent("")

            If (success <> True) Then

                Response.Write("Component unlock failed")

                Exit Sub

            End If

     

     

            '  Set the POP3 server's hostname

            mailman.MailHost = "xxx"

     

            '  Set the POP3 login/password.

            mailman.PopUsername = "xxx"

            mailman.PopPassword = "xxx"

     

            '  We're keeping a list of already-seen UIDLs in a text file:

            '  If this is the first time you're running this example,

            '  create an empty text file named "seenUidls.txt"

            Dim saSeenUidls As New Chilkat.StringArray()

            success = saSeenUidls.LoadFromFile(Server.MapPath("\FolderName\seenUidls.txt"))

            If (success <> True) Then

                Response.Write("failed to load seenUidls.txt - Check if File Exists")

                Exit Sub

            End If

     

     

            '  Get the complete list of UIDLs on the mail server.

            Dim saUidls As Chilkat.StringArray

            saUidls = mailman.GetUidls()

     

            If (saUidls Is Nothing) Then

                Response.Write(mailman.LastErrorText)

                Exit Sub

            End If

     

     

            '  We don't have set operators (yet) on the StringArray object,

            '  so create a new string array object (it's an object, not an actual array)

            '  and add the UIDLs from saUidls that aren't already seen.

            Dim saUnseenUidls As New Chilkat.StringArray()

     

            Dim i As Long

            Dim n As Long

            Dim emailA As Chilkat.Email

            Dim bundleA As Chilkat.EmailBundle

            Dim bundleA2 As New Chilkat.EmailBundle()

     

            'bundle = mailman.CopyMail()

            n = saUidls.Count

            For i = 0 To n - 1

                If (saSeenUidls.Contains(saUidls.GetString(i)) <> True) Then

                    saUnseenUidls.Append(saUidls.GetString(i))

                    bundleA = mailman.CopyMail()

                    If (Not (bundleA Is Nothing)) Then

                        Dim k As Long

                       

                        For k = 0 To bundleA.MessageCount - 1

                            emailA = bundleA.GetEmail(i)

                            emailA.SaveAllAttachments(Server.MapPath("\FolderName"))

                            emailA.DropAttachments()

                            ' Add the email (without attachments) to bundle2.

                            bundleA2.AddEmail(emailA)

                        Next

                    End If

                   

                   

                   

                End If

     

            Next

     

     

            If (saUnseenUidls.Count = 0) Then

                Response.Write("No unseen emails!")

                Exit Sub

            End If

     

     

            '  Download in full the unseen emails:

            Dim bundle As Chilkat.EmailBundle

     

            bundle = mailman.FetchMultiple(saUnseenUidls)

            If (bundle Is Nothing) Then

                Response.Write(mailman.LastErrorText)

     

                Exit Sub

            End If

     

            Dim email As Chilkat.Email

            For i = 0 To bundle.MessageCount - 1

                email = bundle.GetEmail(i)

                TextBox1.Text = TextBox1.Text & email.From & vbCrLf

                TextBox1.Text = ""

                TextBox1.Text = TextBox1.Text & email.Subject & vbCrLf

                TextBox1.Text = ""

                TextBox1.Text = TextBox1.Text & "----" & vbCrLf

                TextBox1.Text = ""

     

            Next

     

            '  We've seen all the emails, save saUidls to "seenUidls.txt"

            saUidls.SaveToFile(Server.MapPath("\Foldername\seenUidls.txt"))

     

    What this does is the following : Reads all the emails and get their UIDL's, and places them into a text file, so and saves all the attachments. the next time the application is run, it collects the UIDL's and if they match, it does not save the attachments again, but if they dont match, it saves those attachments and places those UIDL's in the same text file(this allows the application to see which emails have been "read" already and those which havnt)

     

    Hope this helps anyone else.

    Please Note that you need to download the Chilkat software

     

    I then used a Batch file to move the files from my folder on my server to my FTP site

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 8, 2009 4:45 AM