Getting file list on ftp server

Cem_Karaer
Member Posts: 281
Hello, I want to get a list of files on FTP server giving a file pattern. For example I want to get a list of files matching following pattern: /matmaster/MATMASTER_*.CSV
Why do I want this? Because it is not allowed for us to make any file movement on the remote server. So we have to check none-downloaded files on the FTP and download if exists.
Is there any dotnet solution for this?
Why do I want this? Because it is not allowed for us to make any file movement on the remote server. So we have to check none-downloaded files on the FTP and download if exists.
Is there any dotnet solution for this?
Cem Karaer @ Pargesoft
Dynamics NAV Developer since 2005
Dynamics NAV Developer since 2005
0
Best Answer
-
Here's an example function I use to retrieve a list of files on a remote FTP site. The files are loaded to a temp table which can then be processed.
Hope this helps.
"FtpWebReques" is System.Net.FtpWebRequest.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
FtpWebResponse is System.Net.FtpWebResponse.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
--
LOCAL ListFTPFiles(SiteAddress : Text[250];Username : Text[30];PWD : Text[30];Path : Text)
FileCount := 0;
FilesToGet.RESET;
FilesToGet.DELETEALL;
FtpWebRequest := FtpWebRequest.Create('ftp://' + SiteAddress + '/' + Path);
FtpWebRequest.Credentials := NetworkCredential.NetworkCredential(Username, PWD);
FtpWebRequest.Method := 'NLST';
FtpWebRequest.KeepAlive := TRUE;
FtpWebRequest.UseBinary := TRUE;
FtpWebResponse := FtpWebRequest.GetResponse;
Stream := FtpWebResponse.GetResponseStream;
StreamReader := StreamReader.StreamReader(Stream);
WHILE NOT (StreamReader.EndOfStream) DO BEGIN
FileCount := FileCount + 1;
FilesToGet.INIT;
FilesToGet."Is a file" := TRUE;
FilesToGet.Name := StreamReader.ReadLine;
FilesToGet.INSERT;
END;
StreamReader.Close;
Stream.Close;
--There are no bugs - only undocumented features.5
Answers
-
Here's an example function I use to retrieve a list of files on a remote FTP site. The files are loaded to a temp table which can then be processed.
Hope this helps.
"FtpWebReques" is System.Net.FtpWebRequest.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
FtpWebResponse is System.Net.FtpWebResponse.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
--
LOCAL ListFTPFiles(SiteAddress : Text[250];Username : Text[30];PWD : Text[30];Path : Text)
FileCount := 0;
FilesToGet.RESET;
FilesToGet.DELETEALL;
FtpWebRequest := FtpWebRequest.Create('ftp://' + SiteAddress + '/' + Path);
FtpWebRequest.Credentials := NetworkCredential.NetworkCredential(Username, PWD);
FtpWebRequest.Method := 'NLST';
FtpWebRequest.KeepAlive := TRUE;
FtpWebRequest.UseBinary := TRUE;
FtpWebResponse := FtpWebRequest.GetResponse;
Stream := FtpWebResponse.GetResponseStream;
StreamReader := StreamReader.StreamReader(Stream);
WHILE NOT (StreamReader.EndOfStream) DO BEGIN
FileCount := FileCount + 1;
FilesToGet.INIT;
FilesToGet."Is a file" := TRUE;
FilesToGet.Name := StreamReader.ReadLine;
FilesToGet.INSERT;
END;
StreamReader.Close;
Stream.Close;
--There are no bugs - only undocumented features.5 -
@bbrown, thanks a lot. It seems good. I will try and post the result.Cem Karaer @ Pargesoft
Dynamics NAV Developer since 20050 -
Nice approach really, here is a link listing available options for FtpWebRequest.Method
https://msdn.microsoft.com/en-us/library/system.net.webrequestmethods.ftp_fields(v=vs.110).aspxCem Karaer @ Pargesoft
Dynamics NAV Developer since 20050 -
https://en.wikipedia.org/wiki/List_of_FTP_commands
This provides a listing of the 4 character commands needed for the above process. Not all of the commands are supported. That, I think is just trial and error. I'm thinking only the RFC 959 commands are supported, but can't say for sure.
There are no bugs - only undocumented features.0 -
Also note that I am using the assembly "System.Net.FtpWebRequest" and not "System.Net.WebRequestMethods".There are no bugs - only undocumented features.0
-
Hello, FtpWebRequest is derived from WebRequest and I think Method is not overriden in FtpWebRequest so the link is revelant actually.Cem Karaer @ Pargesoft
Dynamics NAV Developer since 20050
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions