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.