Thursday 14 November 2019

Get file name from file path by X++

When you want to get a file name from long file path, you can use a method fileNameSplit() which you may find in Global class.

This method returns a container, where the first value is a path, the second file name (without extension), and the last third one is an extension.

You have to remember that path is returned already with last backslash '\', so for example C:\Test\
And also what is important, that last value (extension) is returned with dot '.', for example '.xml'

An example of code below

static void SplitFileName(Args _args)
{
    Filename    fileNameOrig = @'\\NetworkPath\In\FileName.xml';
    fileName    filePath;
    Filename    fileName;
    fileName    fileExt;
    
    [FilePath, fileName, fileExt] = fileNameSplit(fileNameOrig);

    info(FilePath);
    info(Filename);
    info(fileExt);

}

And a result