question

Upvotes
Accepted
1 1 0 5

How to pull all files from ftp link

error-1
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
Accepted
45.2k 103 44 60

@Shalini.YPatted

I assumed that you would like to download all files and all folders from the FTP server.

In Python, you can use the ftplib library.

The code looks like this:

import ftplib
import os
local_path = []
ftp = ftplib.FTP("FTP Server")
ftp.login("username","password")
## Function ###
def downloadfiles(directory):
    global local_path
    print("Download files from ",directory)
    if directory!='.':
        local_path.append(directory)
    if(len(local_path) != 0): 
        joined_str = "\\".join(local_path)
        if os.path.exists(joined_str) == False:
            print("Create a local directory ", joined_str)
            os.makedirs(joined_str)
    ftp.cwd(directory)
    for name, facts in ftp.mlsd():
        if(facts["type"] == 'dir'):
            downloadfiles(name)
        if(facts["type"] == 'file'):
            print("Download a file ", name)
            ftp.retrbinary("RETR "+name, open(joined_str+"\\"+name, 'wb').write)
    if len(local_path) != 0:
        local_path.pop()
    if ftp.pwd() != '/':
        ftp.cwd("..")
## End Function ##
downloadfiles(".")
ftp.close()

This is just a sample code. Please test it thoroughly before using it in production.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

@Jirapongse ,

Thanks a lot:-)

Hi @Jirapongse ,

I want to try to pull all files from FTP via VBA.


1639744489035.png

1639744537907.png

Can you please help me with this error

1639744489035.png (57.7 KiB)
1639744537907.png (54.6 KiB)
Upvotes
1 1 0 5

Hi @Jirapongse ,

Thanks a lot for the above FTP command.

I tried connecting same with VBA.

1639713202341.png \1639713162441.png

But not able to rectify error, Can you please help me


1639713202341.png (19.4 KiB)
1639713162441.png (33.7 KiB)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
45.2k 103 44 60

@Shalini.YPatted

I can run it properly. You need to create a python file (.py), not a notebook file (.ipynb).

Sub RunPythonScript()


Dim objShell As Object
Dim PythonExePath, PythonScriptPath As String


Set objShell = VBA.CreateObject("Wscript.Shell")


PythonExePath = """C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\Python.exe"""
PythonScriptPath = """C:\D_DRIVE\hello.py"""


objShell.Run PythonExePath & PythonScriptPath


End Sub

1639716670183.png



1639716670183.png (47.2 KiB)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thanks a lot @Jirapongse :-)