Browsing Folders for Files on 64-bit Server Side Implementation (Batch Run)

I am sure all of us have had a requirement where we were supposed to process a folder for specific file pattern / all file pattern and process these files.

This scenario is very prominent during integration of different applications with AX.

I scenarios where this process is run manually or on client side, we use the standard methods provided by Microsoft in WinApi class (FindFirstFile, FindNextFile, FindClose).

These methods in turn use the FindFirstFile, FindNextFile and FindClose methods defined in Kernel32 dll.

Now these methods are not provided on Server Side implementation of WinApi namely WinApiServer. But you can copy these methods from WinApi to WinApiServer and use them as required.

These methods work fine on any 32-bit OS on both client side and server side.

But the real issue comes when you are using these methods on 64-bit systems and that too on server side.

The reason being we need some redirection that needs to be done. I will speak about this re-direction in a separate blog post as I am still working on making this work inside Ax.

But other method that I found was to use System.IO class.

Here is a sample code with proper comments to make you understand.

server public static void getFiles()
System.IO.DirectoryInfo directoryInfo;
System.IO.FileInfo[] fileList;
System.IO.FileInfo file;
System.Exception e;
System.String moveName, fromName;

int i;
int fileCount;
str axMoveName, axFromName;

InterOpPermission perm;
FileIOPermission filePerm;
FilePath searchFilePath;
FileName moveFileName;

//Check if the folder exists
filePerm = new FileIOPermission(@"\AX_InBound\", ‘r’);

if (!WinApiServer::fileExists(@"\AX_InBound\", true))
throw error("Path not found");


//Check if the archive folder exists
filePerm = new FileIOPermission(@"\AX_Archive\", ‘r’);

if (!WinApiServer::fileExists(@"\AX_Archive\", true))
throw error("Archive Path not found");


//Check the permission for CLR InterOp
perm = new InterOpPermission(InteropKind::ClrInterop);

//Traverse to the inbound folder and search file
searchFilePath = @"\AX_InBound\";
directoryInfo = new System.IO.DirectoryInfo(searchFilePath);

//Get list of only text files
fileList = directoryInfo.GetFiles("*.txt");

//Get the file count
fileCount = fileList.get_Length();

//One by one get each file
for (i = 0; i < fileCount; i++) { file = fileList.GetValue(i); //.Net method returns System.String need to marshal it to AX str data type fromName = file.get_FullName(); axFromName = fromName; //Display the name of the fetched file info(fromName); //.Net method returns System.String need to marshal it to AX str data type moveName = file.get_Name(); axMoveName = moveName; //Move to archive folder moveFileName = @"\AX_Archive\" + axMoveName;

info("File moved to " + moveFileName);
e = ClrInterOp::getLastException();
while (e)
e = e.get_InnerException();
error("An unknown exception has occurred");


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.