-2

I've got a Python script that iterates over an FTP directory downloading any new files to a specific location based on the file name and what directory it was in. Because the FTP library I'm using is misbehaving in unexpected ways, I would like to move it to C# and merge it with a form application I have.

The problem is that I can't seem to find much on FTP in C# for what I need so I was hoping I could receive some help here. Below I have included my Python code as it can likely show what I want to do better than my words.

def work(ftp, extensions):
    if not ftp.path.exists('/downloads'):
        ftp.mkdir('/downloads')
    ftp.chdir('/downloads')
    os.chdir('Z:\\')
    r = re.compile(r'\./(.+?)/.+?(\d+?)/.+?E?(\d{2})\..+(\..+?$)')
    for root, dirs, files in ftp.walk('.'):
        for file in files:
            _, ext = os.path.splitext(file)
            if ext in extensions:
                    file_path = ftp.path.join(root, file)
                    date_stripped_file_path = ftp.path.join(root, re.sub(r'\.20[12]\d', '', file))
                    local_file_path = r.sub(r'./\1/\2/\1 S0\2E\3\4', date_stripped_file_path)
                    if download(ftp, file_path, local_file_path, 'TV'):
                        m = r.match(date_stripped_file_path)
                        insert_into_database(m.group(1), int(m.group(2)), int(m.group(3)), os.path.abspath(local_file_path))

def download(ftp, source, target, type):
    if (not os.path.isfile(target)) or \
        os.path.getsize(target) < ftp.path.getsize(source):
        file_name = os.path.splitext(os.path.basename(target))[0]
        print(f'{get_timestamp()}... Downloading: {file_name}')
        # Create dir if not exists
        dirname = os.path.dirname(target)
        if not (os.path.exists(dirname) and os.path.isdir(dirname)):
            os.makedirs(dirname)
        try:
            def display_progress(chunk):
                bar.update(len(chunk))

            bar = click.progressbar(length=ftp.path.getsize(source))
            ftp.download(source, target, callback=display_progress)
            print('')
        except Exception as e:
            print(f'{get_timestamp()}... Dowmload failed')
            log_error(e, True)
        else:
            ftp.remove(source)
            return True
    else:
        ftp.remove(source)
        return True

I have work in an indefinite while-loop that will run every 10 minutes. In short, the only thing I need to find in C# is an equivalent to for root, dirs, files in ftp.walk('.')

EDIT: I'd prefer to use the libraries that are included in .NET, however if that gets complicated, I'm open to using an external library.

Spedwards
  • 4,167
  • 16
  • 49
  • 106
  • which ftp set are you using with c#? – BugFinder Nov 02 '17 at 10:32
  • @BugFinder Not sure what you mean entirely. Never used ftp in C# before, but an example I saw for something else uses `System.Net.FtpWebRequest` and `System.Net.FtpWebResponse` if that's what you mean. – Spedwards Nov 02 '17 at 10:38
  • walk is not a standard ftp command - so you would need to make your own.. but the principal of walk (guessing its main function) is very quick and simple to replicate.. There are a bunch of examples of getting listings and downloading your files.. – BugFinder Nov 02 '17 at 11:11

1 Answers1

1

You can use the WebClient Class provided with the .NET Framework.

An example of file download via FTP can be found in the official MSDN page provided by Microsoft

https://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest(v=vs.110).aspx

  • Some issues. The example provided copies a text file. Not a media file which is what I want. Also, there's no equivalent to Python's `walk` that I can see. – Spedwards Nov 02 '17 at 11:00