D365 Finance & Operations and Dynamics AX Forum

Expand all | Collapse all

Moving subfolders to a different folder via X++

  • 1.  Moving subfolders to a different folder via X++

    TOP CONTRIBUTOR
    Posted Feb 23, 2021 04:20 PM
    Edited by Rudy Salcedo Feb 23, 2021 04:36 PM
    Is there a way in AX 2012 to move a subfolder and all of its contents to a different folder via X++?  All the examples I've seen so far only show how to move a file from one folder to another folder.  For our purposes I need to move a subfolder and everything in it to a different folder.

    For example I need to move both test subfolders and their contents from C:\TEST_SOURCE  to   C:\TEST_DESTINATION, leaving C:\TEST_SOURCE empty when finished.


    Can this be accomplished via X++ code?  I know it can be done in .NET, but for our application we want to do it in X++ as part of a batch job that runs other processes in AX to determine which folders should be moved.​  So we need to be able to move the folders one at a time since not all folders will be moved.

    ------------------------------
    Rudy Salcedo
    Senior Programmer/Analyst
    LaForce, Inc
    Green Bay WI
    ------------------------------


  • 2.  RE: Moving subfolders to a different folder via X++

    Posted Feb 23, 2021 05:25 PM
    Hi Rudy,

    I do not have access to a 2012 VM but please verify this can't be done using namespace System.IO.Directory::Move(Source, Destination) type solution.

    Refer here: https://aristonjavier.wordpress.com/2015/10/21/ax-2012-moving-file-to-another-folder-using-system-io-file/

    See if you can't change System.IO.File::Move to System.IO.Directory::Move and get the results you expect.

    Bob Coward

    ------------------------------
    Bob Coward
    Dynamics AX/F&O Developer
    DSG
    Grayson GA
    ------------------------------



  • 3.  RE: Moving subfolders to a different folder via X++

    TOP CONTRIBUTOR
    Posted Feb 24, 2021 10:58 AM
    @Bob Coward, thanks for the suggestion.  I did see that blog earlier and tried to tweak it for our purposes, but couldn't get it to work.  I haven't used Directory::Move before, so ​​do you see anything wrong with the code that's preventing the folder from being moved?



    ------------------------------
    Rudy Salcedo
    Senior Programmer/Analyst
    LaForce, Inc
    Green Bay WI
    ------------------------------



  • 4.  RE: Moving subfolders to a different folder via X++

    Posted Feb 25, 2021 09:19 AM
    Hi Rudy,

    My Dev-VM won't let me run an F&O project so I can't test your code.  But looking at it I assume it will work.

    The Sytem.IO docs tell me an IOException will be thrown with Directory.Move() and File.Move() if the destination or file already exists. From your code if, C:\Test_Destination exists, you should get an Error::IOException

    I confirm that speculation with a console app on my VM. The below code works to your requirements if Bob2 does not exist.  it works the first time, then after putting the Bob folder back it will fail with an IOException error the second time because Bob2 folder exists

    class Program
    {
    static void Main(string[] args)
    {
    string source = "C:\\Bob";
    string destination = "C:\\Bob2";

    System.IO.Directory.Move(source, destination);

    }
    }

    ------------------------------
    Bob Coward
    Dynamics AX/F&O Developer
    DSG
    Grayson GA
    ------------------------------



  • 5.  RE: Moving subfolders to a different folder via X++

    TOP CONTRIBUTOR
    Posted Feb 25, 2021 10:35 AM
    ​Thanks Bob, it looks like that's the issue I'm experiencing too.  I changed my code a bit to use DirectoryInfo.​MoveTo instead of Directory.Move (hoping that it might work), and tweaked my Try/Catch block to catch the CLR error which indicates exactly what you suspected.



    If C:\TEST_DESTINATION doesn't already exist, then it works (sort of).  But if C:\TEST_DESTINATION already exists, it doesn't work.

    I'll see if I can tweak the code so it always works whether or not the destination folder already exists.  Thanks for your help!

    ------------------------------
    Rudy Salcedo
    Senior Programmer/Analyst
    LaForce, Inc
    Green Bay WI
    ------------------------------



If you've found this thread useful, dive deeper into User Group community content by role