web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

how to download only specific file from ftp server

(0) ShareShare
ReportReport
Posted on by

i am downloading file from ftp server to local folder and after that i am reading those file from the folder to ax table

the problem is i want to filter some file on the basis of file extension while downloading 

i am using the code to download file from ftp server.

 

private void ftpDownloadAllFiles()
{
str fileNameType;

FilePath filePathDest;
;
saveToFilePath= local file path;
fileNameType=".R102";
ftpHostName= "ftp path";
// info("weqd");
this.getFTPDirFilesList();
//info("weq1");
ftpFilesListEnum = ftpFilesList.getEnumerator();
ftpFilesListEnum.reset();

while (ftpFilesListEnum.moveNext())
{
fileNameType = ftpFilesListEnum.current();
//info("weq");
// Marshaling .NET to X++
ftpObject = System.Net.WebRequest::Create(ftpHostName + @"/" + fileNameType);
ftpWebRequest = ftpObject;

if (ftpWebRequest)
{
ftpWebRequest.set_KeepAlive(false);
ftpWebRequest.set_UsePassive(true);
ftpWebRequest.set_UseBinary(true);
//ftpWebRequest.set_Timeout(ftpTimeOut);
ftpWebRequest.set_Method(#DownloadFile);
//ftpWebRequest.set_ReadWriteTimeout(ftpTimeOut);
this.setFTPCredentials();

ftpWebResponse = ftpWebRequest.GetResponse();
//info("weqq4");
// BP Deviation Documented
ioStreamReader = new System.IO.StreamReader(ftpWebResponse.GetResponseStream());

if (ioStreamReader)
{
strReadLine = ioStreamReader.ReadToEnd();

if (strReadLine)
{
filePathDest = saveToFilePath + @"\" + fileNameType;
this.writeFile(filePathDest);

info(strfmt("Downloaded file %1 to %2", fileNameType, saveToFilePath));
}

ioStreamReader.Close();
}
}
}
}

I have the same question (0)
  • ergun sahin Profile Picture
    8,826 Moderator on at

    I don't fully understand, is the code above working?  what's inside the fileNameType object (in loop).

  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    Please always use Insert > Insert Code (in the rich-formatting view) to paste source code. It preserves line indentation, which makes code much easier to read:

    private void ftpDownloadAllFiles()
    {
    	str fileNameType;
    	FilePath filePathDest;
    	;
    	saveToFilePath= local file path;
    	fileNameType=".R102";
    	ftpHostName= "ftp path";
    	this.getFTPDirFilesList();
    	ftpFilesListEnum = ftpFilesList.getEnumerator();
    	ftpFilesListEnum.reset();
    
    	while (ftpFilesListEnum.moveNext())
    	{
    		fileNameType = ftpFilesListEnum.current();
    		//info("weq");
    		// Marshaling .NET to X  
    		ftpObject = System.Net.WebRequest::Create(ftpHostName   @"/"   fileNameType);
    		ftpWebRequest = ftpObject;
    
    		if (ftpWebRequest)
    		{
    			ftpWebRequest.set_KeepAlive(false);
    			ftpWebRequest.set_UsePassive(true);
    			ftpWebRequest.set_UseBinary(true);
    			ftpWebRequest.set_Method(#DownloadFile);
    			this.setFTPCredentials();
    
    			ftpWebResponse = ftpWebRequest.GetResponse();
    			// BP Deviation Documented
    			ioStreamReader = new System.IO.StreamReader(ftpWebResponse.GetResponseStream());
    
    			if (ioStreamReader)
    			{
    				strReadLine = ioStreamReader.ReadToEnd();
    
    				if (strReadLine)
    				{
    					filePathDest = saveToFilePath   @"\"   fileNameType;
    					this.writeFile(filePathDest);
    
    					info(strfmt("Downloaded file %1 to %2", fileNameType, saveToFilePath));
    				}
    
    				ioStreamReader.Close();
    			}
    		}
    	}
    }

    Nevertheless your description suggests that you want to change the logic of getFTPDirFilesList(), not the method you've shown above.

  • Community Member Profile Picture
    on at

    private void getFTPDirFilesList()
    {
    container conFTPFilesDownload;
    ListIterator ftpFilesListIterator;
     ftpHostName="ftppath";
        
        ftpObject = System.Net.WebRequest::Create(new System.Uri(ftpHostName));
    
    ftpWebRequest = ftpObject;
    
    if (ftpWebRequest)
    {
    ftpWebRequest.set_KeepAlive(true);
    ftpWebRequest.set_UsePassive(true);
    ftpWebRequest.set_UseBinary(true);
      ftpWebRequest.set_Method(#ListDirectory);
    
    this.setFTPCredentials();
    
        try
    {
        ftpWebResponse = ftpWebRequest.GetResponse();
    }
    catch (Exception::CLRError)
    {
        throw error(AifUtil::getClrErrorMessage());
    }
    if (ftpWebResponse)
    {
    ftpFilesList = new list(Types::String);
    
    // BP Deviation Documented
    ioStreamReader = new System.IO.StreamReader(ftpWebResponse.GetResponseStream());
    
    if (ioStreamReader)
    {
    strReadLine = ioStreamReader.ReadLine();
    
    while (!System.String::IsNullOrEmpty(strReadLine))
    {
    ftpFilesListIterator = new Listiterator(strsplit(strReadLine, '/'));
    
    while (ftpFilesListIterator.more())
    {
    conFTPFilesDownload  = ftpFilesListIterator.value();
    ftpFilesListIterator.next();
    }
    
    ftpFilesList.addEnd(conPeek(conFTPFilesDownload, conlen(conFTPFilesDownload)));
    strReadLine = ioStreamReader.ReadLine();
    }
    
    ioStreamReader.Close();
    }
    
    if (ftpFilesList.empty())
    {
    warning (strfmt("No files available in %1", ftpHostName));
    }
    }
    }
    
    
    }
    

  • Suggested answer
    Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    Unfortunately your code indentation is still weird in getFTPDirFilesList(). Could you fix it by yourself this time, please? Please pay more attention to it - if you make it difficult for us to help you, you decrease your chances to get an answer.

    Regarding your code, it seems to me that all you need to do is adding a condition for adding file names to getFTPDirFilesList. You will skip the file if its extension isn't .R001.

    You can utilize fileNameSplit() method, for example, to get the file extension.

  • Suggested answer
    Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at
    [deleted]
  • Community Member Profile Picture
    on at

    I am using code available at this url

    ax.com/.../

    can you suggest me code or changes by which i can get the correct result.

    i had tried to filter the file with several changes, didn't got any result

  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    I mean using the "if' statement to add the file name to the list only if it meets certain conditions.

    Instead of this:

    ftpFilesList.addEnd(...); // Add every single file

    you would do this:

    if (fileExtension == requiredFileExtension)
    {
    	ftpFilesList.addEnd(...); // Add only selected file names
    }

    And as I mentioned, you can use fileNameSplit() to get the file extension from a file name.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans