Skip to main content

Microsoft Dynamics NAV - Get File Count from a Folder using Dotnet.

Hi All,

Today I want to discuss a code re-write that I had to do on a recent project about file handling.

Scenario -
PDF will be saved in a shared path whenever a purchase invoice is printed. To Generate File Name we were using Purchase Invoice No. and then extending that with an integer.

So if Purchase Invoice (PRO001) Report will be printed for the fifth time, the shared path will already have following files -

PRO001-1.pdf
PRO001-2.pdf
PRO001-3.pdf
PRO001-4.pdf

And Next file name should be PRO001-5.pdf.

In Pre 2009 R2 we don't have Dotnet DataType and the code that is written using File Data Type.

The Code is filtering-
1. The Shared Path Passed as Var to the function.
2. Set the type of files of Type is a FILE.
3. Then filter the File Name with the filter passed (in our case it will be passed as PRO001-%)


Then It counts the Files in the Folder.

But in my case, it was having Performance issue as the folder size is huge and filtering and then count was taking somewhere 03-05 minutes. To Enhance that code we tried to look into re-write it using DOTNET and the performance was WOW!

Here is what we did with Dotnet to get the file count using DOTNET Class. 

The Code is -
1. Generates a Filter String with the File Name filter passed.
    ** Just a reminder for DOTNET we don't require @ to filter as it's already case insensitive.

2. The Call the function inside Directory calls to get files from - Folder with Filter and Search Options set as to filter only files in current directory.


Here are the Dotnet Variables Used - 
Directory   - System.IO.Directory.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

Search Options - System.IO.SearchOption.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'


Additionally, if you require counting all files in folder and sub-folders then we just need to change the SearchOptions enumerations to AllDirectories as commented in above code.

Share your thoughts about it. I have so many rewrites where I use Dotnet to get performance or only option to rewrite in new versions. If you have a scenario that we can discuss in future articles please put also that as the comment.

If you want to use the code, please download an object from the on-drive. 
File Name - Microsoft Dynamics NAV - #SauravNav_Dotnet_File_Count_Codeunit_50000

Please share your views. Happy to hear all of them !! Stay connected will be back soon.

Regards,
Saurav Dhyani
www.sauravdhyani.com

Comments

Popular posts from this blog

VIEW SERVER STATE permission on SQL Server?

Hi all, Sometime While trying to Login into a database we face an error message as shown below. --------------------------- Microsoft Dynamics NAV Classic --------------------------- You cannot start Microsoft Dynamics NAV Classic because you do not have the VIEW SERVER STATE permission on SQL Server. Contact your system administrator. --------------------------- OK    ---------------------------

NAV 2013 R2 - Cumulative update 12 Released.

Hi all, Please find below the details of  Cumulative Update 12 released for Microsoft Dynamics NAV 2013 R2. Title - Cumulative Update 12 for Microsoft Dynamics NAV 2013 R2 Build No. - 38053 Release Date - October, 2014 Local Version Included - AU, AT, BE, CH, DE, DK, ES, FI, FR, IS, IT, NA, NL, NO, NZ, SE, UK, RU Download Link Note: Implementing this cumulative update will require a database conversion unless you have already implemented update rollup 5.

NAV 2013 Upgrade Part III - Data Upgrading.

Hi all, As per agenda we need to discuss the data upgrade in NAV 2013. The process is similar to what used to be till NAV 2009 R2 but with some changes. Let's start the process. Remember -  1) You can only upgrade a database to NAV 2013 from NAV 2009 SP1 / R2 SQL Only. 2) The Synchronization of users with SQL Server is no more required.