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();
}
}
}
}

  • Martin Dráb Profile Picture
    232,896 Most Valuable Professional on at
    RE: how to download only specific file from ftp server

    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.

  • Community Member Profile Picture
    on at
    RE: how to download only specific file from ftp server

    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

  • Suggested answer
    Martin Dráb Profile Picture
    232,896 Most Valuable Professional on at
    RE: how to download only specific file from ftp server
    [deleted]
  • Suggested answer
    Martin Dráb Profile Picture
    232,896 Most Valuable Professional on at
    RE: how to download only specific file from ftp server

    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.

  • Community Member Profile Picture
    on at
    RE: how to download only specific file from ftp server

    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));
    }
    }
    }
    
    
    }
    

  • Martin Dráb Profile Picture
    232,896 Most Valuable Professional on at
    RE: how to download only specific file from ftp server

    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.

  • ergun sahin Profile Picture
    8,816 Moderator on at
    RE: how to download only specific file from ftp server

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

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,145 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,896 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans