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

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Sample code to loop through folder and get files in AX

(0) ShareShare
ReportReport
Posted on by

Create the class as shown below

class LIQ_FileSystemAdapter
{
    #Aif
    #File

    Microsoft.Dynamics.IntegrationFramework.Adapter.FileSystem fileSystem;
    int     fileCount;
    str     inputDirectory;
    str     fullFileName;
    str     fileName;
    str     filePath;
    str     fileExtension;

}

public str fileName()
{
    return fileName;
}

public void finalize()
{;
    fileSystem = null;
}

public str fullFileName()
{
    return fullFileName;
}

protected void init(str _inputFolder, str _fileFilter = "*.*")
{
    AifFileSystemAdapter fileSystemAdapter = new AifFileSystemAdapter();

    inputDirectory = _inputFolder;

    // Calling code must assert permission to access a new Clr object.
    // The permission is not asserted by this method.

    // BP Deviation Documented
    new InteropPermission(InteropKind::ClrInterop).assert();
    fileSystem = AifUtil::getClrObject(#FileSystemProgId);
    CodeAccessPermission::revertAssert();
    try
    {
        new InteropPermission(InteropKind::ClrInterop).assert();
        // BP Deviation Documented
        fileCount = fileSystem.FindFiles(inputDirectory, _fileFilter);
        CodeAccessPermission::revertAssert();
    }
    catch
    {
        // "Cannot read files from directory" is a fatal error.
        throw error(strfmt("@SYS95801", inputDirectory, AifUtil::getClrErrorMessage()));
    }
}

public boolean nextFile()
{;

    try
    {
        // BP Deviation Documented
        new InteropPermission(InteropKind::ClrInterop).assert();
        fullFileName = fileSystem.GetNextFile();
        CodeAccessPermission::revertAssert();

        [filePath, fileName, fileExtension] = fileNameSplit(fullFileName);
        fileName += fileExtension;

    }
    catch
    {
        // "Cannot determine next file to read" is a fatal error.
        throw error(strfmt("@SYS95802", AifUtil::getClrErrorMessage()));
    }

    return fullFileName == '' ? false : true;
}

public static LIQ_FileSystemAdapter construct(str _filePath)
{
    LIQ_FileSystemAdapter fileSystemAdapter = new LIQ_FileSystemAdapter();
    fileSystemAdapter.init(_filePath);

    return fileSystemAdapter;
}

How to use? check below code

LIQ_FileSystemAdapter       fileSystemAdapter;

;

 fileSystemAdapter   = LIQ_FileSystemAdapter::construct('folder path');
while (fileSystemAdapter.nextFile())
        {
                       imageFileName = fileSystemAdapter.fileName();
            imageFileNameWithOutExtension = this.getFileName(imageFileName);

       }

public str getFileName(str _fullFileName, boolean _considerSlashes = true)
{
    str     ret =_fullFileName;
    int     separatorPosition;

    ;

    if (_considerSlashes)
    {
        separatorPosition = strfind(_fullFileName, '.', strlen(_fullFileName), -strlen(_fullFileName));
        if(separatorPosition)
        {
            ret = substr(_fullFileName, 0, separatorPosition - 1);
        }
    }

    return ret;
}

 

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at
    RE: Sample code to loop through folder and get files in AX

    Raghu does the code works fine because I am trying some related things?

    Where is main()?

    Where is the variable imageFileName declared?

  • Okie Profile Picture
    95 on at
    RE: Sample code to loop through folder and get files in AX

    I have a problem in AX 2009 like this.

    I want to know all files in a directory (count and name of files unkown).

    After a bit searching i found this question with right .net class.

    My code works fine:

           Microsoft.Dynamics.IntegrationFramework.Adapter.FileSystem fileSystem;

           ;

           fileSystem = new Microsoft.Dynamics.IntegrationFramework.Adapter.FileSystem();

           fileSystem.FindFiles(@"\\server\path", "*");

           filename    = fileSystem.GetNextFile();

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#2
Guy Terry Profile Picture

Guy Terry 2 Moderator

#2
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans