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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Finance forum / Copy file in ftp from ...
Finance forum

Copy file in ftp from folder to another folder X++

(0) ShareShare
ReportReport
Posted on by 403

Hi All,

I nee to copy all file in folder INP to folder Archive in ftp by X++. I only found out when using the 'RENAME' method but only moved the file. How can I move the file from INP to Archive but the file in INP still exists?

This is my code

private str rename(filename fileNameString)
{
System.Net.FtpWebRequest request;
System.Net.NetworkCredential credential;
System.Net.FtpWebResponse response;
Filename filepath, filename;
Filename fileType, resultfile;
System.IO.StreamReader reader;
System.Byte[] bytes;
System.Text.Encoding utf8;

try
{
KREFTPParameter FTPParameter = KREFTPParameter::find();
KREDataEntityExportLogHeader entDest = KREDataEntityExportLogHeader::find();

str OriftpUrlarc = FTPParameter.servername + entDest.FTPINP + fileNameString;
credential = new System.Net.NetworkCredential("","","");
credential.username = FTPParameter.UserName;
credential.password = FTPParameter.passwordEdit(false,"");

request = System.Net.WebRequest::Create(OriftpUrlarc);
request.set_Credentials(credential);
request.set_Method("RENAME");
request.set_RenameTo(entDest.FTPArchive + fileNameString);
response = request.GetResponse();

resultfile = FTPParameter.servername + entDest.FTPArchive + fileNameString;
}
catch(Exception::CLRError)
{
throw error(AifUtil::getClrErrorMessage());
}

return resultfile;
}

Thanks,

  • Martin Dráb Profile Picture
    241,499 Most Valuable Professional on at

    This is a .NET API, not an AX API, therefore you should look at .NET resources, such as StackOverflow: How to copy a file on an FTP server?.

  • Suggested answer
    Rudi Hansen Profile Picture
    4,079 on at

    Just a quick guess.

    Change request.set_Method("RENAME");

    to request.set_Method("COPY");

    I would expect that RENAME is the same as MOVE, but I think that what you want is to COPY

  • Ferdiansah Profile Picture
    403 on at

    Hi Rudi,

    I tried it before but got an error like this "This method is not supported.

    Parameter name: value"

    In this link there is no copy method

    docs.microsoft.com/.../system.net.webrequestmethods.ftp

  • Rudi Hansen Profile Picture
    4,079 on at

    Worth a try :-)

    With a little more Goggling it does seem the webrequest method does not support copying of files on an ftp server.

    stackoverflow.com/.../how-to-copy-a-file-on-an-ftp-server

    This is the best I could find using webrequest, with is downloading and uploading the file, which just seems wrong to me.

    But apparently the ftp protocol does not seem to support copying of files.

  • Suggested answer
    Sukrut Parab Profile Picture
    71,741 Moderator on at

    You could also write a C# library   which does  these things( download, copy, List files, send file to FTP) for you and call  that in FnO.  Refer below link for the reference.

    ourcodeworld.com/.../how-to-access-a-sftp-server-using-ssh-net-sync-and-async-with-c-in-winforms

    github.com/.../SSH.NET

  • Blue Wang Profile Picture
    Microsoft Employee on at

    Hi  Ferdiansah,

    Please check if it's useful to you.

    [View:https://stoneridgesoftware.com/sending-flat-files-from-dynamics-365-for-operations-to-a-different-location/:750:50]

  • Suggested answer
    Ferdiansah Profile Picture
    403 on at

    I am done. I have to download the first file from INP then I upload it to Archive. Thank you to all of you.

    This my code 

    private str copyFile(filename fileNameString)
    {
    System.Object ftpResponseINP, ftpResponse;
    System.IO.StreamReader readerINP, reader;
    System.IO.Stream requestStreamINP, requestStream;
    System.Byte[] bytes;
    System.Text.Encoding utf8;
    System.Net.FtpWebResponse responseINP, response;
    int fileCount;
    System.Array files;
    int i;
    Filename filepath;
    //FileNameType pattern = '*.txt';
    FileName ftpINP, ftpFileName,fileName,resultfile;
    Object ftpo;
    System.Net.FtpWebRequest requestINP, request;
    System.Net.NetworkCredential credential;

    new InteropPermission(InteropKind::ClrInterop).assert();
    try
    {
    KREFTPParameter ftpParm = KREFTPParameter::find();
    KREDataEntityExportLogHeader entDest = KREDataEntityExportLogHeader::find();

    ftpINP = ftpParm.ServerName + entDest.FTPINP + fileNameString;
    ftpFileName = ftpParm.ServerName + entDest.FTPArchive + fileNameString;

    requestINP = System.Net.WebRequest::Create(ftpINP);
    credential = new System.Net.NetworkCredential("","","");
    credential.username = ftpParm.UserName;
    credential.password = ftpParm.passwordEdit(false,"");

    requestINP.set_Credentials(credential);
    requestINP.set_Method('RETR');

    ftpResponseINP = requestINP.GetResponse();
    responseINP = ftpResponseINP;

    reader = new System.IO.StreamReader(responseINP.GetResponseStream());
    utf8 = System.Text.Encoding::get_UTF8();
    bytes = utf8.GetBytes(reader.ReadToEnd());
    reader.Close();

    request = System.Net.WebRequest::Create(ftpFileName);
    credential = new System.Net.NetworkCredential("","","");
    credential.username = ftpParm.UserName;
    credential.password = ftpParm.passwordEdit(false,"");

    request.set_Credentials(credential);
    request.set_Method('STOR');
    requestStream = request.GetRequestStream();
    requestStream.Write(bytes,0,bytes.get_Length());
    requestStream.Close();

    ftpResponse = request.GetResponse();
    response = ftpResponse;
    }
    catch(Exception::CLRError)
    {
    throw error(AifUtil::getClrErrorMessage());
    }
    return resultfile;
    }

  • Ferdiansah Profile Picture
    403 on at

    But sometimes an error like "The remote server returned an error: (500) Syntax error, command unrecognized.". Anyone can  help me ?

  • Blue Wang Profile Picture
    Microsoft Employee on at

    Hi Ferdiansah,

    It's better for a thread to discuss only one topic.

    On your question, you can look at this.

    [View:https://stackoverflow.com/questions/21221300/fixing-system-net-webexception-the-remote-server-returned-an-error-500-syn:750:50]

  • Ferdiansah Profile Picture
    403 on at

    Hi Wang,

    I've tried it but the error still appears

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans