Quantcast
Channel: Microsoft Dynamics AX Forum - Recent Threads
Viewing all articles
Browse latest Browse all 72043

Sample code to loop through folder and get files in AX

$
0
0

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

 


Viewing all articles
Browse latest Browse all 72043

Trending Articles